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/vmalloc.h>
31 #include <linux/file.h>
32 #include <scsi/scsi_proto.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
) {
64 snprintf(buf
, size
, ",i,0x%s", pr_reg
->pr_reg_isid
);
69 REGISTER_AND_IGNORE_EXISTING_KEY
,
78 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
79 struct t10_pr_registration
*, int, int);
81 static int is_reservation_holder(
82 struct t10_pr_registration
*pr_res_holder
,
83 struct t10_pr_registration
*pr_reg
)
88 pr_res_type
= pr_res_holder
->pr_res_type
;
90 return pr_res_holder
== pr_reg
||
91 pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
||
92 pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
;
98 target_scsi2_reservation_check(struct se_cmd
*cmd
)
100 struct se_device
*dev
= cmd
->se_dev
;
101 struct se_session
*sess
= cmd
->se_sess
;
103 switch (cmd
->t_task_cdb
[0]) {
112 if (!dev
->dev_reserved_node_acl
|| !sess
)
115 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
116 return TCM_RESERVATION_CONFLICT
;
118 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
119 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
120 return TCM_RESERVATION_CONFLICT
;
126 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
127 struct se_node_acl
*, struct se_session
*);
128 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
130 static int target_check_scsi2_reservation_conflict(struct se_cmd
*cmd
)
132 struct se_session
*se_sess
= cmd
->se_sess
;
133 struct se_device
*dev
= cmd
->se_dev
;
134 struct t10_pr_registration
*pr_reg
;
135 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
138 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
142 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
145 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
146 * status, but no reservation shall be established and the
147 * persistent reservation shall not be changed, if the command
148 * is received from a) and b) below.
150 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
151 * status, but the persistent reservation shall not be released,
152 * if the command is received from a) and b)
154 * a) An I_T nexus that is a persistent reservation holder; or
155 * b) An I_T nexus that is registered if a registrants only or
156 * all registrants type persistent reservation is present.
158 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
159 * RELEASE(6) command, or RELEASE(10) command shall be processed
160 * as defined in SPC-2.
162 if (pr_reg
->pr_res_holder
) {
163 core_scsi3_put_pr_reg(pr_reg
);
166 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
167 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
168 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
169 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
170 core_scsi3_put_pr_reg(pr_reg
);
173 core_scsi3_put_pr_reg(pr_reg
);
177 * Following spc2r20 5.5.1 Reservations overview:
179 * If a logical unit has executed a PERSISTENT RESERVE OUT
180 * command with the REGISTER or the REGISTER AND IGNORE
181 * EXISTING KEY service action and is still registered by any
182 * initiator, all RESERVE commands and all RELEASE commands
183 * regardless of initiator shall conflict and shall terminate
184 * with a RESERVATION CONFLICT status.
186 spin_lock(&pr_tmpl
->registration_lock
);
187 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
188 spin_unlock(&pr_tmpl
->registration_lock
);
192 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
193 " while active SPC-3 registrations exist,"
194 " returning RESERVATION_CONFLICT\n");
202 target_scsi2_reservation_release(struct se_cmd
*cmd
)
204 struct se_device
*dev
= cmd
->se_dev
;
205 struct se_session
*sess
= cmd
->se_sess
;
206 struct se_portal_group
*tpg
;
209 if (!sess
|| !sess
->se_tpg
)
211 rc
= target_check_scsi2_reservation_conflict(cmd
);
215 return TCM_RESERVATION_CONFLICT
;
217 spin_lock(&dev
->dev_reservation_lock
);
218 if (!dev
->dev_reserved_node_acl
|| !sess
)
221 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
224 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
227 dev
->dev_reserved_node_acl
= NULL
;
228 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS
;
229 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
230 dev
->dev_res_bin_isid
= 0;
231 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS_WITH_ISID
;
234 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
235 " MAPPED LUN: %llu for %s\n",
236 tpg
->se_tpg_tfo
->get_fabric_name(),
237 cmd
->se_lun
->unpacked_lun
, cmd
->orig_fe_lun
,
238 sess
->se_node_acl
->initiatorname
);
241 spin_unlock(&dev
->dev_reservation_lock
);
243 target_complete_cmd(cmd
, GOOD
);
248 target_scsi2_reservation_reserve(struct se_cmd
*cmd
)
250 struct se_device
*dev
= cmd
->se_dev
;
251 struct se_session
*sess
= cmd
->se_sess
;
252 struct se_portal_group
*tpg
;
253 sense_reason_t ret
= 0;
256 if ((cmd
->t_task_cdb
[1] & 0x01) &&
257 (cmd
->t_task_cdb
[1] & 0x02)) {
258 pr_err("LongIO and Obselete Bits set, returning"
259 " ILLEGAL_REQUEST\n");
260 return TCM_UNSUPPORTED_SCSI_OPCODE
;
263 * This is currently the case for target_core_mod passthrough struct se_cmd
266 if (!sess
|| !sess
->se_tpg
)
268 rc
= target_check_scsi2_reservation_conflict(cmd
);
273 return TCM_RESERVATION_CONFLICT
;
276 spin_lock(&dev
->dev_reservation_lock
);
277 if (dev
->dev_reserved_node_acl
&&
278 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
279 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
280 tpg
->se_tpg_tfo
->get_fabric_name());
281 pr_err("Original reserver LUN: %llu %s\n",
282 cmd
->se_lun
->unpacked_lun
,
283 dev
->dev_reserved_node_acl
->initiatorname
);
284 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
285 " from %s \n", cmd
->se_lun
->unpacked_lun
,
287 sess
->se_node_acl
->initiatorname
);
288 ret
= TCM_RESERVATION_CONFLICT
;
292 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
293 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS
;
294 if (sess
->sess_bin_isid
!= 0) {
295 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
296 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS_WITH_ISID
;
298 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
299 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
300 cmd
->se_lun
->unpacked_lun
, cmd
->orig_fe_lun
,
301 sess
->se_node_acl
->initiatorname
);
304 spin_unlock(&dev
->dev_reservation_lock
);
307 target_complete_cmd(cmd
, GOOD
);
313 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
315 * This function is called by those initiator ports who are *NOT*
316 * the active PR reservation holder when a reservation is present.
318 static int core_scsi3_pr_seq_non_holder(struct se_cmd
*cmd
, u32 pr_reg_type
,
321 unsigned char *cdb
= cmd
->t_task_cdb
;
322 struct se_session
*se_sess
= cmd
->se_sess
;
323 struct se_node_acl
*nacl
= se_sess
->se_node_acl
;
325 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
326 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
327 int we
= 0; /* Write Exclusive */
328 int legacy
= 0; /* Act like a legacy device and return
329 * RESERVATION CONFLICT on some CDBs */
332 registered_nexus
= 0;
334 struct se_dev_entry
*se_deve
;
337 se_deve
= target_nacl_find_deve(nacl
, cmd
->orig_fe_lun
);
339 registered_nexus
= test_bit(DEF_PR_REG_ACTIVE
,
340 &se_deve
->deve_flags
);
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
353 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
355 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
357 * Some commands are only allowed for registered I_T Nexuses.
361 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
363 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
365 * Each registered I_T Nexus is a reservation holder.
373 * Referenced from spc4r17 table 45 for *NON* PR holder access
376 case SECURITY_PROTOCOL_IN
:
377 if (registered_nexus
)
385 case RECEIVE_DIAGNOSTIC
:
390 if (registered_nexus
) {
394 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
396 case PERSISTENT_RESERVE_OUT
:
398 * This follows PERSISTENT_RESERVE_OUT service actions that
399 * are allowed in the presence of various reservations.
400 * See spc4r17, table 46
402 switch (cdb
[1] & 0x1f) {
405 case PRO_PREEMPT_AND_ABORT
:
406 ret
= (registered_nexus
) ? 0 : 1;
409 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
412 case PRO_REGISTER_AND_MOVE
:
417 ret
= (registered_nexus
) ? 0 : 1;
420 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
421 " action: 0x%02x\n", cdb
[1] & 0x1f);
427 /* Handled by CRH=1 in target_scsi2_reservation_release() */
432 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
435 case TEST_UNIT_READY
:
436 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
439 switch (cdb
[1] & 0x1f) {
440 case MI_MANAGEMENT_PROTOCOL_IN
:
441 if (registered_nexus
) {
445 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
447 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
448 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
453 if (registered_nexus
) {
457 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
459 case MI_REPORT_ALIASES
:
460 case MI_REPORT_IDENTIFYING_INFORMATION
:
461 case MI_REPORT_PRIORITY
:
462 case MI_REPORT_TARGET_PGS
:
463 case MI_REPORT_TIMESTAMP
:
464 ret
= 0; /* Allowed */
467 pr_err("Unknown MI Service Action: 0x%02x\n",
472 case ACCESS_CONTROL_IN
:
473 case ACCESS_CONTROL_OUT
:
476 case SERVICE_ACTION_IN_12
:
479 case PERSISTENT_RESERVE_IN
:
480 ret
= 0; /*/ Allowed CDBs */
487 * Case where the CDB is explicitly allowed in the above switch
490 if (!ret
&& !other_cdb
) {
491 pr_debug("Allowing explicit CDB: 0x%02x for %s"
492 " reservation holder\n", cdb
[0],
493 core_scsi3_pr_dump_type(pr_reg_type
));
498 * Check if write exclusive initiator ports *NOT* holding the
499 * WRITE_EXCLUSIVE_* reservation.
501 if (we
&& !registered_nexus
) {
502 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
504 * Conflict for write exclusive
506 pr_debug("%s Conflict for unregistered nexus"
507 " %s CDB: 0x%02x to %s reservation\n",
508 transport_dump_cmd_direction(cmd
),
509 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
510 core_scsi3_pr_dump_type(pr_reg_type
));
514 * Allow non WRITE CDBs for all Write Exclusive
515 * PR TYPEs to pass for registered and
516 * non-registered_nexuxes NOT holding the reservation.
518 * We only make noise for the unregisterd nexuses,
519 * as we expect registered non-reservation holding
520 * nexuses to issue CDBs.
523 if (!registered_nexus
) {
524 pr_debug("Allowing implicit CDB: 0x%02x"
525 " for %s reservation on unregistered"
527 core_scsi3_pr_dump_type(pr_reg_type
));
532 } else if ((reg_only
) || (all_reg
)) {
533 if (registered_nexus
) {
535 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
536 * allow commands from registered nexuses.
539 pr_debug("Allowing implicit CDB: 0x%02x for %s"
540 " reservation\n", cdb
[0],
541 core_scsi3_pr_dump_type(pr_reg_type
));
545 } else if (we
&& registered_nexus
) {
547 * Reads are allowed for Write Exclusive locks
548 * from all registrants.
550 if (cmd
->data_direction
== DMA_FROM_DEVICE
) {
551 pr_debug("Allowing READ CDB: 0x%02x for %s"
552 " reservation\n", cdb
[0],
553 core_scsi3_pr_dump_type(pr_reg_type
));
558 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
559 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
560 (registered_nexus
) ? "" : "un",
561 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
562 core_scsi3_pr_dump_type(pr_reg_type
));
564 return 1; /* Conflict by default */
567 static sense_reason_t
568 target_scsi3_pr_reservation_check(struct se_cmd
*cmd
)
570 struct se_device
*dev
= cmd
->se_dev
;
571 struct se_session
*sess
= cmd
->se_sess
;
573 bool isid_mismatch
= false;
575 if (!dev
->dev_pr_res_holder
)
578 pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
579 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
580 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
)
581 goto check_nonholder
;
583 if (dev
->dev_pr_res_holder
->isid_present_at_reg
) {
584 if (dev
->dev_pr_res_holder
->pr_reg_bin_isid
!=
585 sess
->sess_bin_isid
) {
586 isid_mismatch
= true;
587 goto check_nonholder
;
594 if (core_scsi3_pr_seq_non_holder(cmd
, pr_reg_type
, isid_mismatch
))
595 return TCM_RESERVATION_CONFLICT
;
599 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
604 * PRGeneration field shall contain the value of a 32-bit wrapping
605 * counter mainted by the device server.
607 * Note that this is done regardless of Active Persist across
608 * Target PowerLoss (APTPL)
610 * See spc4r17 section 6.3.12 READ_KEYS service action
612 spin_lock(&dev
->dev_reservation_lock
);
613 prg
= dev
->t10_pr
.pr_generation
++;
614 spin_unlock(&dev
->dev_reservation_lock
);
619 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
620 struct se_device
*dev
,
621 struct se_node_acl
*nacl
,
623 struct se_dev_entry
*dest_deve
,
630 struct t10_pr_registration
*pr_reg
;
632 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
634 pr_err("Unable to allocate struct t10_pr_registration\n");
638 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
639 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
640 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
641 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
642 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
643 atomic_set(&pr_reg
->pr_res_holders
, 0);
644 pr_reg
->pr_reg_nacl
= nacl
;
646 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
647 * the se_dev_entry->pr_ref will have been already obtained by
648 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
650 * Otherwise, locate se_dev_entry now and obtain a reference until
651 * registration completes in __core_scsi3_add_registration().
654 pr_reg
->pr_reg_deve
= dest_deve
;
657 pr_reg
->pr_reg_deve
= target_nacl_find_deve(nacl
, mapped_lun
);
658 if (!pr_reg
->pr_reg_deve
) {
660 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
661 nacl
->initiatorname
, mapped_lun
);
662 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
665 kref_get(&pr_reg
->pr_reg_deve
->pr_kref
);
668 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
669 pr_reg
->pr_aptpl_target_lun
= lun
->unpacked_lun
;
670 pr_reg
->tg_pt_sep_rtpi
= lun
->lun_rtpi
;
671 pr_reg
->pr_res_key
= sa_res_key
;
672 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
673 pr_reg
->pr_reg_aptpl
= aptpl
;
675 * If an ISID value for this SCSI Initiator Port exists,
676 * save it to the registration now.
679 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
680 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
681 pr_reg
->isid_present_at_reg
= 1;
687 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
688 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
691 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
694 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
695 struct se_device
*dev
,
696 struct se_node_acl
*nacl
,
698 struct se_dev_entry
*deve
,
705 struct se_dev_entry
*deve_tmp
;
706 struct se_node_acl
*nacl_tmp
;
707 struct se_lun_acl
*lacl_tmp
;
708 struct se_lun
*lun_tmp
, *next
, *dest_lun
;
709 const struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
710 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
713 * Create a registration for the I_T Nexus upon which the
714 * PROUT REGISTER was received.
716 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, lun
, deve
, mapped_lun
,
717 isid
, sa_res_key
, all_tg_pt
,
722 * Return pointer to pr_reg for ALL_TG_PT=0
727 * Create list of matching SCSI Initiator Port registrations
730 spin_lock(&dev
->se_port_lock
);
731 list_for_each_entry_safe(lun_tmp
, next
, &dev
->dev_sep_list
, lun_dev_link
) {
732 if (!percpu_ref_tryget_live(&lun_tmp
->lun_ref
))
734 spin_unlock(&dev
->se_port_lock
);
736 spin_lock(&lun_tmp
->lun_deve_lock
);
737 list_for_each_entry(deve_tmp
, &lun_tmp
->lun_deve_list
, lun_link
) {
739 * This pointer will be NULL for demo mode MappedLUNs
740 * that have not been make explicit via a ConfigFS
741 * MappedLUN group for the SCSI Initiator Node ACL.
743 if (!deve_tmp
->se_lun_acl
)
746 lacl_tmp
= rcu_dereference_check(deve_tmp
->se_lun_acl
,
747 lockdep_is_held(&lun_tmp
->lun_deve_lock
));
748 nacl_tmp
= lacl_tmp
->se_lun_nacl
;
750 * Skip the matching struct se_node_acl that is allocated
753 if (nacl
== nacl_tmp
)
756 * Only perform PR registrations for target ports on
757 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
760 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
763 * Look for a matching Initiator Node ACL in ASCII format
765 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
768 kref_get(&deve_tmp
->pr_kref
);
769 spin_unlock(&lun_tmp
->lun_deve_lock
);
771 * Grab a configfs group dependency that is released
772 * for the exception path at label out: below, or upon
773 * completion of adding ALL_TG_PT=1 registrations in
774 * __core_scsi3_add_registration()
776 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
778 pr_err("core_scsi3_lunacl_depend"
780 percpu_ref_put(&lun_tmp
->lun_ref
);
781 kref_put(&deve_tmp
->pr_kref
, target_pr_kref_release
);
785 * Located a matching SCSI Initiator Port on a different
786 * port, allocate the pr_reg_atp and attach it to the
787 * pr_reg->pr_reg_atp_list that will be processed once
788 * the original *pr_reg is processed in
789 * __core_scsi3_add_registration()
791 dest_lun
= rcu_dereference_check(deve_tmp
->se_lun
,
792 atomic_read(&deve_tmp
->pr_kref
.refcount
) != 0);
794 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
795 nacl_tmp
, dest_lun
, deve_tmp
,
796 deve_tmp
->mapped_lun
, NULL
,
797 sa_res_key
, all_tg_pt
, aptpl
);
799 percpu_ref_put(&lun_tmp
->lun_ref
);
800 core_scsi3_lunacl_undepend_item(deve_tmp
);
804 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
805 &pr_reg
->pr_reg_atp_list
);
806 spin_lock(&lun_tmp
->lun_deve_lock
);
808 spin_unlock(&lun_tmp
->lun_deve_lock
);
810 spin_lock(&dev
->se_port_lock
);
811 percpu_ref_put(&lun_tmp
->lun_ref
);
813 spin_unlock(&dev
->se_port_lock
);
817 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
818 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
819 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
820 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
821 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
823 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
827 int core_scsi3_alloc_aptpl_registration(
828 struct t10_reservation
*pr_tmpl
,
830 unsigned char *i_port
,
833 unsigned char *t_port
,
840 struct t10_pr_registration
*pr_reg
;
842 if (!i_port
|| !t_port
|| !sa_res_key
) {
843 pr_err("Illegal parameters for APTPL registration\n");
847 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
849 pr_err("Unable to allocate struct t10_pr_registration\n");
853 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
854 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
855 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
856 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
857 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
858 atomic_set(&pr_reg
->pr_res_holders
, 0);
859 pr_reg
->pr_reg_nacl
= NULL
;
860 pr_reg
->pr_reg_deve
= NULL
;
861 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
862 pr_reg
->pr_aptpl_target_lun
= target_lun
;
863 pr_reg
->pr_res_key
= sa_res_key
;
864 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
865 pr_reg
->pr_reg_aptpl
= 1;
866 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
867 pr_reg
->pr_res_type
= type
;
869 * If an ISID value had been saved in APTPL metadata for this
870 * SCSI Initiator Port, restore it now.
873 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
874 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
875 pr_reg
->isid_present_at_reg
= 1;
878 * Copy the i_port and t_port information from caller.
880 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
881 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
882 pr_reg
->pr_reg_tpgt
= tpgt
;
884 * Set pr_res_holder from caller, the pr_reg who is the reservation
885 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
886 * the Initiator Node LUN ACL from the fabric module is created for
889 pr_reg
->pr_res_holder
= res_holder
;
891 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
892 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
893 " metadata\n", (res_holder
) ? "+reservation" : "");
897 static void core_scsi3_aptpl_reserve(
898 struct se_device
*dev
,
899 struct se_portal_group
*tpg
,
900 struct se_node_acl
*node_acl
,
901 struct t10_pr_registration
*pr_reg
)
903 char i_buf
[PR_REG_ISID_ID_LEN
];
905 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
906 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
908 spin_lock(&dev
->dev_reservation_lock
);
909 dev
->dev_pr_res_holder
= pr_reg
;
910 spin_unlock(&dev
->dev_reservation_lock
);
912 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
913 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
914 tpg
->se_tpg_tfo
->get_fabric_name(),
915 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
916 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
917 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
918 tpg
->se_tpg_tfo
->get_fabric_name(), node_acl
->initiatorname
,
922 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
923 struct t10_pr_registration
*, enum register_type
, int);
925 static int __core_scsi3_check_aptpl_registration(
926 struct se_device
*dev
,
927 struct se_portal_group
*tpg
,
930 struct se_node_acl
*nacl
,
933 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
934 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
935 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
936 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
939 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
940 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
942 * Copy Initiator Port information from struct se_node_acl
944 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
945 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
946 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
947 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
949 * Look for the matching registrations+reservation from those
950 * created from APTPL metadata. Note that multiple registrations
951 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
954 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
955 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
958 if (!strcmp(pr_reg
->pr_iport
, i_port
) &&
959 (pr_reg
->pr_res_mapped_lun
== mapped_lun
) &&
960 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
961 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
962 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
964 * Obtain the ->pr_reg_deve pointer + reference, that
965 * is released by __core_scsi3_add_registration() below.
968 pr_reg
->pr_reg_deve
= target_nacl_find_deve(nacl
, mapped_lun
);
969 if (!pr_reg
->pr_reg_deve
) {
970 pr_err("Unable to locate PR APTPL %s mapped_lun:"
971 " %llu\n", nacl
->initiatorname
, mapped_lun
);
975 kref_get(&pr_reg
->pr_reg_deve
->pr_kref
);
978 pr_reg
->pr_reg_nacl
= nacl
;
979 pr_reg
->tg_pt_sep_rtpi
= lun
->lun_rtpi
;
980 list_del(&pr_reg
->pr_reg_aptpl_list
);
981 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
983 * At this point all of the pointers in *pr_reg will
984 * be setup, so go ahead and add the registration.
986 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
988 * If this registration is the reservation holder,
989 * make that happen now..
991 if (pr_reg
->pr_res_holder
)
992 core_scsi3_aptpl_reserve(dev
, tpg
,
995 * Reenable pr_aptpl_active to accept new metadata
996 * updates once the SCSI device is active again..
998 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
999 pr_tmpl
->pr_aptpl_active
= 1;
1002 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1007 int core_scsi3_check_aptpl_registration(
1008 struct se_device
*dev
,
1009 struct se_portal_group
*tpg
,
1011 struct se_node_acl
*nacl
,
1014 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
1017 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
1018 lun
->unpacked_lun
, nacl
,
1022 static void __core_scsi3_dump_registration(
1023 const struct target_core_fabric_ops
*tfo
,
1024 struct se_device
*dev
,
1025 struct se_node_acl
*nacl
,
1026 struct t10_pr_registration
*pr_reg
,
1027 enum register_type register_type
)
1029 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
1030 char i_buf
[PR_REG_ISID_ID_LEN
];
1032 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
1033 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1035 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1036 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== REGISTER_AND_MOVE
) ?
1037 "_AND_MOVE" : (register_type
== REGISTER_AND_IGNORE_EXISTING_KEY
) ?
1038 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
1040 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1041 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
1042 tfo
->tpg_get_tag(se_tpg
));
1043 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1044 " Port(s)\n", tfo
->get_fabric_name(),
1045 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1046 dev
->transport
->name
);
1047 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1048 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
1049 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
1050 pr_reg
->pr_reg_aptpl
);
1053 static void __core_scsi3_add_registration(
1054 struct se_device
*dev
,
1055 struct se_node_acl
*nacl
,
1056 struct t10_pr_registration
*pr_reg
,
1057 enum register_type register_type
,
1060 const struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1061 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1062 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1063 struct se_dev_entry
*deve
;
1066 * Increment PRgeneration counter for struct se_device upon a successful
1067 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1069 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1070 * action, the struct se_device->dev_reservation_lock will already be held,
1071 * so we do not call core_scsi3_pr_generation() which grabs the lock
1074 pr_reg
->pr_res_generation
= (register_move
) ?
1075 dev
->t10_pr
.pr_generation
++ :
1076 core_scsi3_pr_generation(dev
);
1078 spin_lock(&pr_tmpl
->registration_lock
);
1079 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1081 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1082 spin_unlock(&pr_tmpl
->registration_lock
);
1084 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1086 if (!pr_reg
->pr_reg_all_tg_pt
|| register_move
)
1089 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1090 * allocated in __core_scsi3_alloc_registration()
1092 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1093 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1094 struct se_node_acl
*nacl_tmp
= pr_reg_tmp
->pr_reg_nacl
;
1096 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1098 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1100 spin_lock(&pr_tmpl
->registration_lock
);
1101 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1102 &pr_tmpl
->registration_list
);
1104 __core_scsi3_dump_registration(tfo
, dev
, nacl_tmp
, pr_reg_tmp
,
1106 spin_unlock(&pr_tmpl
->registration_lock
);
1108 * Drop configfs group dependency reference and deve->pr_kref
1109 * obtained from __core_scsi3_alloc_registration() code.
1112 deve
= pr_reg_tmp
->pr_reg_deve
;
1114 set_bit(DEF_PR_REG_ACTIVE
, &deve
->deve_flags
);
1115 core_scsi3_lunacl_undepend_item(deve
);
1116 pr_reg_tmp
->pr_reg_deve
= NULL
;
1122 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1125 deve
= pr_reg
->pr_reg_deve
;
1127 set_bit(DEF_PR_REG_ACTIVE
, &deve
->deve_flags
);
1128 kref_put(&deve
->pr_kref
, target_pr_kref_release
);
1129 pr_reg
->pr_reg_deve
= NULL
;
1134 static int core_scsi3_alloc_registration(
1135 struct se_device
*dev
,
1136 struct se_node_acl
*nacl
,
1138 struct se_dev_entry
*deve
,
1140 unsigned char *isid
,
1144 enum register_type register_type
,
1147 struct t10_pr_registration
*pr_reg
;
1149 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, lun
, deve
, mapped_lun
,
1150 isid
, sa_res_key
, all_tg_pt
,
1155 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1156 register_type
, register_move
);
1160 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1161 struct se_device
*dev
,
1162 struct se_node_acl
*nacl
,
1163 unsigned char *isid
)
1165 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1166 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1167 struct se_portal_group
*tpg
;
1169 spin_lock(&pr_tmpl
->registration_lock
);
1170 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1171 &pr_tmpl
->registration_list
, pr_reg_list
) {
1173 * First look for a matching struct se_node_acl
1175 if (pr_reg
->pr_reg_nacl
!= nacl
)
1178 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1180 * If this registration does NOT contain a fabric provided
1181 * ISID, then we have found a match.
1183 if (!pr_reg
->isid_present_at_reg
) {
1185 * Determine if this SCSI device server requires that
1186 * SCSI Intiatior TransportID w/ ISIDs is enforced
1187 * for fabric modules (iSCSI) requiring them.
1189 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1190 if (dev
->dev_attrib
.enforce_pr_isids
)
1193 atomic_inc_mb(&pr_reg
->pr_res_holders
);
1194 spin_unlock(&pr_tmpl
->registration_lock
);
1198 * If the *pr_reg contains a fabric defined ISID for multi-value
1199 * SCSI Initiator Port TransportIDs, then we expect a valid
1200 * matching ISID to be provided by the local SCSI Initiator Port.
1204 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1207 atomic_inc_mb(&pr_reg
->pr_res_holders
);
1208 spin_unlock(&pr_tmpl
->registration_lock
);
1211 spin_unlock(&pr_tmpl
->registration_lock
);
1216 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1217 struct se_device
*dev
,
1218 struct se_node_acl
*nacl
,
1219 struct se_session
*sess
)
1221 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1222 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1224 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1225 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1226 tpg
->se_tpg_tfo
->sess_get_initiator_sid(sess
, &buf
[0],
1231 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1234 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1236 atomic_dec_mb(&pr_reg
->pr_res_holders
);
1239 static int core_scsi3_check_implicit_release(
1240 struct se_device
*dev
,
1241 struct t10_pr_registration
*pr_reg
)
1243 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1244 struct t10_pr_registration
*pr_res_holder
;
1247 spin_lock(&dev
->dev_reservation_lock
);
1248 pr_res_holder
= dev
->dev_pr_res_holder
;
1249 if (!pr_res_holder
) {
1250 spin_unlock(&dev
->dev_reservation_lock
);
1253 if (pr_res_holder
== pr_reg
) {
1255 * Perform an implicit RELEASE if the registration that
1256 * is being released is holding the reservation.
1258 * From spc4r17, section 5.7.11.1:
1260 * e) If the I_T nexus is the persistent reservation holder
1261 * and the persistent reservation is not an all registrants
1262 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1263 * service action or REGISTER AND IGNORE EXISTING KEY
1264 * service action with the SERVICE ACTION RESERVATION KEY
1265 * field set to zero (see 5.7.11.3).
1267 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0, 1);
1270 * For 'All Registrants' reservation types, all existing
1271 * registrations are still processed as reservation holders
1272 * in core_scsi3_pr_seq_non_holder() after the initial
1273 * reservation holder is implicitly released here.
1275 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1276 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1277 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1278 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1279 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1280 " UNREGISTER while existing reservation with matching"
1281 " key 0x%016Lx is present from another SCSI Initiator"
1282 " Port\n", pr_reg
->pr_res_key
);
1285 spin_unlock(&dev
->dev_reservation_lock
);
1291 * Called with struct t10_reservation->registration_lock held.
1293 static void __core_scsi3_free_registration(
1294 struct se_device
*dev
,
1295 struct t10_pr_registration
*pr_reg
,
1296 struct list_head
*preempt_and_abort_list
,
1298 __releases(&pr_tmpl
->registration_lock
)
1299 __acquires(&pr_tmpl
->registration_lock
)
1301 const struct target_core_fabric_ops
*tfo
=
1302 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1303 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1304 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1305 struct se_dev_entry
*deve
;
1306 char i_buf
[PR_REG_ISID_ID_LEN
];
1308 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1309 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1311 if (!list_empty(&pr_reg
->pr_reg_list
))
1312 list_del(&pr_reg
->pr_reg_list
);
1314 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1315 * so call core_scsi3_put_pr_reg() to decrement our reference.
1318 core_scsi3_put_pr_reg(pr_reg
);
1320 spin_unlock(&pr_tmpl
->registration_lock
);
1322 * Wait until all reference from any other I_T nexuses for this
1323 * *pr_reg have been released. Because list_del() is called above,
1324 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1325 * count back to zero, and we release *pr_reg.
1327 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1328 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1329 tfo
->get_fabric_name());
1334 deve
= target_nacl_find_deve(nacl
, pr_reg
->pr_res_mapped_lun
);
1336 clear_bit(DEF_PR_REG_ACTIVE
, &deve
->deve_flags
);
1339 spin_lock(&pr_tmpl
->registration_lock
);
1340 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1341 " Node: %s%s\n", tfo
->get_fabric_name(),
1342 pr_reg
->pr_reg_nacl
->initiatorname
,
1344 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1345 " Port(s)\n", tfo
->get_fabric_name(),
1346 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1347 dev
->transport
->name
);
1348 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1349 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1350 pr_reg
->pr_res_generation
);
1352 if (!preempt_and_abort_list
) {
1353 pr_reg
->pr_reg_deve
= NULL
;
1354 pr_reg
->pr_reg_nacl
= NULL
;
1355 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1359 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1360 * are released once the ABORT_TASK_SET has completed..
1362 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1365 void core_scsi3_free_pr_reg_from_nacl(
1366 struct se_device
*dev
,
1367 struct se_node_acl
*nacl
)
1369 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1370 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1371 bool free_reg
= false;
1373 * If the passed se_node_acl matches the reservation holder,
1374 * release the reservation.
1376 spin_lock(&dev
->dev_reservation_lock
);
1377 pr_res_holder
= dev
->dev_pr_res_holder
;
1378 if ((pr_res_holder
!= NULL
) &&
1379 (pr_res_holder
->pr_reg_nacl
== nacl
)) {
1380 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0, 1);
1383 spin_unlock(&dev
->dev_reservation_lock
);
1385 * Release any registration associated with the struct se_node_acl.
1387 spin_lock(&pr_tmpl
->registration_lock
);
1388 if (pr_res_holder
&& free_reg
)
1389 __core_scsi3_free_registration(dev
, pr_res_holder
, NULL
, 0);
1391 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1392 &pr_tmpl
->registration_list
, pr_reg_list
) {
1394 if (pr_reg
->pr_reg_nacl
!= nacl
)
1397 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1399 spin_unlock(&pr_tmpl
->registration_lock
);
1402 void core_scsi3_free_all_registrations(
1403 struct se_device
*dev
)
1405 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1406 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1408 spin_lock(&dev
->dev_reservation_lock
);
1409 pr_res_holder
= dev
->dev_pr_res_holder
;
1410 if (pr_res_holder
!= NULL
) {
1411 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1412 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1413 pr_res_holder
, 0, 0);
1415 spin_unlock(&dev
->dev_reservation_lock
);
1417 spin_lock(&pr_tmpl
->registration_lock
);
1418 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1419 &pr_tmpl
->registration_list
, pr_reg_list
) {
1421 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1423 spin_unlock(&pr_tmpl
->registration_lock
);
1425 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1426 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1427 pr_reg_aptpl_list
) {
1428 list_del(&pr_reg
->pr_reg_aptpl_list
);
1429 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1431 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1434 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1436 return target_depend_item(&tpg
->tpg_group
.cg_item
);
1439 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1441 target_undepend_item(&tpg
->tpg_group
.cg_item
);
1442 atomic_dec_mb(&tpg
->tpg_pr_ref_count
);
1445 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1447 if (nacl
->dynamic_node_acl
)
1449 return target_depend_item(&nacl
->acl_group
.cg_item
);
1452 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1454 if (!nacl
->dynamic_node_acl
)
1455 target_undepend_item(&nacl
->acl_group
.cg_item
);
1456 atomic_dec_mb(&nacl
->acl_pr_ref_count
);
1459 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1461 struct se_lun_acl
*lun_acl
;
1464 * For nacl->dynamic_node_acl=1
1466 lun_acl
= rcu_dereference_check(se_deve
->se_lun_acl
,
1467 atomic_read(&se_deve
->pr_kref
.refcount
) != 0);
1471 return target_depend_item(&lun_acl
->se_lun_group
.cg_item
);
1474 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1476 struct se_lun_acl
*lun_acl
;
1479 * For nacl->dynamic_node_acl=1
1481 lun_acl
= rcu_dereference_check(se_deve
->se_lun_acl
,
1482 atomic_read(&se_deve
->pr_kref
.refcount
) != 0);
1484 kref_put(&se_deve
->pr_kref
, target_pr_kref_release
);
1488 target_undepend_item(&lun_acl
->se_lun_group
.cg_item
);
1489 kref_put(&se_deve
->pr_kref
, target_pr_kref_release
);
1492 static sense_reason_t
1493 core_scsi3_decode_spec_i_port(
1495 struct se_portal_group
*tpg
,
1496 unsigned char *l_isid
,
1501 struct se_device
*dev
= cmd
->se_dev
;
1502 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1503 struct se_session
*se_sess
= cmd
->se_sess
;
1504 struct se_node_acl
*dest_node_acl
= NULL
;
1505 struct se_dev_entry
*dest_se_deve
= NULL
;
1506 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1507 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1508 LIST_HEAD(tid_dest_list
);
1509 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1510 unsigned char *buf
, *ptr
, proto_ident
;
1511 const unsigned char *i_str
= NULL
;
1512 char *iport_ptr
= NULL
, i_buf
[PR_REG_ISID_ID_LEN
];
1514 u32 tpdl
, tid_len
= 0;
1518 * Allocate a struct pr_transport_id_holder and setup the
1519 * local_node_acl pointer and add to struct list_head tid_dest_list
1520 * for add registration processing in the loop of tid_dest_list below.
1522 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1524 pr_err("Unable to allocate tidh_new\n");
1525 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1527 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1528 tidh_new
->dest_tpg
= tpg
;
1529 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1531 local_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1532 se_sess
->se_node_acl
, cmd
->se_lun
,
1533 NULL
, cmd
->orig_fe_lun
, l_isid
,
1534 sa_res_key
, all_tg_pt
, aptpl
);
1535 if (!local_pr_reg
) {
1537 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1539 tidh_new
->dest_pr_reg
= local_pr_reg
;
1541 * The local I_T nexus does not hold any configfs dependances,
1542 * so we set tidh_new->dest_se_deve to NULL to prevent the
1543 * configfs_undepend_item() calls in the tid_dest_list loops below.
1545 tidh_new
->dest_se_deve
= NULL
;
1546 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1548 if (cmd
->data_length
< 28) {
1549 pr_warn("SPC-PR: Received PR OUT parameter list"
1550 " length too small: %u\n", cmd
->data_length
);
1551 ret
= TCM_INVALID_PARAMETER_LIST
;
1555 buf
= transport_kmap_data_sg(cmd
);
1557 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1562 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1563 * first extract TransportID Parameter Data Length, and make sure
1564 * the value matches up to the SCSI expected data transfer length.
1566 tpdl
= (buf
[24] & 0xff) << 24;
1567 tpdl
|= (buf
[25] & 0xff) << 16;
1568 tpdl
|= (buf
[26] & 0xff) << 8;
1569 tpdl
|= buf
[27] & 0xff;
1571 if ((tpdl
+ 28) != cmd
->data_length
) {
1572 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1573 " does not equal CDB data_length: %u\n", tpdl
,
1575 ret
= TCM_INVALID_PARAMETER_LIST
;
1579 * Start processing the received transport IDs using the
1580 * receiving I_T Nexus portal's fabric dependent methods to
1581 * obtain the SCSI Initiator Port/Device Identifiers.
1586 struct se_lun
*dest_lun
, *tmp_lun
;
1588 proto_ident
= (ptr
[0] & 0x0f);
1591 spin_lock(&dev
->se_port_lock
);
1592 list_for_each_entry(tmp_lun
, &dev
->dev_sep_list
, lun_dev_link
) {
1593 tmp_tpg
= tmp_lun
->lun_tpg
;
1596 * Look for the matching proto_ident provided by
1597 * the received TransportID
1599 if (tmp_tpg
->proto_id
!= proto_ident
)
1601 dest_rtpi
= tmp_lun
->lun_rtpi
;
1603 i_str
= target_parse_pr_out_transport_id(tmp_tpg
,
1604 (const char *)ptr
, &tid_len
, &iport_ptr
);
1608 atomic_inc_mb(&tmp_tpg
->tpg_pr_ref_count
);
1609 spin_unlock(&dev
->se_port_lock
);
1611 if (core_scsi3_tpg_depend_item(tmp_tpg
)) {
1612 pr_err(" core_scsi3_tpg_depend_item()"
1614 atomic_dec_mb(&tmp_tpg
->tpg_pr_ref_count
);
1615 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1619 * Locate the destination initiator ACL to be registered
1620 * from the decoded fabric module specific TransportID
1623 mutex_lock(&tmp_tpg
->acl_node_mutex
);
1624 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1627 atomic_inc_mb(&dest_node_acl
->acl_pr_ref_count
);
1628 mutex_unlock(&tmp_tpg
->acl_node_mutex
);
1630 if (!dest_node_acl
) {
1631 core_scsi3_tpg_undepend_item(tmp_tpg
);
1632 spin_lock(&dev
->se_port_lock
);
1636 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
1637 pr_err("configfs_depend_item() failed"
1638 " for dest_node_acl->acl_group\n");
1639 atomic_dec_mb(&dest_node_acl
->acl_pr_ref_count
);
1640 core_scsi3_tpg_undepend_item(tmp_tpg
);
1641 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1646 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1647 " %s Port RTPI: %hu\n",
1648 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1649 dest_node_acl
->initiatorname
, dest_rtpi
);
1651 spin_lock(&dev
->se_port_lock
);
1654 spin_unlock(&dev
->se_port_lock
);
1657 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1659 ret
= TCM_INVALID_PARAMETER_LIST
;
1663 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1664 " tid_len: %d for %s + %s\n",
1665 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1666 tpdl
, tid_len
, i_str
, iport_ptr
);
1668 if (tid_len
> tpdl
) {
1669 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1670 " %u for Transport ID: %s\n", tid_len
, ptr
);
1671 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1672 core_scsi3_tpg_undepend_item(dest_tpg
);
1673 ret
= TCM_INVALID_PARAMETER_LIST
;
1677 * Locate the desintation struct se_dev_entry pointer for matching
1678 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1681 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1683 if (!dest_se_deve
) {
1684 pr_err("Unable to locate %s dest_se_deve"
1685 " from destination RTPI: %hu\n",
1686 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1689 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1690 core_scsi3_tpg_undepend_item(dest_tpg
);
1691 ret
= TCM_INVALID_PARAMETER_LIST
;
1695 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
1696 pr_err("core_scsi3_lunacl_depend_item()"
1698 kref_put(&dest_se_deve
->pr_kref
, target_pr_kref_release
);
1699 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1700 core_scsi3_tpg_undepend_item(dest_tpg
);
1701 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1705 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1706 " dest_se_deve mapped_lun: %llu\n",
1707 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1708 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1711 * Skip any TransportIDs that already have a registration for
1714 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1717 core_scsi3_put_pr_reg(pr_reg_e
);
1718 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1719 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1720 core_scsi3_tpg_undepend_item(dest_tpg
);
1727 * Allocate a struct pr_transport_id_holder and setup
1728 * the dest_node_acl and dest_se_deve pointers for the
1731 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1734 pr_err("Unable to allocate tidh_new\n");
1735 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1736 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1737 core_scsi3_tpg_undepend_item(dest_tpg
);
1738 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1741 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1742 tidh_new
->dest_tpg
= dest_tpg
;
1743 tidh_new
->dest_node_acl
= dest_node_acl
;
1744 tidh_new
->dest_se_deve
= dest_se_deve
;
1747 * Allocate, but do NOT add the registration for the
1748 * TransportID referenced SCSI Initiator port. This
1749 * done because of the following from spc4r17 in section
1750 * 6.14.3 wrt SPEC_I_PT:
1752 * "If a registration fails for any initiator port (e.g., if th
1753 * logical unit does not have enough resources available to
1754 * hold the registration information), no registrations shall be
1755 * made, and the command shall be terminated with
1756 * CHECK CONDITION status."
1758 * That means we call __core_scsi3_alloc_registration() here,
1759 * and then call __core_scsi3_add_registration() in the
1760 * 2nd loop which will never fail.
1762 dest_lun
= rcu_dereference_check(dest_se_deve
->se_lun
,
1763 atomic_read(&dest_se_deve
->pr_kref
.refcount
) != 0);
1765 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1766 dest_node_acl
, dest_lun
, dest_se_deve
,
1767 dest_se_deve
->mapped_lun
, iport_ptr
,
1768 sa_res_key
, all_tg_pt
, aptpl
);
1770 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1771 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1772 core_scsi3_tpg_undepend_item(dest_tpg
);
1774 ret
= TCM_INVALID_PARAMETER_LIST
;
1777 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1778 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1786 transport_kunmap_data_sg(cmd
);
1789 * Go ahead and create a registrations from tid_dest_list for the
1790 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1793 * The SA Reservation Key from the PROUT is set for the
1794 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1795 * means that the TransportID Initiator port will be
1796 * registered on all of the target ports in the SCSI target device
1797 * ALL_TG_PT=0 means the registration will only be for the
1798 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1801 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1802 dest_tpg
= tidh
->dest_tpg
;
1803 dest_node_acl
= tidh
->dest_node_acl
;
1804 dest_se_deve
= tidh
->dest_se_deve
;
1805 dest_pr_reg
= tidh
->dest_pr_reg
;
1807 list_del(&tidh
->dest_list
);
1810 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1811 core_pr_dump_initiator_port(dest_pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1813 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1816 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1817 " registered Transport ID for Node: %s%s Mapped LUN:"
1818 " %llu\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1819 dest_node_acl
->initiatorname
, i_buf
, (dest_se_deve
) ?
1820 dest_se_deve
->mapped_lun
: 0);
1822 if (!dest_se_deve
) {
1823 kref_put(&local_pr_reg
->pr_reg_deve
->pr_kref
,
1824 target_pr_kref_release
);
1827 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1828 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1829 core_scsi3_tpg_undepend_item(dest_tpg
);
1834 transport_kunmap_data_sg(cmd
);
1837 * For the failure case, release everything from tid_dest_list
1838 * including *dest_pr_reg and the configfs dependances..
1840 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1841 dest_tpg
= tidh
->dest_tpg
;
1842 dest_node_acl
= tidh
->dest_node_acl
;
1843 dest_se_deve
= tidh
->dest_se_deve
;
1844 dest_pr_reg
= tidh
->dest_pr_reg
;
1846 list_del(&tidh
->dest_list
);
1849 * Release any extra ALL_TG_PT=1 registrations for
1850 * the SPEC_I_PT=1 case.
1852 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1853 &dest_pr_reg
->pr_reg_atp_list
,
1854 pr_reg_atp_mem_list
) {
1855 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1856 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1857 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1860 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1862 if (!dest_se_deve
) {
1863 kref_put(&local_pr_reg
->pr_reg_deve
->pr_kref
,
1864 target_pr_kref_release
);
1867 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1868 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1869 core_scsi3_tpg_undepend_item(dest_tpg
);
1874 static int core_scsi3_update_aptpl_buf(
1875 struct se_device
*dev
,
1877 u32 pr_aptpl_buf_len
)
1879 struct se_portal_group
*tpg
;
1880 struct t10_pr_registration
*pr_reg
;
1881 unsigned char tmp
[512], isid_buf
[32];
1886 spin_lock(&dev
->dev_reservation_lock
);
1887 spin_lock(&dev
->t10_pr
.registration_lock
);
1889 * Walk the registration list..
1891 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
1896 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1898 * Write out any ISID value to APTPL metadata that was included
1899 * in the original registration.
1901 if (pr_reg
->isid_present_at_reg
)
1902 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1903 pr_reg
->pr_reg_isid
);
1905 * Include special metadata if the pr_reg matches the
1906 * reservation holder.
1908 if (dev
->dev_pr_res_holder
== pr_reg
) {
1909 snprintf(tmp
, 512, "PR_REG_START: %d"
1910 "\ninitiator_fabric=%s\n"
1911 "initiator_node=%s\n%s"
1913 "res_holder=1\nres_type=%02x\n"
1914 "res_scope=%02x\nres_all_tg_pt=%d\n"
1915 "mapped_lun=%llu\n", reg_count
,
1916 tpg
->se_tpg_tfo
->get_fabric_name(),
1917 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1918 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1919 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1920 pr_reg
->pr_res_mapped_lun
);
1922 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1923 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1924 "sa_res_key=%llu\nres_holder=0\n"
1925 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
1926 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1927 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1928 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1929 pr_reg
->pr_res_mapped_lun
);
1932 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1933 pr_err("Unable to update renaming APTPL metadata,"
1934 " reallocating larger buffer\n");
1938 len
+= sprintf(buf
+len
, "%s", tmp
);
1941 * Include information about the associated SCSI target port.
1943 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1944 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
1945 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1946 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1947 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1948 pr_reg
->tg_pt_sep_rtpi
, pr_reg
->pr_aptpl_target_lun
,
1951 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1952 pr_err("Unable to update renaming APTPL metadata,"
1953 " reallocating larger buffer\n");
1957 len
+= sprintf(buf
+len
, "%s", tmp
);
1962 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1965 spin_unlock(&dev
->t10_pr
.registration_lock
);
1966 spin_unlock(&dev
->dev_reservation_lock
);
1971 static int __core_scsi3_write_aptpl_to_file(
1972 struct se_device
*dev
,
1975 struct t10_wwn
*wwn
= &dev
->t10_wwn
;
1977 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1979 u32 pr_aptpl_buf_len
;
1982 memset(path
, 0, 512);
1984 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
1985 pr_err("WWN value for struct se_device does not fit"
1986 " into path buffer\n");
1990 snprintf(path
, 512, "%s/pr/aptpl_%s", db_root
, &wwn
->unit_serial
[0]);
1991 file
= filp_open(path
, flags
, 0600);
1993 pr_err("filp_open(%s) for APTPL metadata"
1995 return PTR_ERR(file
);
1998 pr_aptpl_buf_len
= (strlen(buf
) + 1); /* Add extra for NULL */
2000 ret
= kernel_write(file
, buf
, pr_aptpl_buf_len
, 0);
2003 pr_debug("Error writing APTPL metadata file: %s\n", path
);
2006 return (ret
< 0) ? -EIO
: 0;
2010 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2011 * write out the updated metadata to struct file for this SCSI device.
2013 static sense_reason_t
core_scsi3_update_and_write_aptpl(struct se_device
*dev
, bool aptpl
)
2016 int rc
, len
= PR_APTPL_BUF_LEN
;
2019 char *null_buf
= "No Registrations or Reservations\n";
2021 rc
= __core_scsi3_write_aptpl_to_file(dev
, null_buf
);
2022 dev
->t10_pr
.pr_aptpl_active
= 0;
2023 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
2026 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2033 return TCM_OUT_OF_RESOURCES
;
2035 rc
= core_scsi3_update_aptpl_buf(dev
, buf
, len
);
2042 rc
= __core_scsi3_write_aptpl_to_file(dev
, buf
);
2044 pr_err("SPC-3 PR: Could not update APTPL\n");
2046 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2048 dev
->t10_pr
.pr_aptpl_active
= 1;
2050 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2054 static sense_reason_t
2055 core_scsi3_emulate_pro_register(struct se_cmd
*cmd
, u64 res_key
, u64 sa_res_key
,
2056 bool aptpl
, bool all_tg_pt
, bool spec_i_pt
, enum register_type register_type
)
2058 struct se_session
*se_sess
= cmd
->se_sess
;
2059 struct se_device
*dev
= cmd
->se_dev
;
2060 struct se_lun
*se_lun
= cmd
->se_lun
;
2061 struct se_portal_group
*se_tpg
;
2062 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
;
2063 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2064 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2065 sense_reason_t ret
= TCM_NO_SENSE
;
2066 int pr_holder
= 0, type
;
2068 if (!se_sess
|| !se_lun
) {
2069 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2070 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2072 se_tpg
= se_sess
->se_tpg
;
2074 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2075 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2076 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2078 isid_ptr
= &isid_buf
[0];
2081 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2083 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2086 pr_warn("SPC-3 PR: Reservation Key non-zero"
2087 " for SA REGISTER, returning CONFLICT\n");
2088 return TCM_RESERVATION_CONFLICT
;
2091 * Do nothing but return GOOD status.
2098 * Perform the Service Action REGISTER on the Initiator
2099 * Port Endpoint that the PRO was received from on the
2100 * Logical Unit of the SCSI device server.
2102 if (core_scsi3_alloc_registration(cmd
->se_dev
,
2103 se_sess
->se_node_acl
, cmd
->se_lun
,
2104 NULL
, cmd
->orig_fe_lun
, isid_ptr
,
2105 sa_res_key
, all_tg_pt
, aptpl
,
2106 register_type
, 0)) {
2107 pr_err("Unable to allocate"
2108 " struct t10_pr_registration\n");
2109 return TCM_INVALID_PARAMETER_LIST
;
2113 * Register both the Initiator port that received
2114 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2115 * TransportID from Parameter list and loop through
2116 * fabric dependent parameter list while calling
2117 * logic from of core_scsi3_alloc_registration() for
2118 * each TransportID provided SCSI Initiator Port/Device
2120 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2121 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2125 return core_scsi3_update_and_write_aptpl(dev
, aptpl
);
2128 /* ok, existing registration */
2130 if ((register_type
== REGISTER
) && (res_key
!= pr_reg
->pr_res_key
)) {
2131 pr_err("SPC-3 PR REGISTER: Received"
2132 " res_key: 0x%016Lx does not match"
2133 " existing SA REGISTER res_key:"
2134 " 0x%016Lx\n", res_key
,
2135 pr_reg
->pr_res_key
);
2136 ret
= TCM_RESERVATION_CONFLICT
;
2141 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2142 " set on a registered nexus\n");
2143 ret
= TCM_INVALID_PARAMETER_LIST
;
2148 * An existing ALL_TG_PT=1 registration being released
2149 * must also set ALL_TG_PT=1 in the incoming PROUT.
2151 if (pr_reg
->pr_reg_all_tg_pt
&& !all_tg_pt
) {
2152 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2153 " registration exists, but ALL_TG_PT=1 bit not"
2154 " present in received PROUT\n");
2155 ret
= TCM_INVALID_CDB_FIELD
;
2160 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2164 * Increment PRgeneration counter for struct se_device"
2165 * upon a successful REGISTER, see spc4r17 section 6.3.2
2166 * READ_KEYS service action.
2168 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(cmd
->se_dev
);
2169 pr_reg
->pr_res_key
= sa_res_key
;
2170 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2171 " Key for %s to: 0x%016Lx PRgeneration:"
2172 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2173 (register_type
== REGISTER_AND_IGNORE_EXISTING_KEY
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2174 pr_reg
->pr_reg_nacl
->initiatorname
,
2175 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2179 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2181 type
= pr_reg
->pr_res_type
;
2182 pr_holder
= core_scsi3_check_implicit_release(cmd
->se_dev
,
2184 if (pr_holder
< 0) {
2185 ret
= TCM_RESERVATION_CONFLICT
;
2189 spin_lock(&pr_tmpl
->registration_lock
);
2191 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2192 * and matching pr_res_key.
2194 if (pr_reg
->pr_reg_all_tg_pt
) {
2195 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2196 &pr_tmpl
->registration_list
,
2199 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2201 if (pr_reg_p
->pr_res_key
!= res_key
)
2203 if (pr_reg
== pr_reg_p
)
2205 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2206 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2209 __core_scsi3_free_registration(dev
,
2215 * Release the calling I_T Nexus registration now..
2217 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
, NULL
, 1);
2221 * From spc4r17, section 5.7.11.3 Unregistering
2223 * If the persistent reservation is a registrants only
2224 * type, the device server shall establish a unit
2225 * attention condition for the initiator port associated
2226 * with every registered I_T nexus except for the I_T
2227 * nexus on which the PERSISTENT RESERVE OUT command was
2228 * received, with the additional sense code set to
2229 * RESERVATIONS RELEASED.
2232 (type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
||
2233 type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
)) {
2234 list_for_each_entry(pr_reg_p
,
2235 &pr_tmpl
->registration_list
,
2238 target_ua_allocate_lun(
2239 pr_reg_p
->pr_reg_nacl
,
2240 pr_reg_p
->pr_res_mapped_lun
,
2242 ASCQ_2AH_RESERVATIONS_RELEASED
);
2246 spin_unlock(&pr_tmpl
->registration_lock
);
2249 ret
= core_scsi3_update_and_write_aptpl(dev
, aptpl
);
2253 core_scsi3_put_pr_reg(pr_reg
);
2257 unsigned char *core_scsi3_pr_dump_type(int type
)
2260 case PR_TYPE_WRITE_EXCLUSIVE
:
2261 return "Write Exclusive Access";
2262 case PR_TYPE_EXCLUSIVE_ACCESS
:
2263 return "Exclusive Access";
2264 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2265 return "Write Exclusive Access, Registrants Only";
2266 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2267 return "Exclusive Access, Registrants Only";
2268 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2269 return "Write Exclusive Access, All Registrants";
2270 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2271 return "Exclusive Access, All Registrants";
2276 return "Unknown SPC-3 PR Type";
2279 static sense_reason_t
2280 core_scsi3_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
)
2282 struct se_device
*dev
= cmd
->se_dev
;
2283 struct se_session
*se_sess
= cmd
->se_sess
;
2284 struct se_lun
*se_lun
= cmd
->se_lun
;
2285 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2286 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2287 char i_buf
[PR_REG_ISID_ID_LEN
];
2290 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2292 if (!se_sess
|| !se_lun
) {
2293 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2294 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2297 * Locate the existing *pr_reg via struct se_node_acl pointers
2299 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2302 pr_err("SPC-3 PR: Unable to locate"
2303 " PR_REGISTERED *pr_reg for RESERVE\n");
2304 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2307 * From spc4r17 Section 5.7.9: Reserving:
2309 * An application client creates a persistent reservation by issuing
2310 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2311 * a registered I_T nexus with the following parameters:
2312 * a) RESERVATION KEY set to the value of the reservation key that is
2313 * registered with the logical unit for the I_T nexus; and
2315 if (res_key
!= pr_reg
->pr_res_key
) {
2316 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2317 " does not match existing SA REGISTER res_key:"
2318 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2319 ret
= TCM_RESERVATION_CONFLICT
;
2320 goto out_put_pr_reg
;
2323 * From spc4r17 Section 5.7.9: Reserving:
2326 * b) TYPE field and SCOPE field set to the persistent reservation
2329 * Only one persistent reservation is allowed at a time per logical unit
2330 * and that persistent reservation has a scope of LU_SCOPE.
2332 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2333 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2334 ret
= TCM_INVALID_PARAMETER_LIST
;
2335 goto out_put_pr_reg
;
2338 * See if we have an existing PR reservation holder pointer at
2339 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2342 spin_lock(&dev
->dev_reservation_lock
);
2343 pr_res_holder
= dev
->dev_pr_res_holder
;
2344 if (pr_res_holder
) {
2346 * From spc4r17 Section 5.7.9: Reserving:
2348 * If the device server receives a PERSISTENT RESERVE OUT
2349 * command from an I_T nexus other than a persistent reservation
2350 * holder (see 5.7.10) that attempts to create a persistent
2351 * reservation when a persistent reservation already exists for
2352 * the logical unit, then the command shall be completed with
2353 * RESERVATION CONFLICT status.
2355 if (!is_reservation_holder(pr_res_holder
, pr_reg
)) {
2356 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2357 pr_err("SPC-3 PR: Attempted RESERVE from"
2358 " [%s]: %s while reservation already held by"
2359 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2360 cmd
->se_tfo
->get_fabric_name(),
2361 se_sess
->se_node_acl
->initiatorname
,
2362 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2363 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2365 spin_unlock(&dev
->dev_reservation_lock
);
2366 ret
= TCM_RESERVATION_CONFLICT
;
2367 goto out_put_pr_reg
;
2370 * From spc4r17 Section 5.7.9: Reserving:
2372 * If a persistent reservation holder attempts to modify the
2373 * type or scope of an existing persistent reservation, the
2374 * command shall be completed with RESERVATION CONFLICT status.
2376 if ((pr_res_holder
->pr_res_type
!= type
) ||
2377 (pr_res_holder
->pr_res_scope
!= scope
)) {
2378 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2379 pr_err("SPC-3 PR: Attempted RESERVE from"
2380 " [%s]: %s trying to change TYPE and/or SCOPE,"
2381 " while reservation already held by [%s]: %s,"
2382 " returning RESERVATION_CONFLICT\n",
2383 cmd
->se_tfo
->get_fabric_name(),
2384 se_sess
->se_node_acl
->initiatorname
,
2385 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2386 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2388 spin_unlock(&dev
->dev_reservation_lock
);
2389 ret
= TCM_RESERVATION_CONFLICT
;
2390 goto out_put_pr_reg
;
2393 * From spc4r17 Section 5.7.9: Reserving:
2395 * If the device server receives a PERSISTENT RESERVE OUT
2396 * command with RESERVE service action where the TYPE field and
2397 * the SCOPE field contain the same values as the existing type
2398 * and scope from a persistent reservation holder, it shall not
2399 * make any change to the existing persistent reservation and
2400 * shall completethe command with GOOD status.
2402 spin_unlock(&dev
->dev_reservation_lock
);
2404 goto out_put_pr_reg
;
2407 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2408 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2410 pr_reg
->pr_res_scope
= scope
;
2411 pr_reg
->pr_res_type
= type
;
2412 pr_reg
->pr_res_holder
= 1;
2413 dev
->dev_pr_res_holder
= pr_reg
;
2414 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2416 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2417 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2418 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2419 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2420 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2421 cmd
->se_tfo
->get_fabric_name(),
2422 se_sess
->se_node_acl
->initiatorname
,
2424 spin_unlock(&dev
->dev_reservation_lock
);
2426 if (pr_tmpl
->pr_aptpl_active
)
2427 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
2431 core_scsi3_put_pr_reg(pr_reg
);
2435 static sense_reason_t
2436 core_scsi3_emulate_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
,
2440 case PR_TYPE_WRITE_EXCLUSIVE
:
2441 case PR_TYPE_EXCLUSIVE_ACCESS
:
2442 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2443 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2444 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2445 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2446 return core_scsi3_pro_reserve(cmd
, type
, scope
, res_key
);
2448 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2450 return TCM_INVALID_CDB_FIELD
;
2455 * Called with struct se_device->dev_reservation_lock held.
2457 static void __core_scsi3_complete_pro_release(
2458 struct se_device
*dev
,
2459 struct se_node_acl
*se_nacl
,
2460 struct t10_pr_registration
*pr_reg
,
2464 const struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2465 char i_buf
[PR_REG_ISID_ID_LEN
];
2466 int pr_res_type
= 0, pr_res_scope
= 0;
2468 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2469 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2471 * Go ahead and release the current PR reservation holder.
2472 * If an All Registrants reservation is currently active and
2473 * a unregister operation is requested, replace the current
2474 * dev_pr_res_holder with another active registration.
2476 if (dev
->dev_pr_res_holder
) {
2477 pr_res_type
= dev
->dev_pr_res_holder
->pr_res_type
;
2478 pr_res_scope
= dev
->dev_pr_res_holder
->pr_res_scope
;
2479 dev
->dev_pr_res_holder
->pr_res_type
= 0;
2480 dev
->dev_pr_res_holder
->pr_res_scope
= 0;
2481 dev
->dev_pr_res_holder
->pr_res_holder
= 0;
2482 dev
->dev_pr_res_holder
= NULL
;
2487 spin_lock(&dev
->t10_pr
.registration_lock
);
2488 list_del_init(&pr_reg
->pr_reg_list
);
2490 * If the I_T nexus is a reservation holder, the persistent reservation
2491 * is of an all registrants type, and the I_T nexus is the last remaining
2492 * registered I_T nexus, then the device server shall also release the
2493 * persistent reservation.
2495 if (!list_empty(&dev
->t10_pr
.registration_list
) &&
2496 ((pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2497 (pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))) {
2498 dev
->dev_pr_res_holder
=
2499 list_entry(dev
->t10_pr
.registration_list
.next
,
2500 struct t10_pr_registration
, pr_reg_list
);
2501 dev
->dev_pr_res_holder
->pr_res_type
= pr_res_type
;
2502 dev
->dev_pr_res_holder
->pr_res_scope
= pr_res_scope
;
2503 dev
->dev_pr_res_holder
->pr_res_holder
= 1;
2505 spin_unlock(&dev
->t10_pr
.registration_lock
);
2507 if (!dev
->dev_pr_res_holder
) {
2508 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2509 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2510 tfo
->get_fabric_name(), (explicit) ? "explicit" :
2511 "implicit", core_scsi3_pr_dump_type(pr_res_type
),
2512 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2514 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2515 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2518 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2520 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2523 static sense_reason_t
2524 core_scsi3_emulate_pro_release(struct se_cmd
*cmd
, int type
, int scope
,
2527 struct se_device
*dev
= cmd
->se_dev
;
2528 struct se_session
*se_sess
= cmd
->se_sess
;
2529 struct se_lun
*se_lun
= cmd
->se_lun
;
2530 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2531 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2532 sense_reason_t ret
= 0;
2534 if (!se_sess
|| !se_lun
) {
2535 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2536 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2539 * Locate the existing *pr_reg via struct se_node_acl pointers
2541 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2543 pr_err("SPC-3 PR: Unable to locate"
2544 " PR_REGISTERED *pr_reg for RELEASE\n");
2545 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2548 * From spc4r17 Section 5.7.11.2 Releasing:
2550 * If there is no persistent reservation or in response to a persistent
2551 * reservation release request from a registered I_T nexus that is not a
2552 * persistent reservation holder (see 5.7.10), the device server shall
2555 * a) Not release the persistent reservation, if any;
2556 * b) Not remove any registrations; and
2557 * c) Complete the command with GOOD status.
2559 spin_lock(&dev
->dev_reservation_lock
);
2560 pr_res_holder
= dev
->dev_pr_res_holder
;
2561 if (!pr_res_holder
) {
2563 * No persistent reservation, return GOOD status.
2565 spin_unlock(&dev
->dev_reservation_lock
);
2566 goto out_put_pr_reg
;
2569 if (!is_reservation_holder(pr_res_holder
, pr_reg
)) {
2571 * Release request from a registered I_T nexus that is not a
2572 * persistent reservation holder. return GOOD status.
2574 spin_unlock(&dev
->dev_reservation_lock
);
2575 goto out_put_pr_reg
;
2579 * From spc4r17 Section 5.7.11.2 Releasing:
2581 * Only the persistent reservation holder (see 5.7.10) is allowed to
2582 * release a persistent reservation.
2584 * An application client releases the persistent reservation by issuing
2585 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2586 * an I_T nexus that is a persistent reservation holder with the
2587 * following parameters:
2589 * a) RESERVATION KEY field set to the value of the reservation key
2590 * that is registered with the logical unit for the I_T nexus;
2592 if (res_key
!= pr_reg
->pr_res_key
) {
2593 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2594 " does not match existing SA REGISTER res_key:"
2595 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2596 spin_unlock(&dev
->dev_reservation_lock
);
2597 ret
= TCM_RESERVATION_CONFLICT
;
2598 goto out_put_pr_reg
;
2601 * From spc4r17 Section 5.7.11.2 Releasing and above:
2603 * b) TYPE field and SCOPE field set to match the persistent
2604 * reservation being released.
2606 if ((pr_res_holder
->pr_res_type
!= type
) ||
2607 (pr_res_holder
->pr_res_scope
!= scope
)) {
2608 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2609 pr_err("SPC-3 PR RELEASE: Attempted to release"
2610 " reservation from [%s]: %s with different TYPE "
2611 "and/or SCOPE while reservation already held by"
2612 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2613 cmd
->se_tfo
->get_fabric_name(),
2614 se_sess
->se_node_acl
->initiatorname
,
2615 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2616 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2618 spin_unlock(&dev
->dev_reservation_lock
);
2619 ret
= TCM_RESERVATION_CONFLICT
;
2620 goto out_put_pr_reg
;
2623 * In response to a persistent reservation release request from the
2624 * persistent reservation holder the device server shall perform a
2625 * release by doing the following as an uninterrupted series of actions:
2626 * a) Release the persistent reservation;
2627 * b) Not remove any registration(s);
2628 * c) If the released persistent reservation is a registrants only type
2629 * or all registrants type persistent reservation,
2630 * the device server shall establish a unit attention condition for
2631 * the initiator port associated with every regis-
2632 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2633 * RESERVE OUT command with RELEASE service action was received,
2634 * with the additional sense code set to RESERVATIONS RELEASED; and
2635 * d) If the persistent reservation is of any other type, the device
2636 * server shall not establish a unit attention condition.
2638 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2641 spin_unlock(&dev
->dev_reservation_lock
);
2643 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2644 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2645 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2646 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2648 * If no UNIT ATTENTION conditions will be established for
2649 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2650 * go ahead and check for APTPL=1 update+write below
2655 spin_lock(&pr_tmpl
->registration_lock
);
2656 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2659 * Do not establish a UNIT ATTENTION condition
2660 * for the calling I_T Nexus
2662 if (pr_reg_p
== pr_reg
)
2665 target_ua_allocate_lun(pr_reg_p
->pr_reg_nacl
,
2666 pr_reg_p
->pr_res_mapped_lun
,
2667 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2669 spin_unlock(&pr_tmpl
->registration_lock
);
2672 if (pr_tmpl
->pr_aptpl_active
)
2673 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
2676 core_scsi3_put_pr_reg(pr_reg
);
2680 static sense_reason_t
2681 core_scsi3_emulate_pro_clear(struct se_cmd
*cmd
, u64 res_key
)
2683 struct se_device
*dev
= cmd
->se_dev
;
2684 struct se_node_acl
*pr_reg_nacl
;
2685 struct se_session
*se_sess
= cmd
->se_sess
;
2686 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2687 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2688 u64 pr_res_mapped_lun
= 0;
2689 int calling_it_nexus
= 0;
2691 * Locate the existing *pr_reg via struct se_node_acl pointers
2693 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2694 se_sess
->se_node_acl
, se_sess
);
2696 pr_err("SPC-3 PR: Unable to locate"
2697 " PR_REGISTERED *pr_reg for CLEAR\n");
2698 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2701 * From spc4r17 section 5.7.11.6, Clearing:
2703 * Any application client may release the persistent reservation and
2704 * remove all registrations from a device server by issuing a
2705 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2706 * registered I_T nexus with the following parameter:
2708 * a) RESERVATION KEY field set to the value of the reservation key
2709 * that is registered with the logical unit for the I_T nexus.
2711 if (res_key
!= pr_reg_n
->pr_res_key
) {
2712 pr_err("SPC-3 PR REGISTER: Received"
2713 " res_key: 0x%016Lx does not match"
2714 " existing SA REGISTER res_key:"
2715 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2716 core_scsi3_put_pr_reg(pr_reg_n
);
2717 return TCM_RESERVATION_CONFLICT
;
2720 * a) Release the persistent reservation, if any;
2722 spin_lock(&dev
->dev_reservation_lock
);
2723 pr_res_holder
= dev
->dev_pr_res_holder
;
2724 if (pr_res_holder
) {
2725 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2726 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2727 pr_res_holder
, 0, 0);
2729 spin_unlock(&dev
->dev_reservation_lock
);
2731 * b) Remove all registration(s) (see spc4r17 5.7.7);
2733 spin_lock(&pr_tmpl
->registration_lock
);
2734 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2735 &pr_tmpl
->registration_list
, pr_reg_list
) {
2737 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2738 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2739 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2740 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2743 * e) Establish a unit attention condition for the initiator
2744 * port associated with every registered I_T nexus other
2745 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2746 * command with CLEAR service action was received, with the
2747 * additional sense code set to RESERVATIONS PREEMPTED.
2749 if (!calling_it_nexus
)
2750 target_ua_allocate_lun(pr_reg_nacl
, pr_res_mapped_lun
,
2751 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2753 spin_unlock(&pr_tmpl
->registration_lock
);
2755 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2756 cmd
->se_tfo
->get_fabric_name());
2758 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, false);
2760 core_scsi3_pr_generation(dev
);
2765 * Called with struct se_device->dev_reservation_lock held.
2767 static void __core_scsi3_complete_pro_preempt(
2768 struct se_device
*dev
,
2769 struct t10_pr_registration
*pr_reg
,
2770 struct list_head
*preempt_and_abort_list
,
2773 enum preempt_type preempt_type
)
2775 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2776 const struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2777 char i_buf
[PR_REG_ISID_ID_LEN
];
2779 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2780 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2782 * Do an implicit RELEASE of the existing reservation.
2784 if (dev
->dev_pr_res_holder
)
2785 __core_scsi3_complete_pro_release(dev
, nacl
,
2786 dev
->dev_pr_res_holder
, 0, 0);
2788 dev
->dev_pr_res_holder
= pr_reg
;
2789 pr_reg
->pr_res_holder
= 1;
2790 pr_reg
->pr_res_type
= type
;
2791 pr_reg
->pr_res_scope
= scope
;
2793 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2794 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2795 tfo
->get_fabric_name(), (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "",
2796 core_scsi3_pr_dump_type(type
),
2797 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2798 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2799 tfo
->get_fabric_name(), (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "",
2800 nacl
->initiatorname
, i_buf
);
2802 * For PREEMPT_AND_ABORT, add the preempting reservation's
2803 * struct t10_pr_registration to the list that will be compared
2804 * against received CDBs..
2806 if (preempt_and_abort_list
)
2807 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2808 preempt_and_abort_list
);
2811 static void core_scsi3_release_preempt_and_abort(
2812 struct list_head
*preempt_and_abort_list
,
2813 struct t10_pr_registration
*pr_reg_holder
)
2815 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2817 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2818 pr_reg_abort_list
) {
2820 list_del(&pr_reg
->pr_reg_abort_list
);
2821 if (pr_reg_holder
== pr_reg
)
2823 if (pr_reg
->pr_res_holder
) {
2824 pr_warn("pr_reg->pr_res_holder still set\n");
2828 pr_reg
->pr_reg_deve
= NULL
;
2829 pr_reg
->pr_reg_nacl
= NULL
;
2830 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2834 static sense_reason_t
2835 core_scsi3_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
,
2836 u64 sa_res_key
, enum preempt_type preempt_type
)
2838 struct se_device
*dev
= cmd
->se_dev
;
2839 struct se_node_acl
*pr_reg_nacl
;
2840 struct se_session
*se_sess
= cmd
->se_sess
;
2841 LIST_HEAD(preempt_and_abort_list
);
2842 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2843 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2844 u64 pr_res_mapped_lun
= 0;
2845 int all_reg
= 0, calling_it_nexus
= 0;
2846 bool sa_res_key_unmatched
= sa_res_key
!= 0;
2847 int prh_type
= 0, prh_scope
= 0;
2850 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2852 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2855 pr_err("SPC-3 PR: Unable to locate"
2856 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2857 (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "");
2858 return TCM_RESERVATION_CONFLICT
;
2860 if (pr_reg_n
->pr_res_key
!= res_key
) {
2861 core_scsi3_put_pr_reg(pr_reg_n
);
2862 return TCM_RESERVATION_CONFLICT
;
2864 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2865 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2866 core_scsi3_put_pr_reg(pr_reg_n
);
2867 return TCM_INVALID_PARAMETER_LIST
;
2870 spin_lock(&dev
->dev_reservation_lock
);
2871 pr_res_holder
= dev
->dev_pr_res_holder
;
2872 if (pr_res_holder
&&
2873 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2874 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
2877 if (!all_reg
&& !sa_res_key
) {
2878 spin_unlock(&dev
->dev_reservation_lock
);
2879 core_scsi3_put_pr_reg(pr_reg_n
);
2880 return TCM_INVALID_PARAMETER_LIST
;
2883 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2885 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2886 * persistent reservation holder or there is no persistent reservation
2887 * holder (i.e., there is no persistent reservation), then the device
2888 * server shall perform a preempt by doing the following in an
2889 * uninterrupted series of actions. (See below..)
2891 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
2893 * No existing or SA Reservation Key matching reservations..
2895 * PROUT SA PREEMPT with All Registrant type reservations are
2896 * allowed to be processed without a matching SA Reservation Key
2898 spin_lock(&pr_tmpl
->registration_lock
);
2899 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2900 &pr_tmpl
->registration_list
, pr_reg_list
) {
2902 * Removing of registrations in non all registrants
2903 * type reservations without a matching SA reservation
2906 * a) Remove the registrations for all I_T nexuses
2907 * specified by the SERVICE ACTION RESERVATION KEY
2909 * b) Ignore the contents of the SCOPE and TYPE fields;
2910 * c) Process tasks as defined in 5.7.1; and
2911 * d) Establish a unit attention condition for the
2912 * initiator port associated with every I_T nexus
2913 * that lost its registration other than the I_T
2914 * nexus on which the PERSISTENT RESERVE OUT command
2915 * was received, with the additional sense code set
2916 * to REGISTRATIONS PREEMPTED.
2919 if (pr_reg
->pr_res_key
!= sa_res_key
)
2921 sa_res_key_unmatched
= false;
2923 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2924 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2925 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2926 __core_scsi3_free_registration(dev
, pr_reg
,
2927 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
:
2928 NULL
, calling_it_nexus
);
2931 * Case for any existing all registrants type
2932 * reservation, follow logic in spc4r17 section
2933 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2935 * For a ZERO SA Reservation key, release
2936 * all other registrations and do an implicit
2937 * release of active persistent reservation.
2939 * For a non-ZERO SA Reservation key, only
2940 * release the matching reservation key from
2944 (pr_reg
->pr_res_key
!= sa_res_key
))
2946 sa_res_key_unmatched
= false;
2948 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2949 if (calling_it_nexus
)
2952 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2953 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2954 __core_scsi3_free_registration(dev
, pr_reg
,
2955 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
:
2958 if (!calling_it_nexus
)
2959 target_ua_allocate_lun(pr_reg_nacl
,
2960 pr_res_mapped_lun
, 0x2A,
2961 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
2963 spin_unlock(&pr_tmpl
->registration_lock
);
2965 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2966 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2967 * RESERVATION KEY field to a value that does not match any
2968 * registered reservation key, then the device server shall
2969 * complete the command with RESERVATION CONFLICT status.
2971 if (sa_res_key_unmatched
) {
2972 spin_unlock(&dev
->dev_reservation_lock
);
2973 core_scsi3_put_pr_reg(pr_reg_n
);
2974 return TCM_RESERVATION_CONFLICT
;
2977 * For an existing all registrants type reservation
2978 * with a zero SA rservation key, preempt the existing
2979 * reservation with the new PR type and scope.
2981 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
2982 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
2983 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
2984 type
, scope
, preempt_type
);
2986 if (preempt_type
== PREEMPT_AND_ABORT
)
2987 core_scsi3_release_preempt_and_abort(
2988 &preempt_and_abort_list
, pr_reg_n
);
2990 spin_unlock(&dev
->dev_reservation_lock
);
2992 if (pr_tmpl
->pr_aptpl_active
)
2993 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
2995 core_scsi3_put_pr_reg(pr_reg_n
);
2996 core_scsi3_pr_generation(cmd
->se_dev
);
3000 * The PREEMPTing SA reservation key matches that of the
3001 * existing persistent reservation, first, we check if
3002 * we are preempting our own reservation.
3003 * From spc4r17, section 5.7.11.4.3 Preempting
3004 * persistent reservations and registration handling
3006 * If an all registrants persistent reservation is not
3007 * present, it is not an error for the persistent
3008 * reservation holder to preempt itself (i.e., a
3009 * PERSISTENT RESERVE OUT with a PREEMPT service action
3010 * or a PREEMPT AND ABORT service action with the
3011 * SERVICE ACTION RESERVATION KEY value equal to the
3012 * persistent reservation holder's reservation key that
3013 * is received from the persistent reservation holder).
3014 * In that case, the device server shall establish the
3015 * new persistent reservation and maintain the
3018 prh_type
= pr_res_holder
->pr_res_type
;
3019 prh_scope
= pr_res_holder
->pr_res_scope
;
3021 * If the SERVICE ACTION RESERVATION KEY field identifies a
3022 * persistent reservation holder (see 5.7.10), the device
3023 * server shall perform a preempt by doing the following as
3024 * an uninterrupted series of actions:
3026 * a) Release the persistent reservation for the holder
3027 * identified by the SERVICE ACTION RESERVATION KEY field;
3029 if (pr_reg_n
!= pr_res_holder
)
3030 __core_scsi3_complete_pro_release(dev
,
3031 pr_res_holder
->pr_reg_nacl
,
3032 dev
->dev_pr_res_holder
, 0, 0);
3034 * b) Remove the registrations for all I_T nexuses identified
3035 * by the SERVICE ACTION RESERVATION KEY field, except the
3036 * I_T nexus that is being used for the PERSISTENT RESERVE
3037 * OUT command. If an all registrants persistent reservation
3038 * is present and the SERVICE ACTION RESERVATION KEY field
3039 * is set to zero, then all registrations shall be removed
3040 * except for that of the I_T nexus that is being used for
3041 * the PERSISTENT RESERVE OUT command;
3043 spin_lock(&pr_tmpl
->registration_lock
);
3044 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3045 &pr_tmpl
->registration_list
, pr_reg_list
) {
3047 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3048 if (calling_it_nexus
)
3051 if (pr_reg
->pr_res_key
!= sa_res_key
)
3054 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3055 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3056 __core_scsi3_free_registration(dev
, pr_reg
,
3057 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
3060 * e) Establish a unit attention condition for the initiator
3061 * port associated with every I_T nexus that lost its
3062 * persistent reservation and/or registration, with the
3063 * additional sense code set to REGISTRATIONS PREEMPTED;
3065 target_ua_allocate_lun(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3066 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3068 spin_unlock(&pr_tmpl
->registration_lock
);
3070 * c) Establish a persistent reservation for the preempting
3071 * I_T nexus using the contents of the SCOPE and TYPE fields;
3073 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3074 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
3075 type
, scope
, preempt_type
);
3077 * d) Process tasks as defined in 5.7.1;
3079 * f) If the type or scope has changed, then for every I_T nexus
3080 * whose reservation key was not removed, except for the I_T
3081 * nexus on which the PERSISTENT RESERVE OUT command was
3082 * received, the device server shall establish a unit
3083 * attention condition for the initiator port associated with
3084 * that I_T nexus, with the additional sense code set to
3085 * RESERVATIONS RELEASED. If the type or scope have not
3086 * changed, then no unit attention condition(s) shall be
3087 * established for this reason.
3089 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3090 spin_lock(&pr_tmpl
->registration_lock
);
3091 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3092 &pr_tmpl
->registration_list
, pr_reg_list
) {
3094 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3095 if (calling_it_nexus
)
3098 target_ua_allocate_lun(pr_reg
->pr_reg_nacl
,
3099 pr_reg
->pr_res_mapped_lun
, 0x2A,
3100 ASCQ_2AH_RESERVATIONS_RELEASED
);
3102 spin_unlock(&pr_tmpl
->registration_lock
);
3104 spin_unlock(&dev
->dev_reservation_lock
);
3106 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3107 * All received CDBs for the matching existing reservation and
3108 * registrations undergo ABORT_TASK logic.
3110 * From there, core_scsi3_release_preempt_and_abort() will
3111 * release every registration in the list (which have already
3112 * been removed from the primary pr_reg list), except the
3113 * new persistent reservation holder, the calling Initiator Port.
3115 if (preempt_type
== PREEMPT_AND_ABORT
) {
3116 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3117 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3121 if (pr_tmpl
->pr_aptpl_active
)
3122 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
3124 core_scsi3_put_pr_reg(pr_reg_n
);
3125 core_scsi3_pr_generation(cmd
->se_dev
);
3129 static sense_reason_t
3130 core_scsi3_emulate_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
,
3131 u64 res_key
, u64 sa_res_key
, enum preempt_type preempt_type
)
3134 case PR_TYPE_WRITE_EXCLUSIVE
:
3135 case PR_TYPE_EXCLUSIVE_ACCESS
:
3136 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3137 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3138 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3139 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3140 return core_scsi3_pro_preempt(cmd
, type
, scope
, res_key
,
3141 sa_res_key
, preempt_type
);
3143 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3144 " Type: 0x%02x\n", (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "", type
);
3145 return TCM_INVALID_CDB_FIELD
;
3150 static sense_reason_t
3151 core_scsi3_emulate_pro_register_and_move(struct se_cmd
*cmd
, u64 res_key
,
3152 u64 sa_res_key
, int aptpl
, int unreg
)
3154 struct se_session
*se_sess
= cmd
->se_sess
;
3155 struct se_device
*dev
= cmd
->se_dev
;
3156 struct se_dev_entry
*dest_se_deve
= NULL
;
3157 struct se_lun
*se_lun
= cmd
->se_lun
, *tmp_lun
;
3158 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3159 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3160 const struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3161 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3162 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3164 const unsigned char *initiator_str
;
3165 char *iport_ptr
= NULL
, i_buf
[PR_REG_ISID_ID_LEN
];
3166 u32 tid_len
, tmp_tid_len
;
3167 int new_reg
= 0, type
, scope
, matching_iname
;
3169 unsigned short rtpi
;
3170 unsigned char proto_ident
;
3172 if (!se_sess
|| !se_lun
) {
3173 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3174 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3177 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3178 se_tpg
= se_sess
->se_tpg
;
3179 tf_ops
= se_tpg
->se_tpg_tfo
;
3181 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3182 * Register behaviors for a REGISTER AND MOVE service action
3184 * Locate the existing *pr_reg via struct se_node_acl pointers
3186 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3189 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3190 " *pr_reg for REGISTER_AND_MOVE\n");
3191 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3194 * The provided reservation key much match the existing reservation key
3195 * provided during this initiator's I_T nexus registration.
3197 if (res_key
!= pr_reg
->pr_res_key
) {
3198 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3199 " res_key: 0x%016Lx does not match existing SA REGISTER"
3200 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3201 ret
= TCM_RESERVATION_CONFLICT
;
3202 goto out_put_pr_reg
;
3205 * The service active reservation key needs to be non zero
3208 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3210 ret
= TCM_INVALID_PARAMETER_LIST
;
3211 goto out_put_pr_reg
;
3215 * Determine the Relative Target Port Identifier where the reservation
3216 * will be moved to for the TransportID containing SCSI initiator WWN
3219 buf
= transport_kmap_data_sg(cmd
);
3221 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3222 goto out_put_pr_reg
;
3225 rtpi
= (buf
[18] & 0xff) << 8;
3226 rtpi
|= buf
[19] & 0xff;
3227 tid_len
= (buf
[20] & 0xff) << 24;
3228 tid_len
|= (buf
[21] & 0xff) << 16;
3229 tid_len
|= (buf
[22] & 0xff) << 8;
3230 tid_len
|= buf
[23] & 0xff;
3231 transport_kunmap_data_sg(cmd
);
3234 if ((tid_len
+ 24) != cmd
->data_length
) {
3235 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3236 " does not equal CDB data_length: %u\n", tid_len
,
3238 ret
= TCM_INVALID_PARAMETER_LIST
;
3239 goto out_put_pr_reg
;
3242 spin_lock(&dev
->se_port_lock
);
3243 list_for_each_entry(tmp_lun
, &dev
->dev_sep_list
, lun_dev_link
) {
3244 if (tmp_lun
->lun_rtpi
!= rtpi
)
3246 dest_se_tpg
= tmp_lun
->lun_tpg
;
3247 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3251 atomic_inc_mb(&dest_se_tpg
->tpg_pr_ref_count
);
3252 spin_unlock(&dev
->se_port_lock
);
3254 if (core_scsi3_tpg_depend_item(dest_se_tpg
)) {
3255 pr_err("core_scsi3_tpg_depend_item() failed"
3256 " for dest_se_tpg\n");
3257 atomic_dec_mb(&dest_se_tpg
->tpg_pr_ref_count
);
3258 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3259 goto out_put_pr_reg
;
3262 spin_lock(&dev
->se_port_lock
);
3265 spin_unlock(&dev
->se_port_lock
);
3267 if (!dest_se_tpg
|| !dest_tf_ops
) {
3268 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3269 " fabric ops from Relative Target Port Identifier:"
3271 ret
= TCM_INVALID_PARAMETER_LIST
;
3272 goto out_put_pr_reg
;
3275 buf
= transport_kmap_data_sg(cmd
);
3277 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3278 goto out_put_pr_reg
;
3280 proto_ident
= (buf
[24] & 0x0f);
3282 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3283 " 0x%02x\n", proto_ident
);
3285 if (proto_ident
!= dest_se_tpg
->proto_id
) {
3286 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3287 " proto_ident: 0x%02x does not match ident: 0x%02x"
3288 " from fabric: %s\n", proto_ident
,
3289 dest_se_tpg
->proto_id
,
3290 dest_tf_ops
->get_fabric_name());
3291 ret
= TCM_INVALID_PARAMETER_LIST
;
3294 initiator_str
= target_parse_pr_out_transport_id(dest_se_tpg
,
3295 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3296 if (!initiator_str
) {
3297 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3298 " initiator_str from Transport ID\n");
3299 ret
= TCM_INVALID_PARAMETER_LIST
;
3303 transport_kunmap_data_sg(cmd
);
3306 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3307 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3308 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3311 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3312 * action specifies a TransportID that is the same as the initiator port
3313 * of the I_T nexus for the command received, then the command shall
3314 * be terminated with CHECK CONDITION status, with the sense key set to
3315 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3316 * IN PARAMETER LIST.
3318 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3319 matching_iname
= (!strcmp(initiator_str
,
3320 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3321 if (!matching_iname
)
3322 goto after_iport_check
;
3324 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3325 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3326 " matches: %s on received I_T Nexus\n", initiator_str
,
3327 pr_reg_nacl
->initiatorname
);
3328 ret
= TCM_INVALID_PARAMETER_LIST
;
3331 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3332 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3333 " matches: %s %s on received I_T Nexus\n",
3334 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3335 pr_reg
->pr_reg_isid
);
3336 ret
= TCM_INVALID_PARAMETER_LIST
;
3341 * Locate the destination struct se_node_acl from the received Transport ID
3343 mutex_lock(&dest_se_tpg
->acl_node_mutex
);
3344 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3347 atomic_inc_mb(&dest_node_acl
->acl_pr_ref_count
);
3348 mutex_unlock(&dest_se_tpg
->acl_node_mutex
);
3350 if (!dest_node_acl
) {
3351 pr_err("Unable to locate %s dest_node_acl for"
3352 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3354 ret
= TCM_INVALID_PARAMETER_LIST
;
3358 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
3359 pr_err("core_scsi3_nodeacl_depend_item() for"
3360 " dest_node_acl\n");
3361 atomic_dec_mb(&dest_node_acl
->acl_pr_ref_count
);
3362 dest_node_acl
= NULL
;
3363 ret
= TCM_INVALID_PARAMETER_LIST
;
3367 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3368 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3369 dest_node_acl
->initiatorname
);
3372 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3375 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3376 if (!dest_se_deve
) {
3377 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3378 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3379 ret
= TCM_INVALID_PARAMETER_LIST
;
3383 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
3384 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3385 kref_put(&dest_se_deve
->pr_kref
, target_pr_kref_release
);
3386 dest_se_deve
= NULL
;
3387 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3391 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3392 " ACL for dest_se_deve->mapped_lun: %llu\n",
3393 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3394 dest_se_deve
->mapped_lun
);
3397 * A persistent reservation needs to already existing in order to
3398 * successfully complete the REGISTER_AND_MOVE service action..
3400 spin_lock(&dev
->dev_reservation_lock
);
3401 pr_res_holder
= dev
->dev_pr_res_holder
;
3402 if (!pr_res_holder
) {
3403 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3404 " currently held\n");
3405 spin_unlock(&dev
->dev_reservation_lock
);
3406 ret
= TCM_INVALID_CDB_FIELD
;
3410 * The received on I_T Nexus must be the reservation holder.
3412 * From spc4r17 section 5.7.8 Table 50 --
3413 * Register behaviors for a REGISTER AND MOVE service action
3415 if (!is_reservation_holder(pr_res_holder
, pr_reg
)) {
3416 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3417 " Nexus is not reservation holder\n");
3418 spin_unlock(&dev
->dev_reservation_lock
);
3419 ret
= TCM_RESERVATION_CONFLICT
;
3423 * From spc4r17 section 5.7.8: registering and moving reservation
3425 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3426 * action is received and the established persistent reservation is a
3427 * Write Exclusive - All Registrants type or Exclusive Access -
3428 * All Registrants type reservation, then the command shall be completed
3429 * with RESERVATION CONFLICT status.
3431 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3432 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3433 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3434 " reservation for type: %s\n",
3435 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3436 spin_unlock(&dev
->dev_reservation_lock
);
3437 ret
= TCM_RESERVATION_CONFLICT
;
3440 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3442 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3444 type
= pr_res_holder
->pr_res_type
;
3445 scope
= pr_res_holder
->pr_res_type
;
3447 * c) Associate the reservation key specified in the SERVICE ACTION
3448 * RESERVATION KEY field with the I_T nexus specified as the
3449 * destination of the register and move, where:
3450 * A) The I_T nexus is specified by the TransportID and the
3451 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3452 * B) Regardless of the TransportID format used, the association for
3453 * the initiator port is based on either the initiator port name
3454 * (see 3.1.71) on SCSI transport protocols where port names are
3455 * required or the initiator port identifier (see 3.1.70) on SCSI
3456 * transport protocols where port names are not required;
3457 * d) Register the reservation key specified in the SERVICE ACTION
3458 * RESERVATION KEY field;
3459 * e) Retain the reservation key specified in the SERVICE ACTION
3460 * RESERVATION KEY field and associated information;
3462 * Also, It is not an error for a REGISTER AND MOVE service action to
3463 * register an I_T nexus that is already registered with the same
3464 * reservation key or a different reservation key.
3466 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3469 struct se_lun
*dest_lun
= rcu_dereference_check(dest_se_deve
->se_lun
,
3470 atomic_read(&dest_se_deve
->pr_kref
.refcount
) != 0);
3472 spin_unlock(&dev
->dev_reservation_lock
);
3473 if (core_scsi3_alloc_registration(cmd
->se_dev
, dest_node_acl
,
3474 dest_lun
, dest_se_deve
, dest_se_deve
->mapped_lun
,
3475 iport_ptr
, sa_res_key
, 0, aptpl
, 2, 1)) {
3476 ret
= TCM_INVALID_PARAMETER_LIST
;
3479 spin_lock(&dev
->dev_reservation_lock
);
3480 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3485 * f) Release the persistent reservation for the persistent reservation
3486 * holder (i.e., the I_T nexus on which the
3488 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3489 dev
->dev_pr_res_holder
, 0, 0);
3491 * g) Move the persistent reservation to the specified I_T nexus using
3492 * the same scope and type as the persistent reservation released in
3495 dev
->dev_pr_res_holder
= dest_pr_reg
;
3496 dest_pr_reg
->pr_res_holder
= 1;
3497 dest_pr_reg
->pr_res_type
= type
;
3498 pr_reg
->pr_res_scope
= scope
;
3499 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
3501 * Increment PRGeneration for existing registrations..
3504 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3505 spin_unlock(&dev
->dev_reservation_lock
);
3507 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3508 " created new reservation holder TYPE: %s on object RTPI:"
3509 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3510 core_scsi3_pr_dump_type(type
), rtpi
,
3511 dest_pr_reg
->pr_res_generation
);
3512 pr_debug("SPC-3 PR Successfully moved reservation from"
3513 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3514 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3515 i_buf
, dest_tf_ops
->get_fabric_name(),
3516 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3519 * It is now safe to release configfs group dependencies for destination
3520 * of Transport ID Initiator Device/Port Identifier
3522 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3523 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3524 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3526 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3527 * nexus on which PERSISTENT RESERVE OUT command was received.
3530 spin_lock(&pr_tmpl
->registration_lock
);
3531 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3532 spin_unlock(&pr_tmpl
->registration_lock
);
3534 core_scsi3_put_pr_reg(pr_reg
);
3536 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, aptpl
);
3538 transport_kunmap_data_sg(cmd
);
3540 core_scsi3_put_pr_reg(dest_pr_reg
);
3544 transport_kunmap_data_sg(cmd
);
3546 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3548 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3549 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3552 core_scsi3_put_pr_reg(pr_reg
);
3556 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3558 unsigned int __v1
, __v2
;
3560 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3561 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3563 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3567 * See spc4r17 section 6.14 Table 170
3570 target_scsi3_emulate_pr_out(struct se_cmd
*cmd
)
3572 struct se_device
*dev
= cmd
->se_dev
;
3573 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3575 u64 res_key
, sa_res_key
;
3576 int sa
, scope
, type
, aptpl
;
3577 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3581 * Following spc2r20 5.5.1 Reservations overview:
3583 * If a logical unit has been reserved by any RESERVE command and is
3584 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3585 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3586 * initiator or service action and shall terminate with a RESERVATION
3589 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
3590 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3591 " SPC-2 reservation is held, returning"
3592 " RESERVATION_CONFLICT\n");
3593 return TCM_RESERVATION_CONFLICT
;
3597 * FIXME: A NULL struct se_session pointer means an this is not coming from
3598 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3601 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3603 if (cmd
->data_length
< 24) {
3604 pr_warn("SPC-PR: Received PR OUT parameter list"
3605 " length too small: %u\n", cmd
->data_length
);
3606 return TCM_INVALID_PARAMETER_LIST
;
3610 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3612 sa
= (cdb
[1] & 0x1f);
3613 scope
= (cdb
[2] & 0xf0);
3614 type
= (cdb
[2] & 0x0f);
3616 buf
= transport_kmap_data_sg(cmd
);
3618 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3621 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3623 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3624 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3626 * REGISTER_AND_MOVE uses a different SA parameter list containing
3627 * SCSI TransportIDs.
3629 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3630 spec_i_pt
= (buf
[20] & 0x08);
3631 all_tg_pt
= (buf
[20] & 0x04);
3632 aptpl
= (buf
[20] & 0x01);
3634 aptpl
= (buf
[17] & 0x01);
3635 unreg
= (buf
[17] & 0x02);
3638 * If the backend device has been configured to force APTPL metadata
3639 * write-out, go ahead and propigate aptpl=1 down now.
3641 if (dev
->dev_attrib
.force_pr_aptpl
)
3644 transport_kunmap_data_sg(cmd
);
3648 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3650 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
))
3651 return TCM_INVALID_PARAMETER_LIST
;
3654 * From spc4r17 section 6.14:
3656 * If the SPEC_I_PT bit is set to zero, the service action is not
3657 * REGISTER AND MOVE, and the parameter list length is not 24, then
3658 * the command shall be terminated with CHECK CONDITION status, with
3659 * the sense key set to ILLEGAL REQUEST, and the additional sense
3660 * code set to PARAMETER LIST LENGTH ERROR.
3662 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3663 (cmd
->data_length
!= 24)) {
3664 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3665 " list length: %u\n", cmd
->data_length
);
3666 return TCM_INVALID_PARAMETER_LIST
;
3670 * (core_scsi3_emulate_pro_* function parameters
3671 * are defined by spc4r17 Table 174:
3672 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3676 ret
= core_scsi3_emulate_pro_register(cmd
,
3677 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, REGISTER
);
3680 ret
= core_scsi3_emulate_pro_reserve(cmd
, type
, scope
, res_key
);
3683 ret
= core_scsi3_emulate_pro_release(cmd
, type
, scope
, res_key
);
3686 ret
= core_scsi3_emulate_pro_clear(cmd
, res_key
);
3689 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3690 res_key
, sa_res_key
, PREEMPT
);
3692 case PRO_PREEMPT_AND_ABORT
:
3693 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3694 res_key
, sa_res_key
, PREEMPT_AND_ABORT
);
3696 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3697 ret
= core_scsi3_emulate_pro_register(cmd
,
3698 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, REGISTER_AND_IGNORE_EXISTING_KEY
);
3700 case PRO_REGISTER_AND_MOVE
:
3701 ret
= core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3702 sa_res_key
, aptpl
, unreg
);
3705 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3706 " action: 0x%02x\n", cdb
[1] & 0x1f);
3707 return TCM_INVALID_CDB_FIELD
;
3711 target_complete_cmd(cmd
, GOOD
);
3716 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3718 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3720 static sense_reason_t
3721 core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3723 struct se_device
*dev
= cmd
->se_dev
;
3724 struct t10_pr_registration
*pr_reg
;
3726 u32 add_len
= 0, off
= 8;
3728 if (cmd
->data_length
< 8) {
3729 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3730 " too small\n", cmd
->data_length
);
3731 return TCM_INVALID_CDB_FIELD
;
3734 buf
= transport_kmap_data_sg(cmd
);
3736 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3738 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3739 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3740 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3741 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3743 spin_lock(&dev
->t10_pr
.registration_lock
);
3744 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
3747 * Check for overflow of 8byte PRI READ_KEYS payload and
3748 * next reservation key list descriptor.
3750 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3753 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3754 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3755 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3756 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3757 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3758 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3759 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3760 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3764 spin_unlock(&dev
->t10_pr
.registration_lock
);
3766 buf
[4] = ((add_len
>> 24) & 0xff);
3767 buf
[5] = ((add_len
>> 16) & 0xff);
3768 buf
[6] = ((add_len
>> 8) & 0xff);
3769 buf
[7] = (add_len
& 0xff);
3771 transport_kunmap_data_sg(cmd
);
3777 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3779 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3781 static sense_reason_t
3782 core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
3784 struct se_device
*dev
= cmd
->se_dev
;
3785 struct t10_pr_registration
*pr_reg
;
3788 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
3790 if (cmd
->data_length
< 8) {
3791 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3792 " too small\n", cmd
->data_length
);
3793 return TCM_INVALID_CDB_FIELD
;
3796 buf
= transport_kmap_data_sg(cmd
);
3798 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3800 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3801 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3802 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3803 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3805 spin_lock(&dev
->dev_reservation_lock
);
3806 pr_reg
= dev
->dev_pr_res_holder
;
3809 * Set the hardcoded Additional Length
3811 buf
[4] = ((add_len
>> 24) & 0xff);
3812 buf
[5] = ((add_len
>> 16) & 0xff);
3813 buf
[6] = ((add_len
>> 8) & 0xff);
3814 buf
[7] = (add_len
& 0xff);
3816 if (cmd
->data_length
< 22)
3820 * Set the Reservation key.
3822 * From spc4r17, section 5.7.10:
3823 * A persistent reservation holder has its reservation key
3824 * returned in the parameter data from a PERSISTENT
3825 * RESERVE IN command with READ RESERVATION service action as
3827 * a) For a persistent reservation of the type Write Exclusive
3828 * - All Registrants or Exclusive Access  All Regitrants,
3829 * the reservation key shall be set to zero; or
3830 * b) For all other persistent reservation types, the
3831 * reservation key shall be set to the registered
3832 * reservation key for the I_T nexus that holds the
3833 * persistent reservation.
3835 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3836 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
3839 pr_res_key
= pr_reg
->pr_res_key
;
3841 buf
[8] = ((pr_res_key
>> 56) & 0xff);
3842 buf
[9] = ((pr_res_key
>> 48) & 0xff);
3843 buf
[10] = ((pr_res_key
>> 40) & 0xff);
3844 buf
[11] = ((pr_res_key
>> 32) & 0xff);
3845 buf
[12] = ((pr_res_key
>> 24) & 0xff);
3846 buf
[13] = ((pr_res_key
>> 16) & 0xff);
3847 buf
[14] = ((pr_res_key
>> 8) & 0xff);
3848 buf
[15] = (pr_res_key
& 0xff);
3850 * Set the SCOPE and TYPE
3852 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
3853 (pr_reg
->pr_res_type
& 0x0f);
3857 spin_unlock(&dev
->dev_reservation_lock
);
3858 transport_kunmap_data_sg(cmd
);
3864 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3866 * See spc4r17 section 6.13.4 Table 165
3868 static sense_reason_t
3869 core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
3871 struct se_device
*dev
= cmd
->se_dev
;
3872 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3874 u16 add_len
= 8; /* Hardcoded to 8. */
3876 if (cmd
->data_length
< 6) {
3877 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3878 " %u too small\n", cmd
->data_length
);
3879 return TCM_INVALID_CDB_FIELD
;
3882 buf
= transport_kmap_data_sg(cmd
);
3884 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3886 buf
[0] = ((add_len
>> 8) & 0xff);
3887 buf
[1] = (add_len
& 0xff);
3888 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3889 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3890 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3891 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3893 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3894 * set the TMV: Task Mask Valid bit.
3898 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3900 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3902 * PTPL_A: Persistence across Target Power Loss Active bit
3904 if (pr_tmpl
->pr_aptpl_active
)
3907 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3909 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3910 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3911 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3912 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3913 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3914 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3916 transport_kunmap_data_sg(cmd
);
3922 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3924 * See spc4r17 section 6.13.5 Table 168 and 169
3926 static sense_reason_t
3927 core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
3929 struct se_device
*dev
= cmd
->se_dev
;
3930 struct se_node_acl
*se_nacl
;
3931 struct se_portal_group
*se_tpg
;
3932 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
3933 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3935 u32 add_desc_len
= 0, add_len
= 0;
3936 u32 off
= 8; /* off into first Full Status descriptor */
3937 int format_code
= 0, pr_res_type
= 0, pr_res_scope
= 0;
3938 int exp_desc_len
, desc_len
;
3939 bool all_reg
= false;
3941 if (cmd
->data_length
< 8) {
3942 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3943 " too small\n", cmd
->data_length
);
3944 return TCM_INVALID_CDB_FIELD
;
3947 buf
= transport_kmap_data_sg(cmd
);
3949 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3951 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3952 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3953 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3954 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3956 spin_lock(&dev
->dev_reservation_lock
);
3957 if (dev
->dev_pr_res_holder
) {
3958 struct t10_pr_registration
*pr_holder
= dev
->dev_pr_res_holder
;
3960 if (pr_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
||
3961 pr_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
) {
3963 pr_res_type
= pr_holder
->pr_res_type
;
3964 pr_res_scope
= pr_holder
->pr_res_scope
;
3967 spin_unlock(&dev
->dev_reservation_lock
);
3969 spin_lock(&pr_tmpl
->registration_lock
);
3970 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3971 &pr_tmpl
->registration_list
, pr_reg_list
) {
3973 se_nacl
= pr_reg
->pr_reg_nacl
;
3974 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
3977 atomic_inc_mb(&pr_reg
->pr_res_holders
);
3978 spin_unlock(&pr_tmpl
->registration_lock
);
3980 * Determine expected length of $FABRIC_MOD specific
3981 * TransportID full status descriptor..
3983 exp_desc_len
= target_get_pr_transport_id_len(se_nacl
, pr_reg
,
3985 if (exp_desc_len
< 0 ||
3986 exp_desc_len
+ add_len
> cmd
->data_length
) {
3987 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3988 " out of buffer: %d\n", cmd
->data_length
);
3989 spin_lock(&pr_tmpl
->registration_lock
);
3990 atomic_dec_mb(&pr_reg
->pr_res_holders
);
3994 * Set RESERVATION KEY
3996 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3997 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3998 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3999 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4000 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4001 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4002 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4003 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4004 off
+= 4; /* Skip Over Reserved area */
4007 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4009 if (pr_reg
->pr_reg_all_tg_pt
)
4012 * The struct se_lun pointer will be present for the
4013 * reservation holder for PR_HOLDER bit.
4015 * Also, if this registration is the reservation
4016 * holder or there is an All Registrants reservation
4017 * active, fill in SCOPE and TYPE in the next byte.
4019 if (pr_reg
->pr_res_holder
) {
4021 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4022 (pr_reg
->pr_res_type
& 0x0f);
4023 } else if (all_reg
) {
4025 buf
[off
++] = (pr_res_scope
& 0xf0) |
4026 (pr_res_type
& 0x0f);
4031 off
+= 4; /* Skip over reserved area */
4033 * From spc4r17 6.3.15:
4035 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4036 * IDENTIFIER field contains the relative port identifier (see
4037 * 3.1.120) of the target port that is part of the I_T nexus
4038 * described by this full status descriptor. If the ALL_TG_PT
4039 * bit is set to one, the contents of the RELATIVE TARGET PORT
4040 * IDENTIFIER field are not defined by this standard.
4042 if (!pr_reg
->pr_reg_all_tg_pt
) {
4043 u16 sep_rtpi
= pr_reg
->tg_pt_sep_rtpi
;
4045 buf
[off
++] = ((sep_rtpi
>> 8) & 0xff);
4046 buf
[off
++] = (sep_rtpi
& 0xff);
4048 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4050 buf
[off
+4] = se_tpg
->proto_id
;
4053 * Now, have the $FABRIC_MOD fill in the transport ID.
4055 desc_len
= target_get_pr_transport_id(se_nacl
, pr_reg
,
4056 &format_code
, &buf
[off
+4]);
4058 spin_lock(&pr_tmpl
->registration_lock
);
4059 atomic_dec_mb(&pr_reg
->pr_res_holders
);
4064 * Set the ADDITIONAL DESCRIPTOR LENGTH
4066 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4067 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4068 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4069 buf
[off
++] = (desc_len
& 0xff);
4071 * Size of full desctipor header minus TransportID
4072 * containing $FABRIC_MOD specific) initiator device/port
4075 * See spc4r17 Section 6.13.5 Table 169
4077 add_desc_len
= (24 + desc_len
);
4080 add_len
+= add_desc_len
;
4082 spin_unlock(&pr_tmpl
->registration_lock
);
4084 * Set ADDITIONAL_LENGTH
4086 buf
[4] = ((add_len
>> 24) & 0xff);
4087 buf
[5] = ((add_len
>> 16) & 0xff);
4088 buf
[6] = ((add_len
>> 8) & 0xff);
4089 buf
[7] = (add_len
& 0xff);
4091 transport_kunmap_data_sg(cmd
);
4097 target_scsi3_emulate_pr_in(struct se_cmd
*cmd
)
4102 * Following spc2r20 5.5.1 Reservations overview:
4104 * If a logical unit has been reserved by any RESERVE command and is
4105 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4106 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4107 * initiator or service action and shall terminate with a RESERVATION
4110 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
4111 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4112 " SPC-2 reservation is held, returning"
4113 " RESERVATION_CONFLICT\n");
4114 return TCM_RESERVATION_CONFLICT
;
4117 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4119 ret
= core_scsi3_pri_read_keys(cmd
);
4121 case PRI_READ_RESERVATION
:
4122 ret
= core_scsi3_pri_read_reservation(cmd
);
4124 case PRI_REPORT_CAPABILITIES
:
4125 ret
= core_scsi3_pri_report_capabilities(cmd
);
4127 case PRI_READ_FULL_STATUS
:
4128 ret
= core_scsi3_pri_read_full_status(cmd
);
4131 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4132 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4133 return TCM_INVALID_CDB_FIELD
;
4137 target_complete_cmd(cmd
, GOOD
);
4142 target_check_reservation(struct se_cmd
*cmd
)
4144 struct se_device
*dev
= cmd
->se_dev
;
4149 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
4151 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
4154 spin_lock(&dev
->dev_reservation_lock
);
4155 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
4156 ret
= target_scsi2_reservation_check(cmd
);
4158 ret
= target_scsi3_pr_reservation_check(cmd
);
4159 spin_unlock(&dev
->dev_reservation_lock
);