1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * (c) Copyright 2009-2012 RisingTide Systems LLC.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <asm/unaligned.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_backend.h>
36 #include <target/target_core_fabric.h>
37 #include <target/target_core_configfs.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
{
48 struct t10_pr_registration
*dest_pr_reg
;
49 struct se_portal_group
*dest_tpg
;
50 struct se_node_acl
*dest_node_acl
;
51 struct se_dev_entry
*dest_se_deve
;
52 struct list_head dest_list
;
55 int core_pr_dump_initiator_port(
56 struct t10_pr_registration
*pr_reg
,
60 if (!pr_reg
->isid_present_at_reg
)
63 snprintf(buf
, size
, ",i,0x%s", &pr_reg
->pr_reg_isid
[0]);
67 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
68 struct t10_pr_registration
*, int);
71 target_scsi2_reservation_check(struct se_cmd
*cmd
)
73 struct se_device
*dev
= cmd
->se_dev
;
74 struct se_session
*sess
= cmd
->se_sess
;
76 switch (cmd
->t_task_cdb
[0]) {
85 if (!dev
->dev_reserved_node_acl
|| !sess
)
88 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
89 return TCM_RESERVATION_CONFLICT
;
91 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
92 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
93 return TCM_RESERVATION_CONFLICT
;
99 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
100 struct se_node_acl
*, struct se_session
*);
101 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
103 static int target_check_scsi2_reservation_conflict(struct se_cmd
*cmd
)
105 struct se_session
*se_sess
= cmd
->se_sess
;
106 struct se_device
*dev
= cmd
->se_dev
;
107 struct t10_pr_registration
*pr_reg
;
108 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
111 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
115 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
118 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
119 * status, but no reservation shall be established and the
120 * persistent reservation shall not be changed, if the command
121 * is received from a) and b) below.
123 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
124 * status, but the persistent reservation shall not be released,
125 * if the command is received from a) and b)
127 * a) An I_T nexus that is a persistent reservation holder; or
128 * b) An I_T nexus that is registered if a registrants only or
129 * all registrants type persistent reservation is present.
131 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
132 * RELEASE(6) command, or RELEASE(10) command shall be processed
133 * as defined in SPC-2.
135 if (pr_reg
->pr_res_holder
) {
136 core_scsi3_put_pr_reg(pr_reg
);
139 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
140 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
141 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
142 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
143 core_scsi3_put_pr_reg(pr_reg
);
146 core_scsi3_put_pr_reg(pr_reg
);
150 * Following spc2r20 5.5.1 Reservations overview:
152 * If a logical unit has executed a PERSISTENT RESERVE OUT
153 * command with the REGISTER or the REGISTER AND IGNORE
154 * EXISTING KEY service action and is still registered by any
155 * initiator, all RESERVE commands and all RELEASE commands
156 * regardless of initiator shall conflict and shall terminate
157 * with a RESERVATION CONFLICT status.
159 spin_lock(&pr_tmpl
->registration_lock
);
160 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
161 spin_unlock(&pr_tmpl
->registration_lock
);
165 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
166 " while active SPC-3 registrations exist,"
167 " returning RESERVATION_CONFLICT\n");
175 target_scsi2_reservation_release(struct se_cmd
*cmd
)
177 struct se_device
*dev
= cmd
->se_dev
;
178 struct se_session
*sess
= cmd
->se_sess
;
179 struct se_portal_group
*tpg
;
182 if (!sess
|| !sess
->se_tpg
)
184 rc
= target_check_scsi2_reservation_conflict(cmd
);
188 return TCM_RESERVATION_CONFLICT
;
190 spin_lock(&dev
->dev_reservation_lock
);
191 if (!dev
->dev_reserved_node_acl
|| !sess
)
194 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
197 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
200 dev
->dev_reserved_node_acl
= NULL
;
201 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS
;
202 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
203 dev
->dev_res_bin_isid
= 0;
204 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS_WITH_ISID
;
207 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
208 " MAPPED LUN: %u for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
209 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
210 sess
->se_node_acl
->initiatorname
);
213 spin_unlock(&dev
->dev_reservation_lock
);
215 target_complete_cmd(cmd
, GOOD
);
220 target_scsi2_reservation_reserve(struct se_cmd
*cmd
)
222 struct se_device
*dev
= cmd
->se_dev
;
223 struct se_session
*sess
= cmd
->se_sess
;
224 struct se_portal_group
*tpg
;
225 sense_reason_t ret
= 0;
228 if ((cmd
->t_task_cdb
[1] & 0x01) &&
229 (cmd
->t_task_cdb
[1] & 0x02)) {
230 pr_err("LongIO and Obselete Bits set, returning"
231 " ILLEGAL_REQUEST\n");
232 return TCM_UNSUPPORTED_SCSI_OPCODE
;
235 * This is currently the case for target_core_mod passthrough struct se_cmd
238 if (!sess
|| !sess
->se_tpg
)
240 rc
= target_check_scsi2_reservation_conflict(cmd
);
245 return TCM_RESERVATION_CONFLICT
;
248 spin_lock(&dev
->dev_reservation_lock
);
249 if (dev
->dev_reserved_node_acl
&&
250 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
251 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
252 tpg
->se_tpg_tfo
->get_fabric_name());
253 pr_err("Original reserver LUN: %u %s\n",
254 cmd
->se_lun
->unpacked_lun
,
255 dev
->dev_reserved_node_acl
->initiatorname
);
256 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
257 " from %s \n", cmd
->se_lun
->unpacked_lun
,
258 cmd
->se_deve
->mapped_lun
,
259 sess
->se_node_acl
->initiatorname
);
260 ret
= TCM_RESERVATION_CONFLICT
;
264 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
265 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS
;
266 if (sess
->sess_bin_isid
!= 0) {
267 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
268 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS_WITH_ISID
;
270 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
271 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
272 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
273 sess
->se_node_acl
->initiatorname
);
276 spin_unlock(&dev
->dev_reservation_lock
);
279 target_complete_cmd(cmd
, GOOD
);
285 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
287 * This function is called by those initiator ports who are *NOT*
288 * the active PR reservation holder when a reservation is present.
290 static int core_scsi3_pr_seq_non_holder(
294 unsigned char *cdb
= cmd
->t_task_cdb
;
295 struct se_dev_entry
*se_deve
;
296 struct se_session
*se_sess
= cmd
->se_sess
;
297 int other_cdb
= 0, ignore_reg
;
298 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
299 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
300 int we
= 0; /* Write Exclusive */
301 int legacy
= 0; /* Act like a legacy device and return
302 * RESERVATION CONFLICT on some CDBs */
304 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
306 * Determine if the registration should be ignored due to
307 * non-matching ISIDs in target_scsi3_pr_reservation_check().
309 ignore_reg
= (pr_reg_type
& 0x80000000);
311 pr_reg_type
&= ~0x80000000;
313 switch (pr_reg_type
) {
314 case PR_TYPE_WRITE_EXCLUSIVE
:
316 case PR_TYPE_EXCLUSIVE_ACCESS
:
318 * Some commands are only allowed for the persistent reservation
321 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
322 registered_nexus
= 1;
324 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
326 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
328 * Some commands are only allowed for registered I_T Nexuses.
331 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
332 registered_nexus
= 1;
334 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
336 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
338 * Each registered I_T Nexus is a reservation holder.
341 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
342 registered_nexus
= 1;
348 * Referenced from spc4r17 table 45 for *NON* PR holder access
351 case SECURITY_PROTOCOL_IN
:
352 if (registered_nexus
)
360 case RECEIVE_DIAGNOSTIC
:
365 if (registered_nexus
) {
369 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
371 case PERSISTENT_RESERVE_OUT
:
373 * This follows PERSISTENT_RESERVE_OUT service actions that
374 * are allowed in the presence of various reservations.
375 * See spc4r17, table 46
377 switch (cdb
[1] & 0x1f) {
380 case PRO_PREEMPT_AND_ABORT
:
381 ret
= (registered_nexus
) ? 0 : 1;
384 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
387 case PRO_REGISTER_AND_MOVE
:
392 ret
= (registered_nexus
) ? 0 : 1;
395 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
396 " action: 0x%02x\n", cdb
[1] & 0x1f);
402 /* Handled by CRH=1 in target_scsi2_reservation_release() */
407 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
410 case TEST_UNIT_READY
:
411 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
414 switch (cdb
[1] & 0x1f) {
415 case MI_MANAGEMENT_PROTOCOL_IN
:
416 if (registered_nexus
) {
420 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
422 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
423 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
428 if (registered_nexus
) {
432 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
434 case MI_REPORT_ALIASES
:
435 case MI_REPORT_IDENTIFYING_INFORMATION
:
436 case MI_REPORT_PRIORITY
:
437 case MI_REPORT_TARGET_PGS
:
438 case MI_REPORT_TIMESTAMP
:
439 ret
= 0; /* Allowed */
442 pr_err("Unknown MI Service Action: 0x%02x\n",
447 case ACCESS_CONTROL_IN
:
448 case ACCESS_CONTROL_OUT
:
451 case READ_MEDIA_SERIAL_NUMBER
:
454 case PERSISTENT_RESERVE_IN
:
455 ret
= 0; /*/ Allowed CDBs */
462 * Case where the CDB is explicitly allowed in the above switch
465 if (!ret
&& !other_cdb
) {
466 pr_debug("Allowing explict CDB: 0x%02x for %s"
467 " reservation holder\n", cdb
[0],
468 core_scsi3_pr_dump_type(pr_reg_type
));
473 * Check if write exclusive initiator ports *NOT* holding the
474 * WRITE_EXCLUSIVE_* reservation.
476 if (we
&& !registered_nexus
) {
477 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
479 * Conflict for write exclusive
481 pr_debug("%s Conflict for unregistered nexus"
482 " %s CDB: 0x%02x to %s reservation\n",
483 transport_dump_cmd_direction(cmd
),
484 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
485 core_scsi3_pr_dump_type(pr_reg_type
));
489 * Allow non WRITE CDBs for all Write Exclusive
490 * PR TYPEs to pass for registered and
491 * non-registered_nexuxes NOT holding the reservation.
493 * We only make noise for the unregisterd nexuses,
494 * as we expect registered non-reservation holding
495 * nexuses to issue CDBs.
498 if (!registered_nexus
) {
499 pr_debug("Allowing implict CDB: 0x%02x"
500 " for %s reservation on unregistered"
502 core_scsi3_pr_dump_type(pr_reg_type
));
507 } else if ((reg_only
) || (all_reg
)) {
508 if (registered_nexus
) {
510 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
511 * allow commands from registered nexuses.
514 pr_debug("Allowing implict CDB: 0x%02x for %s"
515 " reservation\n", cdb
[0],
516 core_scsi3_pr_dump_type(pr_reg_type
));
521 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
522 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
523 (registered_nexus
) ? "" : "un",
524 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
525 core_scsi3_pr_dump_type(pr_reg_type
));
527 return 1; /* Conflict by default */
530 static sense_reason_t
531 target_scsi3_pr_reservation_check(struct se_cmd
*cmd
)
533 struct se_device
*dev
= cmd
->se_dev
;
534 struct se_session
*sess
= cmd
->se_sess
;
537 if (!dev
->dev_pr_res_holder
)
540 pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
541 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
542 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
)
543 goto check_nonholder
;
545 if (dev
->dev_pr_res_holder
->isid_present_at_reg
) {
546 if (dev
->dev_pr_res_holder
->pr_reg_bin_isid
!=
547 sess
->sess_bin_isid
) {
548 pr_reg_type
|= 0x80000000;
549 goto check_nonholder
;
556 if (core_scsi3_pr_seq_non_holder(cmd
, pr_reg_type
))
557 return TCM_RESERVATION_CONFLICT
;
561 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
566 * PRGeneration field shall contain the value of a 32-bit wrapping
567 * counter mainted by the device server.
569 * Note that this is done regardless of Active Persist across
570 * Target PowerLoss (APTPL)
572 * See spc4r17 section 6.3.12 READ_KEYS service action
574 spin_lock(&dev
->dev_reservation_lock
);
575 prg
= dev
->t10_pr
.pr_generation
++;
576 spin_unlock(&dev
->dev_reservation_lock
);
581 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
582 struct se_device
*dev
,
583 struct se_node_acl
*nacl
,
584 struct se_dev_entry
*deve
,
590 struct t10_pr_registration
*pr_reg
;
592 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
594 pr_err("Unable to allocate struct t10_pr_registration\n");
598 pr_reg
->pr_aptpl_buf
= kzalloc(dev
->t10_pr
.pr_aptpl_buf_len
,
600 if (!pr_reg
->pr_aptpl_buf
) {
601 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
602 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
606 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
607 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
608 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
609 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
610 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
611 atomic_set(&pr_reg
->pr_res_holders
, 0);
612 pr_reg
->pr_reg_nacl
= nacl
;
613 pr_reg
->pr_reg_deve
= deve
;
614 pr_reg
->pr_res_mapped_lun
= deve
->mapped_lun
;
615 pr_reg
->pr_aptpl_target_lun
= deve
->se_lun
->unpacked_lun
;
616 pr_reg
->pr_res_key
= sa_res_key
;
617 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
618 pr_reg
->pr_reg_aptpl
= aptpl
;
619 pr_reg
->pr_reg_tg_pt_lun
= deve
->se_lun
;
621 * If an ISID value for this SCSI Initiator Port exists,
622 * save it to the registration now.
625 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
626 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
627 pr_reg
->isid_present_at_reg
= 1;
633 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
634 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
637 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
640 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
641 struct se_device
*dev
,
642 struct se_node_acl
*nacl
,
643 struct se_dev_entry
*deve
,
649 struct se_dev_entry
*deve_tmp
;
650 struct se_node_acl
*nacl_tmp
;
651 struct se_port
*port
, *port_tmp
;
652 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
653 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
656 * Create a registration for the I_T Nexus upon which the
657 * PROUT REGISTER was received.
659 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, deve
, isid
,
660 sa_res_key
, all_tg_pt
, aptpl
);
664 * Return pointer to pr_reg for ALL_TG_PT=0
669 * Create list of matching SCSI Initiator Port registrations
672 spin_lock(&dev
->se_port_lock
);
673 list_for_each_entry_safe(port
, port_tmp
, &dev
->dev_sep_list
, sep_list
) {
674 atomic_inc(&port
->sep_tg_pt_ref_cnt
);
675 smp_mb__after_atomic_inc();
676 spin_unlock(&dev
->se_port_lock
);
678 spin_lock_bh(&port
->sep_alua_lock
);
679 list_for_each_entry(deve_tmp
, &port
->sep_alua_list
,
682 * This pointer will be NULL for demo mode MappedLUNs
683 * that have not been make explict via a ConfigFS
684 * MappedLUN group for the SCSI Initiator Node ACL.
686 if (!deve_tmp
->se_lun_acl
)
689 nacl_tmp
= deve_tmp
->se_lun_acl
->se_lun_nacl
;
691 * Skip the matching struct se_node_acl that is allocated
694 if (nacl
== nacl_tmp
)
697 * Only perform PR registrations for target ports on
698 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
701 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
704 * Look for a matching Initiator Node ACL in ASCII format
706 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
709 atomic_inc(&deve_tmp
->pr_ref_count
);
710 smp_mb__after_atomic_inc();
711 spin_unlock_bh(&port
->sep_alua_lock
);
713 * Grab a configfs group dependency that is released
714 * for the exception path at label out: below, or upon
715 * completion of adding ALL_TG_PT=1 registrations in
716 * __core_scsi3_add_registration()
718 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
720 pr_err("core_scsi3_lunacl_depend"
722 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
723 smp_mb__after_atomic_dec();
724 atomic_dec(&deve_tmp
->pr_ref_count
);
725 smp_mb__after_atomic_dec();
729 * Located a matching SCSI Initiator Port on a different
730 * port, allocate the pr_reg_atp and attach it to the
731 * pr_reg->pr_reg_atp_list that will be processed once
732 * the original *pr_reg is processed in
733 * __core_scsi3_add_registration()
735 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
736 nacl_tmp
, deve_tmp
, NULL
,
737 sa_res_key
, all_tg_pt
, aptpl
);
739 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
740 smp_mb__after_atomic_dec();
741 atomic_dec(&deve_tmp
->pr_ref_count
);
742 smp_mb__after_atomic_dec();
743 core_scsi3_lunacl_undepend_item(deve_tmp
);
747 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
748 &pr_reg
->pr_reg_atp_list
);
749 spin_lock_bh(&port
->sep_alua_lock
);
751 spin_unlock_bh(&port
->sep_alua_lock
);
753 spin_lock(&dev
->se_port_lock
);
754 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
755 smp_mb__after_atomic_dec();
757 spin_unlock(&dev
->se_port_lock
);
761 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
762 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
763 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
764 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
765 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
767 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
771 int core_scsi3_alloc_aptpl_registration(
772 struct t10_reservation
*pr_tmpl
,
774 unsigned char *i_port
,
777 unsigned char *t_port
,
784 struct t10_pr_registration
*pr_reg
;
786 if (!i_port
|| !t_port
|| !sa_res_key
) {
787 pr_err("Illegal parameters for APTPL registration\n");
791 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
793 pr_err("Unable to allocate struct t10_pr_registration\n");
796 pr_reg
->pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
, GFP_KERNEL
);
798 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
799 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
800 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
801 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
802 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
803 atomic_set(&pr_reg
->pr_res_holders
, 0);
804 pr_reg
->pr_reg_nacl
= NULL
;
805 pr_reg
->pr_reg_deve
= NULL
;
806 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
807 pr_reg
->pr_aptpl_target_lun
= target_lun
;
808 pr_reg
->pr_res_key
= sa_res_key
;
809 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
810 pr_reg
->pr_reg_aptpl
= 1;
811 pr_reg
->pr_reg_tg_pt_lun
= NULL
;
812 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
813 pr_reg
->pr_res_type
= type
;
815 * If an ISID value had been saved in APTPL metadata for this
816 * SCSI Initiator Port, restore it now.
819 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
820 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
821 pr_reg
->isid_present_at_reg
= 1;
824 * Copy the i_port and t_port information from caller.
826 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
827 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
828 pr_reg
->pr_reg_tpgt
= tpgt
;
830 * Set pr_res_holder from caller, the pr_reg who is the reservation
831 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
832 * the Initiator Node LUN ACL from the fabric module is created for
835 pr_reg
->pr_res_holder
= res_holder
;
837 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
838 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
839 " metadata\n", (res_holder
) ? "+reservation" : "");
843 static void core_scsi3_aptpl_reserve(
844 struct se_device
*dev
,
845 struct se_portal_group
*tpg
,
846 struct se_node_acl
*node_acl
,
847 struct t10_pr_registration
*pr_reg
)
849 char i_buf
[PR_REG_ISID_ID_LEN
];
852 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
853 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
856 spin_lock(&dev
->dev_reservation_lock
);
857 dev
->dev_pr_res_holder
= pr_reg
;
858 spin_unlock(&dev
->dev_reservation_lock
);
860 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
861 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
862 tpg
->se_tpg_tfo
->get_fabric_name(),
863 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
864 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
865 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
866 tpg
->se_tpg_tfo
->get_fabric_name(), node_acl
->initiatorname
,
867 (prf_isid
) ? &i_buf
[0] : "");
870 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
871 struct t10_pr_registration
*, int, int);
873 static int __core_scsi3_check_aptpl_registration(
874 struct se_device
*dev
,
875 struct se_portal_group
*tpg
,
878 struct se_node_acl
*nacl
,
879 struct se_dev_entry
*deve
)
881 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
882 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
883 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
884 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
887 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
888 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
890 * Copy Initiator Port information from struct se_node_acl
892 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
893 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
894 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
895 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
897 * Look for the matching registrations+reservation from those
898 * created from APTPL metadata. Note that multiple registrations
899 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
902 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
903 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
905 if (!strcmp(pr_reg
->pr_iport
, i_port
) &&
906 (pr_reg
->pr_res_mapped_lun
== deve
->mapped_lun
) &&
907 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
908 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
909 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
911 pr_reg
->pr_reg_nacl
= nacl
;
912 pr_reg
->pr_reg_deve
= deve
;
913 pr_reg
->pr_reg_tg_pt_lun
= lun
;
915 list_del(&pr_reg
->pr_reg_aptpl_list
);
916 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
918 * At this point all of the pointers in *pr_reg will
919 * be setup, so go ahead and add the registration.
922 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
924 * If this registration is the reservation holder,
925 * make that happen now..
927 if (pr_reg
->pr_res_holder
)
928 core_scsi3_aptpl_reserve(dev
, tpg
,
931 * Reenable pr_aptpl_active to accept new metadata
932 * updates once the SCSI device is active again..
934 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
935 pr_tmpl
->pr_aptpl_active
= 1;
938 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
943 int core_scsi3_check_aptpl_registration(
944 struct se_device
*dev
,
945 struct se_portal_group
*tpg
,
947 struct se_lun_acl
*lun_acl
)
949 struct se_node_acl
*nacl
= lun_acl
->se_lun_nacl
;
950 struct se_dev_entry
*deve
= nacl
->device_list
[lun_acl
->mapped_lun
];
952 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
955 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
956 lun
->unpacked_lun
, nacl
, deve
);
959 static void __core_scsi3_dump_registration(
960 struct target_core_fabric_ops
*tfo
,
961 struct se_device
*dev
,
962 struct se_node_acl
*nacl
,
963 struct t10_pr_registration
*pr_reg
,
966 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
967 char i_buf
[PR_REG_ISID_ID_LEN
];
970 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
971 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
974 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
975 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== 2) ?
976 "_AND_MOVE" : (register_type
== 1) ?
977 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
978 (prf_isid
) ? i_buf
: "");
979 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
980 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
981 tfo
->tpg_get_tag(se_tpg
));
982 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
983 " Port(s)\n", tfo
->get_fabric_name(),
984 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
985 dev
->transport
->name
);
986 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
987 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
988 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
989 pr_reg
->pr_reg_aptpl
);
993 * this function can be called with struct se_device->dev_reservation_lock
994 * when register_move = 1
996 static void __core_scsi3_add_registration(
997 struct se_device
*dev
,
998 struct se_node_acl
*nacl
,
999 struct t10_pr_registration
*pr_reg
,
1003 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1004 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1005 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1008 * Increment PRgeneration counter for struct se_device upon a successful
1009 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1011 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1012 * action, the struct se_device->dev_reservation_lock will already be held,
1013 * so we do not call core_scsi3_pr_generation() which grabs the lock
1016 pr_reg
->pr_res_generation
= (register_move
) ?
1017 dev
->t10_pr
.pr_generation
++ :
1018 core_scsi3_pr_generation(dev
);
1020 spin_lock(&pr_tmpl
->registration_lock
);
1021 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1022 pr_reg
->pr_reg_deve
->def_pr_registered
= 1;
1024 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1025 spin_unlock(&pr_tmpl
->registration_lock
);
1027 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1029 if (!pr_reg
->pr_reg_all_tg_pt
|| register_move
)
1032 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1033 * allocated in __core_scsi3_alloc_registration()
1035 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1036 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1037 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1039 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1041 spin_lock(&pr_tmpl
->registration_lock
);
1042 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1043 &pr_tmpl
->registration_list
);
1044 pr_reg_tmp
->pr_reg_deve
->def_pr_registered
= 1;
1046 __core_scsi3_dump_registration(tfo
, dev
,
1047 pr_reg_tmp
->pr_reg_nacl
, pr_reg_tmp
,
1049 spin_unlock(&pr_tmpl
->registration_lock
);
1051 * Drop configfs group dependency reference from
1052 * __core_scsi3_alloc_registration()
1054 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1058 static int core_scsi3_alloc_registration(
1059 struct se_device
*dev
,
1060 struct se_node_acl
*nacl
,
1061 struct se_dev_entry
*deve
,
1062 unsigned char *isid
,
1069 struct t10_pr_registration
*pr_reg
;
1071 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, deve
, isid
,
1072 sa_res_key
, all_tg_pt
, aptpl
);
1076 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1077 register_type
, register_move
);
1081 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1082 struct se_device
*dev
,
1083 struct se_node_acl
*nacl
,
1084 unsigned char *isid
)
1086 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1087 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1088 struct se_portal_group
*tpg
;
1090 spin_lock(&pr_tmpl
->registration_lock
);
1091 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1092 &pr_tmpl
->registration_list
, pr_reg_list
) {
1094 * First look for a matching struct se_node_acl
1096 if (pr_reg
->pr_reg_nacl
!= nacl
)
1099 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1101 * If this registration does NOT contain a fabric provided
1102 * ISID, then we have found a match.
1104 if (!pr_reg
->isid_present_at_reg
) {
1106 * Determine if this SCSI device server requires that
1107 * SCSI Intiatior TransportID w/ ISIDs is enforced
1108 * for fabric modules (iSCSI) requiring them.
1110 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1111 if (dev
->dev_attrib
.enforce_pr_isids
)
1114 atomic_inc(&pr_reg
->pr_res_holders
);
1115 smp_mb__after_atomic_inc();
1116 spin_unlock(&pr_tmpl
->registration_lock
);
1120 * If the *pr_reg contains a fabric defined ISID for multi-value
1121 * SCSI Initiator Port TransportIDs, then we expect a valid
1122 * matching ISID to be provided by the local SCSI Initiator Port.
1126 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1129 atomic_inc(&pr_reg
->pr_res_holders
);
1130 smp_mb__after_atomic_inc();
1131 spin_unlock(&pr_tmpl
->registration_lock
);
1134 spin_unlock(&pr_tmpl
->registration_lock
);
1139 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1140 struct se_device
*dev
,
1141 struct se_node_acl
*nacl
,
1142 struct se_session
*sess
)
1144 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1145 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1147 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1148 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1149 tpg
->se_tpg_tfo
->sess_get_initiator_sid(sess
, &buf
[0],
1154 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1157 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1159 atomic_dec(&pr_reg
->pr_res_holders
);
1160 smp_mb__after_atomic_dec();
1163 static int core_scsi3_check_implict_release(
1164 struct se_device
*dev
,
1165 struct t10_pr_registration
*pr_reg
)
1167 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1168 struct t10_pr_registration
*pr_res_holder
;
1171 spin_lock(&dev
->dev_reservation_lock
);
1172 pr_res_holder
= dev
->dev_pr_res_holder
;
1173 if (!pr_res_holder
) {
1174 spin_unlock(&dev
->dev_reservation_lock
);
1177 if (pr_res_holder
== pr_reg
) {
1179 * Perform an implict RELEASE if the registration that
1180 * is being released is holding the reservation.
1182 * From spc4r17, section 5.7.11.1:
1184 * e) If the I_T nexus is the persistent reservation holder
1185 * and the persistent reservation is not an all registrants
1186 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1187 * service action or REGISTER AND IGNORE EXISTING KEY
1188 * service action with the SERVICE ACTION RESERVATION KEY
1189 * field set to zero (see 5.7.11.3).
1191 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0);
1194 * For 'All Registrants' reservation types, all existing
1195 * registrations are still processed as reservation holders
1196 * in core_scsi3_pr_seq_non_holder() after the initial
1197 * reservation holder is implictly released here.
1199 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1200 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1201 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1202 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1203 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1204 " UNREGISTER while existing reservation with matching"
1205 " key 0x%016Lx is present from another SCSI Initiator"
1206 " Port\n", pr_reg
->pr_res_key
);
1209 spin_unlock(&dev
->dev_reservation_lock
);
1215 * Called with struct t10_reservation->registration_lock held.
1217 static void __core_scsi3_free_registration(
1218 struct se_device
*dev
,
1219 struct t10_pr_registration
*pr_reg
,
1220 struct list_head
*preempt_and_abort_list
,
1223 struct target_core_fabric_ops
*tfo
=
1224 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1225 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1226 char i_buf
[PR_REG_ISID_ID_LEN
];
1229 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1230 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1231 PR_REG_ISID_ID_LEN
);
1233 pr_reg
->pr_reg_deve
->def_pr_registered
= 0;
1234 pr_reg
->pr_reg_deve
->pr_res_key
= 0;
1235 list_del(&pr_reg
->pr_reg_list
);
1237 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1238 * so call core_scsi3_put_pr_reg() to decrement our reference.
1241 core_scsi3_put_pr_reg(pr_reg
);
1243 * Wait until all reference from any other I_T nexuses for this
1244 * *pr_reg have been released. Because list_del() is called above,
1245 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1246 * count back to zero, and we release *pr_reg.
1248 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1249 spin_unlock(&pr_tmpl
->registration_lock
);
1250 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1251 tfo
->get_fabric_name());
1253 spin_lock(&pr_tmpl
->registration_lock
);
1256 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1257 " Node: %s%s\n", tfo
->get_fabric_name(),
1258 pr_reg
->pr_reg_nacl
->initiatorname
,
1259 (prf_isid
) ? &i_buf
[0] : "");
1260 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1261 " Port(s)\n", tfo
->get_fabric_name(),
1262 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1263 dev
->transport
->name
);
1264 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1265 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1266 pr_reg
->pr_res_generation
);
1268 if (!preempt_and_abort_list
) {
1269 pr_reg
->pr_reg_deve
= NULL
;
1270 pr_reg
->pr_reg_nacl
= NULL
;
1271 kfree(pr_reg
->pr_aptpl_buf
);
1272 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1276 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1277 * are released once the ABORT_TASK_SET has completed..
1279 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1282 void core_scsi3_free_pr_reg_from_nacl(
1283 struct se_device
*dev
,
1284 struct se_node_acl
*nacl
)
1286 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1287 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1289 * If the passed se_node_acl matches the reservation holder,
1290 * release the reservation.
1292 spin_lock(&dev
->dev_reservation_lock
);
1293 pr_res_holder
= dev
->dev_pr_res_holder
;
1294 if ((pr_res_holder
!= NULL
) &&
1295 (pr_res_holder
->pr_reg_nacl
== nacl
))
1296 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0);
1297 spin_unlock(&dev
->dev_reservation_lock
);
1299 * Release any registration associated with the struct se_node_acl.
1301 spin_lock(&pr_tmpl
->registration_lock
);
1302 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1303 &pr_tmpl
->registration_list
, pr_reg_list
) {
1305 if (pr_reg
->pr_reg_nacl
!= nacl
)
1308 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1310 spin_unlock(&pr_tmpl
->registration_lock
);
1313 void core_scsi3_free_all_registrations(
1314 struct se_device
*dev
)
1316 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1317 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1319 spin_lock(&dev
->dev_reservation_lock
);
1320 pr_res_holder
= dev
->dev_pr_res_holder
;
1321 if (pr_res_holder
!= NULL
) {
1322 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1323 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1326 spin_unlock(&dev
->dev_reservation_lock
);
1328 spin_lock(&pr_tmpl
->registration_lock
);
1329 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1330 &pr_tmpl
->registration_list
, pr_reg_list
) {
1332 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1334 spin_unlock(&pr_tmpl
->registration_lock
);
1336 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1337 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1338 pr_reg_aptpl_list
) {
1339 list_del(&pr_reg
->pr_reg_aptpl_list
);
1340 kfree(pr_reg
->pr_aptpl_buf
);
1341 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1343 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1346 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1348 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1349 &tpg
->tpg_group
.cg_item
);
1352 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1354 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1355 &tpg
->tpg_group
.cg_item
);
1357 atomic_dec(&tpg
->tpg_pr_ref_count
);
1358 smp_mb__after_atomic_dec();
1361 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1363 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1365 if (nacl
->dynamic_node_acl
)
1368 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1369 &nacl
->acl_group
.cg_item
);
1372 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1374 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1376 if (nacl
->dynamic_node_acl
) {
1377 atomic_dec(&nacl
->acl_pr_ref_count
);
1378 smp_mb__after_atomic_dec();
1382 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1383 &nacl
->acl_group
.cg_item
);
1385 atomic_dec(&nacl
->acl_pr_ref_count
);
1386 smp_mb__after_atomic_dec();
1389 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1391 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1392 struct se_node_acl
*nacl
;
1393 struct se_portal_group
*tpg
;
1395 * For nacl->dynamic_node_acl=1
1400 nacl
= lun_acl
->se_lun_nacl
;
1403 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1404 &lun_acl
->se_lun_group
.cg_item
);
1407 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1409 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1410 struct se_node_acl
*nacl
;
1411 struct se_portal_group
*tpg
;
1413 * For nacl->dynamic_node_acl=1
1416 atomic_dec(&se_deve
->pr_ref_count
);
1417 smp_mb__after_atomic_dec();
1420 nacl
= lun_acl
->se_lun_nacl
;
1423 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1424 &lun_acl
->se_lun_group
.cg_item
);
1426 atomic_dec(&se_deve
->pr_ref_count
);
1427 smp_mb__after_atomic_dec();
1430 static sense_reason_t
1431 core_scsi3_decode_spec_i_port(
1433 struct se_portal_group
*tpg
,
1434 unsigned char *l_isid
,
1439 struct se_device
*dev
= cmd
->se_dev
;
1440 struct se_port
*tmp_port
;
1441 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1442 struct se_session
*se_sess
= cmd
->se_sess
;
1443 struct se_node_acl
*dest_node_acl
= NULL
;
1444 struct se_dev_entry
*dest_se_deve
= NULL
, *local_se_deve
;
1445 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1446 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1447 LIST_HEAD(tid_dest_list
);
1448 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1449 struct target_core_fabric_ops
*tmp_tf_ops
;
1451 unsigned char *ptr
, *i_str
= NULL
, proto_ident
, tmp_proto_ident
;
1452 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
1454 u32 tpdl
, tid_len
= 0;
1455 int dest_local_nexus
, prf_isid
;
1458 memset(dest_iport
, 0, 64);
1460 local_se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
1462 * Allocate a struct pr_transport_id_holder and setup the
1463 * local_node_acl and local_se_deve pointers and add to
1464 * struct list_head tid_dest_list for add registration
1465 * processing in the loop of tid_dest_list below.
1467 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1469 pr_err("Unable to allocate tidh_new\n");
1470 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1472 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1473 tidh_new
->dest_tpg
= tpg
;
1474 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1475 tidh_new
->dest_se_deve
= local_se_deve
;
1477 local_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1478 se_sess
->se_node_acl
, local_se_deve
, l_isid
,
1479 sa_res_key
, all_tg_pt
, aptpl
);
1480 if (!local_pr_reg
) {
1482 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1484 tidh_new
->dest_pr_reg
= local_pr_reg
;
1486 * The local I_T nexus does not hold any configfs dependances,
1487 * so we set tid_h->dest_local_nexus=1 to prevent the
1488 * configfs_undepend_item() calls in the tid_dest_list loops below.
1490 tidh_new
->dest_local_nexus
= 1;
1491 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1493 if (cmd
->data_length
< 28) {
1494 pr_warn("SPC-PR: Received PR OUT parameter list"
1495 " length too small: %u\n", cmd
->data_length
);
1496 ret
= TCM_INVALID_PARAMETER_LIST
;
1500 buf
= transport_kmap_data_sg(cmd
);
1502 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1507 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1508 * first extract TransportID Parameter Data Length, and make sure
1509 * the value matches up to the SCSI expected data transfer length.
1511 tpdl
= (buf
[24] & 0xff) << 24;
1512 tpdl
|= (buf
[25] & 0xff) << 16;
1513 tpdl
|= (buf
[26] & 0xff) << 8;
1514 tpdl
|= buf
[27] & 0xff;
1516 if ((tpdl
+ 28) != cmd
->data_length
) {
1517 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1518 " does not equal CDB data_length: %u\n", tpdl
,
1520 ret
= TCM_INVALID_PARAMETER_LIST
;
1524 * Start processing the received transport IDs using the
1525 * receiving I_T Nexus portal's fabric dependent methods to
1526 * obtain the SCSI Initiator Port/Device Identifiers.
1531 proto_ident
= (ptr
[0] & 0x0f);
1534 spin_lock(&dev
->se_port_lock
);
1535 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1536 tmp_tpg
= tmp_port
->sep_tpg
;
1539 tmp_tf_ops
= tmp_tpg
->se_tpg_tfo
;
1542 if (!tmp_tf_ops
->get_fabric_proto_ident
||
1543 !tmp_tf_ops
->tpg_parse_pr_out_transport_id
)
1546 * Look for the matching proto_ident provided by
1547 * the received TransportID
1549 tmp_proto_ident
= tmp_tf_ops
->get_fabric_proto_ident(tmp_tpg
);
1550 if (tmp_proto_ident
!= proto_ident
)
1552 dest_rtpi
= tmp_port
->sep_rtpi
;
1554 i_str
= tmp_tf_ops
->tpg_parse_pr_out_transport_id(
1555 tmp_tpg
, (const char *)ptr
, &tid_len
,
1560 atomic_inc(&tmp_tpg
->tpg_pr_ref_count
);
1561 smp_mb__after_atomic_inc();
1562 spin_unlock(&dev
->se_port_lock
);
1564 if (core_scsi3_tpg_depend_item(tmp_tpg
)) {
1565 pr_err(" core_scsi3_tpg_depend_item()"
1567 atomic_dec(&tmp_tpg
->tpg_pr_ref_count
);
1568 smp_mb__after_atomic_dec();
1569 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1573 * Locate the destination initiator ACL to be registered
1574 * from the decoded fabric module specific TransportID
1577 spin_lock_irq(&tmp_tpg
->acl_node_lock
);
1578 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1580 if (dest_node_acl
) {
1581 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
1582 smp_mb__after_atomic_inc();
1584 spin_unlock_irq(&tmp_tpg
->acl_node_lock
);
1586 if (!dest_node_acl
) {
1587 core_scsi3_tpg_undepend_item(tmp_tpg
);
1588 spin_lock(&dev
->se_port_lock
);
1592 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
1593 pr_err("configfs_depend_item() failed"
1594 " for dest_node_acl->acl_group\n");
1595 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
1596 smp_mb__after_atomic_dec();
1597 core_scsi3_tpg_undepend_item(tmp_tpg
);
1598 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1603 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1604 " %s Port RTPI: %hu\n",
1605 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1606 dest_node_acl
->initiatorname
, dest_rtpi
);
1608 spin_lock(&dev
->se_port_lock
);
1611 spin_unlock(&dev
->se_port_lock
);
1614 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1616 ret
= TCM_INVALID_PARAMETER_LIST
;
1620 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1621 " tid_len: %d for %s + %s\n",
1622 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1623 tpdl
, tid_len
, i_str
, iport_ptr
);
1625 if (tid_len
> tpdl
) {
1626 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1627 " %u for Transport ID: %s\n", tid_len
, ptr
);
1628 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1629 core_scsi3_tpg_undepend_item(dest_tpg
);
1630 ret
= TCM_INVALID_PARAMETER_LIST
;
1634 * Locate the desintation struct se_dev_entry pointer for matching
1635 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1638 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1640 if (!dest_se_deve
) {
1641 pr_err("Unable to locate %s dest_se_deve"
1642 " from destination RTPI: %hu\n",
1643 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1646 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1647 core_scsi3_tpg_undepend_item(dest_tpg
);
1648 ret
= TCM_INVALID_PARAMETER_LIST
;
1652 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
1653 pr_err("core_scsi3_lunacl_depend_item()"
1655 atomic_dec(&dest_se_deve
->pr_ref_count
);
1656 smp_mb__after_atomic_dec();
1657 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1658 core_scsi3_tpg_undepend_item(dest_tpg
);
1659 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1663 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1664 " dest_se_deve mapped_lun: %u\n",
1665 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1666 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1669 * Skip any TransportIDs that already have a registration for
1672 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1675 core_scsi3_put_pr_reg(pr_reg_e
);
1676 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1677 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1678 core_scsi3_tpg_undepend_item(dest_tpg
);
1685 * Allocate a struct pr_transport_id_holder and setup
1686 * the dest_node_acl and dest_se_deve pointers for the
1689 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1692 pr_err("Unable to allocate tidh_new\n");
1693 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1694 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1695 core_scsi3_tpg_undepend_item(dest_tpg
);
1696 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1699 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1700 tidh_new
->dest_tpg
= dest_tpg
;
1701 tidh_new
->dest_node_acl
= dest_node_acl
;
1702 tidh_new
->dest_se_deve
= dest_se_deve
;
1705 * Allocate, but do NOT add the registration for the
1706 * TransportID referenced SCSI Initiator port. This
1707 * done because of the following from spc4r17 in section
1708 * 6.14.3 wrt SPEC_I_PT:
1710 * "If a registration fails for any initiator port (e.g., if th
1711 * logical unit does not have enough resources available to
1712 * hold the registration information), no registrations shall be
1713 * made, and the command shall be terminated with
1714 * CHECK CONDITION status."
1716 * That means we call __core_scsi3_alloc_registration() here,
1717 * and then call __core_scsi3_add_registration() in the
1718 * 2nd loop which will never fail.
1720 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1721 dest_node_acl
, dest_se_deve
, iport_ptr
,
1722 sa_res_key
, all_tg_pt
, aptpl
);
1724 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1725 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1726 core_scsi3_tpg_undepend_item(dest_tpg
);
1728 ret
= TCM_INVALID_PARAMETER_LIST
;
1731 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1732 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1740 transport_kunmap_data_sg(cmd
);
1743 * Go ahead and create a registrations from tid_dest_list for the
1744 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1747 * The SA Reservation Key from the PROUT is set for the
1748 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1749 * means that the TransportID Initiator port will be
1750 * registered on all of the target ports in the SCSI target device
1751 * ALL_TG_PT=0 means the registration will only be for the
1752 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1755 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1756 dest_tpg
= tidh
->dest_tpg
;
1757 dest_node_acl
= tidh
->dest_node_acl
;
1758 dest_se_deve
= tidh
->dest_se_deve
;
1759 dest_pr_reg
= tidh
->dest_pr_reg
;
1760 dest_local_nexus
= tidh
->dest_local_nexus
;
1762 list_del(&tidh
->dest_list
);
1765 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1766 prf_isid
= core_pr_dump_initiator_port(dest_pr_reg
, &i_buf
[0],
1767 PR_REG_ISID_ID_LEN
);
1769 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1772 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1773 " registered Transport ID for Node: %s%s Mapped LUN:"
1774 " %u\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1775 dest_node_acl
->initiatorname
, (prf_isid
) ?
1776 &i_buf
[0] : "", dest_se_deve
->mapped_lun
);
1778 if (dest_local_nexus
)
1781 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1782 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1783 core_scsi3_tpg_undepend_item(dest_tpg
);
1788 transport_kunmap_data_sg(cmd
);
1791 * For the failure case, release everything from tid_dest_list
1792 * including *dest_pr_reg and the configfs dependances..
1794 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1795 dest_tpg
= tidh
->dest_tpg
;
1796 dest_node_acl
= tidh
->dest_node_acl
;
1797 dest_se_deve
= tidh
->dest_se_deve
;
1798 dest_pr_reg
= tidh
->dest_pr_reg
;
1799 dest_local_nexus
= tidh
->dest_local_nexus
;
1801 list_del(&tidh
->dest_list
);
1804 * Release any extra ALL_TG_PT=1 registrations for
1805 * the SPEC_I_PT=1 case.
1807 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1808 &dest_pr_reg
->pr_reg_atp_list
,
1809 pr_reg_atp_mem_list
) {
1810 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1811 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1812 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1815 kfree(dest_pr_reg
->pr_aptpl_buf
);
1816 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1818 if (dest_local_nexus
)
1821 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1822 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1823 core_scsi3_tpg_undepend_item(dest_tpg
);
1829 * Called with struct se_device->dev_reservation_lock held
1831 static int __core_scsi3_update_aptpl_buf(
1832 struct se_device
*dev
,
1834 u32 pr_aptpl_buf_len
,
1835 int clear_aptpl_metadata
)
1838 struct se_portal_group
*tpg
;
1839 struct t10_pr_registration
*pr_reg
;
1840 unsigned char tmp
[512], isid_buf
[32];
1844 memset(buf
, 0, pr_aptpl_buf_len
);
1846 * Called to clear metadata once APTPL has been deactivated.
1848 if (clear_aptpl_metadata
) {
1849 snprintf(buf
, pr_aptpl_buf_len
,
1850 "No Registrations or Reservations\n");
1854 * Walk the registration list..
1856 spin_lock(&dev
->t10_pr
.registration_lock
);
1857 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
1862 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1863 lun
= pr_reg
->pr_reg_tg_pt_lun
;
1865 * Write out any ISID value to APTPL metadata that was included
1866 * in the original registration.
1868 if (pr_reg
->isid_present_at_reg
)
1869 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1870 pr_reg
->pr_reg_isid
);
1872 * Include special metadata if the pr_reg matches the
1873 * reservation holder.
1875 if (dev
->dev_pr_res_holder
== pr_reg
) {
1876 snprintf(tmp
, 512, "PR_REG_START: %d"
1877 "\ninitiator_fabric=%s\n"
1878 "initiator_node=%s\n%s"
1880 "res_holder=1\nres_type=%02x\n"
1881 "res_scope=%02x\nres_all_tg_pt=%d\n"
1882 "mapped_lun=%u\n", reg_count
,
1883 tpg
->se_tpg_tfo
->get_fabric_name(),
1884 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1885 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1886 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1887 pr_reg
->pr_res_mapped_lun
);
1889 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1890 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1891 "sa_res_key=%llu\nres_holder=0\n"
1892 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1893 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1894 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1895 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1896 pr_reg
->pr_res_mapped_lun
);
1899 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1900 pr_err("Unable to update renaming"
1901 " APTPL metadata\n");
1902 spin_unlock(&dev
->t10_pr
.registration_lock
);
1905 len
+= sprintf(buf
+len
, "%s", tmp
);
1908 * Include information about the associated SCSI target port.
1910 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1911 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1912 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1913 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1914 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1915 lun
->lun_sep
->sep_rtpi
, lun
->unpacked_lun
, reg_count
);
1917 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1918 pr_err("Unable to update renaming"
1919 " APTPL metadata\n");
1920 spin_unlock(&dev
->t10_pr
.registration_lock
);
1923 len
+= sprintf(buf
+len
, "%s", tmp
);
1926 spin_unlock(&dev
->t10_pr
.registration_lock
);
1929 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1934 static int core_scsi3_update_aptpl_buf(
1935 struct se_device
*dev
,
1937 u32 pr_aptpl_buf_len
,
1938 int clear_aptpl_metadata
)
1942 spin_lock(&dev
->dev_reservation_lock
);
1943 ret
= __core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
1944 clear_aptpl_metadata
);
1945 spin_unlock(&dev
->dev_reservation_lock
);
1951 * Called with struct se_device->aptpl_file_mutex held
1953 static int __core_scsi3_write_aptpl_to_file(
1954 struct se_device
*dev
,
1956 u32 pr_aptpl_buf_len
)
1958 struct t10_wwn
*wwn
= &dev
->t10_wwn
;
1960 struct iovec iov
[1];
1961 mm_segment_t old_fs
;
1962 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1966 memset(iov
, 0, sizeof(struct iovec
));
1967 memset(path
, 0, 512);
1969 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
1970 pr_err("WWN value for struct se_device does not fit"
1971 " into path buffer\n");
1975 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
1976 file
= filp_open(path
, flags
, 0600);
1977 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
1978 pr_err("filp_open(%s) for APTPL metadata"
1980 return IS_ERR(file
) ? PTR_ERR(file
) : -ENOENT
;
1983 iov
[0].iov_base
= &buf
[0];
1984 if (!pr_aptpl_buf_len
)
1985 iov
[0].iov_len
= (strlen(&buf
[0]) + 1); /* Add extra for NULL */
1987 iov
[0].iov_len
= pr_aptpl_buf_len
;
1991 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
1995 pr_debug("Error writing APTPL metadata file: %s\n", path
);
1996 filp_close(file
, NULL
);
1999 filp_close(file
, NULL
);
2005 core_scsi3_update_and_write_aptpl(struct se_device
*dev
, unsigned char *in_buf
,
2006 u32 in_pr_aptpl_buf_len
)
2008 unsigned char null_buf
[64], *buf
;
2009 u32 pr_aptpl_buf_len
;
2010 int clear_aptpl_metadata
= 0;
2014 * Can be called with a NULL pointer from PROUT service action CLEAR
2017 memset(null_buf
, 0, 64);
2020 * This will clear the APTPL metadata to:
2021 * "No Registrations or Reservations" status
2023 pr_aptpl_buf_len
= 64;
2024 clear_aptpl_metadata
= 1;
2027 pr_aptpl_buf_len
= in_pr_aptpl_buf_len
;
2030 ret
= core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
2031 clear_aptpl_metadata
);
2036 * __core_scsi3_write_aptpl_to_file() will call strlen()
2037 * on the passed buf to determine pr_aptpl_buf_len.
2039 return __core_scsi3_write_aptpl_to_file(dev
, buf
, 0);
2042 static sense_reason_t
2043 core_scsi3_emulate_pro_register(struct se_cmd
*cmd
, u64 res_key
, u64 sa_res_key
,
2044 int aptpl
, int all_tg_pt
, int spec_i_pt
, int ignore_key
)
2046 struct se_session
*se_sess
= cmd
->se_sess
;
2047 struct se_device
*dev
= cmd
->se_dev
;
2048 struct se_dev_entry
*se_deve
;
2049 struct se_lun
*se_lun
= cmd
->se_lun
;
2050 struct se_portal_group
*se_tpg
;
2051 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
, *pr_reg_e
;
2052 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2053 /* Used for APTPL metadata w/ UNREGISTER */
2054 unsigned char *pr_aptpl_buf
= NULL
;
2055 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2057 int pr_holder
= 0, type
;
2059 if (!se_sess
|| !se_lun
) {
2060 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2061 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2063 se_tpg
= se_sess
->se_tpg
;
2064 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2066 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2067 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2068 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2070 isid_ptr
= &isid_buf
[0];
2073 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2075 pr_reg_e
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2078 pr_warn("SPC-3 PR: Reservation Key non-zero"
2079 " for SA REGISTER, returning CONFLICT\n");
2080 return TCM_RESERVATION_CONFLICT
;
2083 * Do nothing but return GOOD status.
2090 * Perform the Service Action REGISTER on the Initiator
2091 * Port Endpoint that the PRO was received from on the
2092 * Logical Unit of the SCSI device server.
2094 if (core_scsi3_alloc_registration(cmd
->se_dev
,
2095 se_sess
->se_node_acl
, se_deve
, isid_ptr
,
2096 sa_res_key
, all_tg_pt
, aptpl
,
2098 pr_err("Unable to allocate"
2099 " struct t10_pr_registration\n");
2100 return TCM_INVALID_PARAMETER_LIST
;
2104 * Register both the Initiator port that received
2105 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2106 * TransportID from Parameter list and loop through
2107 * fabric dependent parameter list while calling
2108 * logic from of core_scsi3_alloc_registration() for
2109 * each TransportID provided SCSI Initiator Port/Device
2111 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2112 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2117 * Nothing left to do for the APTPL=0 case.
2120 pr_tmpl
->pr_aptpl_active
= 0;
2121 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2122 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2127 * Locate the newly allocated local I_T Nexus *pr_reg, and
2128 * update the APTPL metadata information using its
2129 * preallocated *pr_reg->pr_aptpl_buf.
2131 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2132 se_sess
->se_node_acl
, se_sess
);
2134 if (core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2135 &pr_reg
->pr_aptpl_buf
[0],
2136 pr_tmpl
->pr_aptpl_buf_len
)) {
2137 pr_tmpl
->pr_aptpl_active
= 1;
2138 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2141 goto out_put_pr_reg
;
2145 * Locate the existing *pr_reg via struct se_node_acl pointers
2148 type
= pr_reg
->pr_res_type
;
2151 if (res_key
!= pr_reg
->pr_res_key
) {
2152 pr_err("SPC-3 PR REGISTER: Received"
2153 " res_key: 0x%016Lx does not match"
2154 " existing SA REGISTER res_key:"
2155 " 0x%016Lx\n", res_key
,
2156 pr_reg
->pr_res_key
);
2157 ret
= TCM_RESERVATION_CONFLICT
;
2158 goto out_put_pr_reg
;
2163 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2164 " set while sa_res_key=0\n");
2165 ret
= TCM_INVALID_PARAMETER_LIST
;
2166 goto out_put_pr_reg
;
2170 * An existing ALL_TG_PT=1 registration being released
2171 * must also set ALL_TG_PT=1 in the incoming PROUT.
2173 if (pr_reg
->pr_reg_all_tg_pt
&& !(all_tg_pt
)) {
2174 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2175 " registration exists, but ALL_TG_PT=1 bit not"
2176 " present in received PROUT\n");
2177 ret
= TCM_INVALID_CDB_FIELD
;
2178 goto out_put_pr_reg
;
2182 * Allocate APTPL metadata buffer used for UNREGISTER ops
2185 pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
,
2187 if (!pr_aptpl_buf
) {
2188 pr_err("Unable to allocate"
2190 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2191 goto out_put_pr_reg
;
2196 * sa_res_key=0 Unregister Reservation Key for registered I_T
2197 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2201 pr_holder
= core_scsi3_check_implict_release(
2202 cmd
->se_dev
, pr_reg
);
2203 if (pr_holder
< 0) {
2204 kfree(pr_aptpl_buf
);
2205 ret
= TCM_RESERVATION_CONFLICT
;
2206 goto out_put_pr_reg
;
2209 spin_lock(&pr_tmpl
->registration_lock
);
2211 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2212 * and matching pr_res_key.
2214 if (pr_reg
->pr_reg_all_tg_pt
) {
2215 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2216 &pr_tmpl
->registration_list
,
2219 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2221 if (pr_reg_p
->pr_res_key
!= res_key
)
2223 if (pr_reg
== pr_reg_p
)
2225 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2226 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2229 __core_scsi3_free_registration(dev
,
2235 * Release the calling I_T Nexus registration now..
2237 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
, NULL
, 1);
2240 * From spc4r17, section 5.7.11.3 Unregistering
2242 * If the persistent reservation is a registrants only
2243 * type, the device server shall establish a unit
2244 * attention condition for the initiator port associated
2245 * with every registered I_T nexus except for the I_T
2246 * nexus on which the PERSISTENT RESERVE OUT command was
2247 * received, with the additional sense code set to
2248 * RESERVATIONS RELEASED.
2251 (type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
||
2252 type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
)) {
2253 list_for_each_entry(pr_reg_p
,
2254 &pr_tmpl
->registration_list
,
2257 core_scsi3_ua_allocate(
2258 pr_reg_p
->pr_reg_nacl
,
2259 pr_reg_p
->pr_res_mapped_lun
,
2261 ASCQ_2AH_RESERVATIONS_RELEASED
);
2264 spin_unlock(&pr_tmpl
->registration_lock
);
2267 pr_tmpl
->pr_aptpl_active
= 0;
2268 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2269 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2270 " for UNREGISTER\n");
2274 if (!core_scsi3_update_and_write_aptpl(dev
, &pr_aptpl_buf
[0],
2275 pr_tmpl
->pr_aptpl_buf_len
)) {
2276 pr_tmpl
->pr_aptpl_active
= 1;
2277 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2278 " for UNREGISTER\n");
2281 goto out_free_aptpl_buf
;
2285 * Increment PRgeneration counter for struct se_device"
2286 * upon a successful REGISTER, see spc4r17 section 6.3.2
2287 * READ_KEYS service action.
2289 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(cmd
->se_dev
);
2290 pr_reg
->pr_res_key
= sa_res_key
;
2291 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2292 " Key for %s to: 0x%016Lx PRgeneration:"
2293 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2294 (ignore_key
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2295 pr_reg
->pr_reg_nacl
->initiatorname
,
2296 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2299 pr_tmpl
->pr_aptpl_active
= 0;
2300 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2301 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2304 goto out_put_pr_reg
;
2307 if (!core_scsi3_update_and_write_aptpl(dev
, &pr_aptpl_buf
[0],
2308 pr_tmpl
->pr_aptpl_buf_len
)) {
2309 pr_tmpl
->pr_aptpl_active
= 1;
2310 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2315 kfree(pr_aptpl_buf
);
2318 core_scsi3_put_pr_reg(pr_reg
);
2322 unsigned char *core_scsi3_pr_dump_type(int type
)
2325 case PR_TYPE_WRITE_EXCLUSIVE
:
2326 return "Write Exclusive Access";
2327 case PR_TYPE_EXCLUSIVE_ACCESS
:
2328 return "Exclusive Access";
2329 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2330 return "Write Exclusive Access, Registrants Only";
2331 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2332 return "Exclusive Access, Registrants Only";
2333 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2334 return "Write Exclusive Access, All Registrants";
2335 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2336 return "Exclusive Access, All Registrants";
2341 return "Unknown SPC-3 PR Type";
2344 static sense_reason_t
2345 core_scsi3_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
)
2347 struct se_device
*dev
= cmd
->se_dev
;
2348 struct se_session
*se_sess
= cmd
->se_sess
;
2349 struct se_lun
*se_lun
= cmd
->se_lun
;
2350 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2351 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2352 char i_buf
[PR_REG_ISID_ID_LEN
];
2356 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2358 if (!se_sess
|| !se_lun
) {
2359 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2360 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2363 * Locate the existing *pr_reg via struct se_node_acl pointers
2365 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2368 pr_err("SPC-3 PR: Unable to locate"
2369 " PR_REGISTERED *pr_reg for RESERVE\n");
2370 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2373 * From spc4r17 Section 5.7.9: Reserving:
2375 * An application client creates a persistent reservation by issuing
2376 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2377 * a registered I_T nexus with the following parameters:
2378 * a) RESERVATION KEY set to the value of the reservation key that is
2379 * registered with the logical unit for the I_T nexus; and
2381 if (res_key
!= pr_reg
->pr_res_key
) {
2382 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2383 " does not match existing SA REGISTER res_key:"
2384 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2385 ret
= TCM_RESERVATION_CONFLICT
;
2386 goto out_put_pr_reg
;
2389 * From spc4r17 Section 5.7.9: Reserving:
2392 * b) TYPE field and SCOPE field set to the persistent reservation
2395 * Only one persistent reservation is allowed at a time per logical unit
2396 * and that persistent reservation has a scope of LU_SCOPE.
2398 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2399 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2400 ret
= TCM_INVALID_PARAMETER_LIST
;
2401 goto out_put_pr_reg
;
2404 * See if we have an existing PR reservation holder pointer at
2405 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2408 spin_lock(&dev
->dev_reservation_lock
);
2409 pr_res_holder
= dev
->dev_pr_res_holder
;
2410 if (pr_res_holder
) {
2412 * From spc4r17 Section 5.7.9: Reserving:
2414 * If the device server receives a PERSISTENT RESERVE OUT
2415 * command from an I_T nexus other than a persistent reservation
2416 * holder (see 5.7.10) that attempts to create a persistent
2417 * reservation when a persistent reservation already exists for
2418 * the logical unit, then the command shall be completed with
2419 * RESERVATION CONFLICT status.
2421 if (pr_res_holder
!= pr_reg
) {
2422 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2423 pr_err("SPC-3 PR: Attempted RESERVE from"
2424 " [%s]: %s while reservation already held by"
2425 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2426 cmd
->se_tfo
->get_fabric_name(),
2427 se_sess
->se_node_acl
->initiatorname
,
2428 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2429 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2431 spin_unlock(&dev
->dev_reservation_lock
);
2432 ret
= TCM_RESERVATION_CONFLICT
;
2433 goto out_put_pr_reg
;
2436 * From spc4r17 Section 5.7.9: Reserving:
2438 * If a persistent reservation holder attempts to modify the
2439 * type or scope of an existing persistent reservation, the
2440 * command shall be completed with RESERVATION CONFLICT status.
2442 if ((pr_res_holder
->pr_res_type
!= type
) ||
2443 (pr_res_holder
->pr_res_scope
!= scope
)) {
2444 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2445 pr_err("SPC-3 PR: Attempted RESERVE from"
2446 " [%s]: %s trying to change TYPE and/or SCOPE,"
2447 " while reservation already held by [%s]: %s,"
2448 " returning RESERVATION_CONFLICT\n",
2449 cmd
->se_tfo
->get_fabric_name(),
2450 se_sess
->se_node_acl
->initiatorname
,
2451 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2452 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2454 spin_unlock(&dev
->dev_reservation_lock
);
2455 ret
= TCM_RESERVATION_CONFLICT
;
2456 goto out_put_pr_reg
;
2459 * From spc4r17 Section 5.7.9: Reserving:
2461 * If the device server receives a PERSISTENT RESERVE OUT
2462 * command with RESERVE service action where the TYPE field and
2463 * the SCOPE field contain the same values as the existing type
2464 * and scope from a persistent reservation holder, it shall not
2465 * make any change to the existing persistent reservation and
2466 * shall completethe command with GOOD status.
2468 spin_unlock(&dev
->dev_reservation_lock
);
2470 goto out_put_pr_reg
;
2473 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2474 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2476 pr_reg
->pr_res_scope
= scope
;
2477 pr_reg
->pr_res_type
= type
;
2478 pr_reg
->pr_res_holder
= 1;
2479 dev
->dev_pr_res_holder
= pr_reg
;
2480 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2481 PR_REG_ISID_ID_LEN
);
2483 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2484 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2485 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2486 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2487 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2488 cmd
->se_tfo
->get_fabric_name(),
2489 se_sess
->se_node_acl
->initiatorname
,
2490 (prf_isid
) ? &i_buf
[0] : "");
2491 spin_unlock(&dev
->dev_reservation_lock
);
2493 if (pr_tmpl
->pr_aptpl_active
) {
2494 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2495 &pr_reg
->pr_aptpl_buf
[0],
2496 pr_tmpl
->pr_aptpl_buf_len
)) {
2497 pr_debug("SPC-3 PR: Updated APTPL metadata"
2504 core_scsi3_put_pr_reg(pr_reg
);
2508 static sense_reason_t
2509 core_scsi3_emulate_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
,
2513 case PR_TYPE_WRITE_EXCLUSIVE
:
2514 case PR_TYPE_EXCLUSIVE_ACCESS
:
2515 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2516 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2517 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2518 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2519 return core_scsi3_pro_reserve(cmd
, type
, scope
, res_key
);
2521 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2523 return TCM_INVALID_CDB_FIELD
;
2528 * Called with struct se_device->dev_reservation_lock held.
2530 static void __core_scsi3_complete_pro_release(
2531 struct se_device
*dev
,
2532 struct se_node_acl
*se_nacl
,
2533 struct t10_pr_registration
*pr_reg
,
2536 struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2537 char i_buf
[PR_REG_ISID_ID_LEN
];
2540 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2541 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2542 PR_REG_ISID_ID_LEN
);
2544 * Go ahead and release the current PR reservation holder.
2546 dev
->dev_pr_res_holder
= NULL
;
2548 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2549 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2550 tfo
->get_fabric_name(), (explict
) ? "explict" : "implict",
2551 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
2552 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2553 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2554 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2555 (prf_isid
) ? &i_buf
[0] : "");
2557 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2559 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2562 static sense_reason_t
2563 core_scsi3_emulate_pro_release(struct se_cmd
*cmd
, int type
, int scope
,
2566 struct se_device
*dev
= cmd
->se_dev
;
2567 struct se_session
*se_sess
= cmd
->se_sess
;
2568 struct se_lun
*se_lun
= cmd
->se_lun
;
2569 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2570 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2572 sense_reason_t ret
= 0;
2574 if (!se_sess
|| !se_lun
) {
2575 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2576 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2579 * Locate the existing *pr_reg via struct se_node_acl pointers
2581 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2583 pr_err("SPC-3 PR: Unable to locate"
2584 " PR_REGISTERED *pr_reg for RELEASE\n");
2585 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2588 * From spc4r17 Section 5.7.11.2 Releasing:
2590 * If there is no persistent reservation or in response to a persistent
2591 * reservation release request from a registered I_T nexus that is not a
2592 * persistent reservation holder (see 5.7.10), the device server shall
2595 * a) Not release the persistent reservation, if any;
2596 * b) Not remove any registrations; and
2597 * c) Complete the command with GOOD status.
2599 spin_lock(&dev
->dev_reservation_lock
);
2600 pr_res_holder
= dev
->dev_pr_res_holder
;
2601 if (!pr_res_holder
) {
2603 * No persistent reservation, return GOOD status.
2605 spin_unlock(&dev
->dev_reservation_lock
);
2606 goto out_put_pr_reg
;
2608 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2609 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
2612 if ((all_reg
== 0) && (pr_res_holder
!= pr_reg
)) {
2614 * Non 'All Registrants' PR Type cases..
2615 * Release request from a registered I_T nexus that is not a
2616 * persistent reservation holder. return GOOD status.
2618 spin_unlock(&dev
->dev_reservation_lock
);
2619 goto out_put_pr_reg
;
2623 * From spc4r17 Section 5.7.11.2 Releasing:
2625 * Only the persistent reservation holder (see 5.7.10) is allowed to
2626 * release a persistent reservation.
2628 * An application client releases the persistent reservation by issuing
2629 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2630 * an I_T nexus that is a persistent reservation holder with the
2631 * following parameters:
2633 * a) RESERVATION KEY field set to the value of the reservation key
2634 * that is registered with the logical unit for the I_T nexus;
2636 if (res_key
!= pr_reg
->pr_res_key
) {
2637 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2638 " does not match existing SA REGISTER res_key:"
2639 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2640 spin_unlock(&dev
->dev_reservation_lock
);
2641 ret
= TCM_RESERVATION_CONFLICT
;
2642 goto out_put_pr_reg
;
2645 * From spc4r17 Section 5.7.11.2 Releasing and above:
2647 * b) TYPE field and SCOPE field set to match the persistent
2648 * reservation being released.
2650 if ((pr_res_holder
->pr_res_type
!= type
) ||
2651 (pr_res_holder
->pr_res_scope
!= scope
)) {
2652 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2653 pr_err("SPC-3 PR RELEASE: Attempted to release"
2654 " reservation from [%s]: %s with different TYPE "
2655 "and/or SCOPE while reservation already held by"
2656 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2657 cmd
->se_tfo
->get_fabric_name(),
2658 se_sess
->se_node_acl
->initiatorname
,
2659 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2660 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2662 spin_unlock(&dev
->dev_reservation_lock
);
2663 ret
= TCM_RESERVATION_CONFLICT
;
2664 goto out_put_pr_reg
;
2667 * In response to a persistent reservation release request from the
2668 * persistent reservation holder the device server shall perform a
2669 * release by doing the following as an uninterrupted series of actions:
2670 * a) Release the persistent reservation;
2671 * b) Not remove any registration(s);
2672 * c) If the released persistent reservation is a registrants only type
2673 * or all registrants type persistent reservation,
2674 * the device server shall establish a unit attention condition for
2675 * the initiator port associated with every regis-
2676 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2677 * RESERVE OUT command with RELEASE service action was received,
2678 * with the additional sense code set to RESERVATIONS RELEASED; and
2679 * d) If the persistent reservation is of any other type, the device
2680 * server shall not establish a unit attention condition.
2682 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2685 spin_unlock(&dev
->dev_reservation_lock
);
2687 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2688 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2689 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2690 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2692 * If no UNIT ATTENTION conditions will be established for
2693 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2694 * go ahead and check for APTPL=1 update+write below
2699 spin_lock(&pr_tmpl
->registration_lock
);
2700 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2703 * Do not establish a UNIT ATTENTION condition
2704 * for the calling I_T Nexus
2706 if (pr_reg_p
== pr_reg
)
2709 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2710 pr_reg_p
->pr_res_mapped_lun
,
2711 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2713 spin_unlock(&pr_tmpl
->registration_lock
);
2716 if (pr_tmpl
->pr_aptpl_active
) {
2717 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2718 &pr_reg
->pr_aptpl_buf
[0], pr_tmpl
->pr_aptpl_buf_len
)) {
2719 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2723 core_scsi3_put_pr_reg(pr_reg
);
2727 static sense_reason_t
2728 core_scsi3_emulate_pro_clear(struct se_cmd
*cmd
, u64 res_key
)
2730 struct se_device
*dev
= cmd
->se_dev
;
2731 struct se_node_acl
*pr_reg_nacl
;
2732 struct se_session
*se_sess
= cmd
->se_sess
;
2733 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2734 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2735 u32 pr_res_mapped_lun
= 0;
2736 int calling_it_nexus
= 0;
2738 * Locate the existing *pr_reg via struct se_node_acl pointers
2740 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2741 se_sess
->se_node_acl
, se_sess
);
2743 pr_err("SPC-3 PR: Unable to locate"
2744 " PR_REGISTERED *pr_reg for CLEAR\n");
2745 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2748 * From spc4r17 section 5.7.11.6, Clearing:
2750 * Any application client may release the persistent reservation and
2751 * remove all registrations from a device server by issuing a
2752 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2753 * registered I_T nexus with the following parameter:
2755 * a) RESERVATION KEY field set to the value of the reservation key
2756 * that is registered with the logical unit for the I_T nexus.
2758 if (res_key
!= pr_reg_n
->pr_res_key
) {
2759 pr_err("SPC-3 PR REGISTER: Received"
2760 " res_key: 0x%016Lx does not match"
2761 " existing SA REGISTER res_key:"
2762 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2763 core_scsi3_put_pr_reg(pr_reg_n
);
2764 return TCM_RESERVATION_CONFLICT
;
2767 * a) Release the persistent reservation, if any;
2769 spin_lock(&dev
->dev_reservation_lock
);
2770 pr_res_holder
= dev
->dev_pr_res_holder
;
2771 if (pr_res_holder
) {
2772 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2773 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2776 spin_unlock(&dev
->dev_reservation_lock
);
2778 * b) Remove all registration(s) (see spc4r17 5.7.7);
2780 spin_lock(&pr_tmpl
->registration_lock
);
2781 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2782 &pr_tmpl
->registration_list
, pr_reg_list
) {
2784 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2785 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2786 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2787 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2790 * e) Establish a unit attention condition for the initiator
2791 * port associated with every registered I_T nexus other
2792 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2793 * command with CLEAR service action was received, with the
2794 * additional sense code set to RESERVATIONS PREEMPTED.
2796 if (!calling_it_nexus
)
2797 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2798 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2800 spin_unlock(&pr_tmpl
->registration_lock
);
2802 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2803 cmd
->se_tfo
->get_fabric_name());
2805 if (pr_tmpl
->pr_aptpl_active
) {
2806 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2807 pr_debug("SPC-3 PR: Updated APTPL metadata"
2811 core_scsi3_pr_generation(dev
);
2816 * Called with struct se_device->dev_reservation_lock held.
2818 static void __core_scsi3_complete_pro_preempt(
2819 struct se_device
*dev
,
2820 struct t10_pr_registration
*pr_reg
,
2821 struct list_head
*preempt_and_abort_list
,
2826 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2827 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2828 char i_buf
[PR_REG_ISID_ID_LEN
];
2831 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2832 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2833 PR_REG_ISID_ID_LEN
);
2835 * Do an implict RELEASE of the existing reservation.
2837 if (dev
->dev_pr_res_holder
)
2838 __core_scsi3_complete_pro_release(dev
, nacl
,
2839 dev
->dev_pr_res_holder
, 0);
2841 dev
->dev_pr_res_holder
= pr_reg
;
2842 pr_reg
->pr_res_holder
= 1;
2843 pr_reg
->pr_res_type
= type
;
2844 pr_reg
->pr_res_scope
= scope
;
2846 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2847 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2848 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2849 core_scsi3_pr_dump_type(type
),
2850 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2851 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2852 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2853 nacl
->initiatorname
, (prf_isid
) ? &i_buf
[0] : "");
2855 * For PREEMPT_AND_ABORT, add the preempting reservation's
2856 * struct t10_pr_registration to the list that will be compared
2857 * against received CDBs..
2859 if (preempt_and_abort_list
)
2860 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2861 preempt_and_abort_list
);
2864 static void core_scsi3_release_preempt_and_abort(
2865 struct list_head
*preempt_and_abort_list
,
2866 struct t10_pr_registration
*pr_reg_holder
)
2868 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2870 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2871 pr_reg_abort_list
) {
2873 list_del(&pr_reg
->pr_reg_abort_list
);
2874 if (pr_reg_holder
== pr_reg
)
2876 if (pr_reg
->pr_res_holder
) {
2877 pr_warn("pr_reg->pr_res_holder still set\n");
2881 pr_reg
->pr_reg_deve
= NULL
;
2882 pr_reg
->pr_reg_nacl
= NULL
;
2883 kfree(pr_reg
->pr_aptpl_buf
);
2884 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2888 static sense_reason_t
2889 core_scsi3_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
,
2890 u64 sa_res_key
, int abort
)
2892 struct se_device
*dev
= cmd
->se_dev
;
2893 struct se_node_acl
*pr_reg_nacl
;
2894 struct se_session
*se_sess
= cmd
->se_sess
;
2895 LIST_HEAD(preempt_and_abort_list
);
2896 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2897 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2898 u32 pr_res_mapped_lun
= 0;
2899 int all_reg
= 0, calling_it_nexus
= 0, released_regs
= 0;
2900 int prh_type
= 0, prh_scope
= 0;
2903 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2905 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2908 pr_err("SPC-3 PR: Unable to locate"
2909 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2910 (abort
) ? "_AND_ABORT" : "");
2911 return TCM_RESERVATION_CONFLICT
;
2913 if (pr_reg_n
->pr_res_key
!= res_key
) {
2914 core_scsi3_put_pr_reg(pr_reg_n
);
2915 return TCM_RESERVATION_CONFLICT
;
2917 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2918 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2919 core_scsi3_put_pr_reg(pr_reg_n
);
2920 return TCM_INVALID_PARAMETER_LIST
;
2923 spin_lock(&dev
->dev_reservation_lock
);
2924 pr_res_holder
= dev
->dev_pr_res_holder
;
2925 if (pr_res_holder
&&
2926 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2927 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
2930 if (!all_reg
&& !sa_res_key
) {
2931 spin_unlock(&dev
->dev_reservation_lock
);
2932 core_scsi3_put_pr_reg(pr_reg_n
);
2933 return TCM_INVALID_PARAMETER_LIST
;
2936 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2938 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2939 * persistent reservation holder or there is no persistent reservation
2940 * holder (i.e., there is no persistent reservation), then the device
2941 * server shall perform a preempt by doing the following in an
2942 * uninterrupted series of actions. (See below..)
2944 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
2946 * No existing or SA Reservation Key matching reservations..
2948 * PROUT SA PREEMPT with All Registrant type reservations are
2949 * allowed to be processed without a matching SA Reservation Key
2951 spin_lock(&pr_tmpl
->registration_lock
);
2952 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2953 &pr_tmpl
->registration_list
, pr_reg_list
) {
2955 * Removing of registrations in non all registrants
2956 * type reservations without a matching SA reservation
2959 * a) Remove the registrations for all I_T nexuses
2960 * specified by the SERVICE ACTION RESERVATION KEY
2962 * b) Ignore the contents of the SCOPE and TYPE fields;
2963 * c) Process tasks as defined in 5.7.1; and
2964 * d) Establish a unit attention condition for the
2965 * initiator port associated with every I_T nexus
2966 * that lost its registration other than the I_T
2967 * nexus on which the PERSISTENT RESERVE OUT command
2968 * was received, with the additional sense code set
2969 * to REGISTRATIONS PREEMPTED.
2972 if (pr_reg
->pr_res_key
!= sa_res_key
)
2975 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2976 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2977 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2978 __core_scsi3_free_registration(dev
, pr_reg
,
2979 (abort
) ? &preempt_and_abort_list
:
2980 NULL
, calling_it_nexus
);
2984 * Case for any existing all registrants type
2985 * reservation, follow logic in spc4r17 section
2986 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2988 * For a ZERO SA Reservation key, release
2989 * all other registrations and do an implict
2990 * release of active persistent reservation.
2992 * For a non-ZERO SA Reservation key, only
2993 * release the matching reservation key from
2997 (pr_reg
->pr_res_key
!= sa_res_key
))
3000 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3001 if (calling_it_nexus
)
3004 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3005 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3006 __core_scsi3_free_registration(dev
, pr_reg
,
3007 (abort
) ? &preempt_and_abort_list
:
3011 if (!calling_it_nexus
)
3012 core_scsi3_ua_allocate(pr_reg_nacl
,
3013 pr_res_mapped_lun
, 0x2A,
3014 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3016 spin_unlock(&pr_tmpl
->registration_lock
);
3018 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3019 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3020 * RESERVATION KEY field to a value that does not match any
3021 * registered reservation key, then the device server shall
3022 * complete the command with RESERVATION CONFLICT status.
3024 if (!released_regs
) {
3025 spin_unlock(&dev
->dev_reservation_lock
);
3026 core_scsi3_put_pr_reg(pr_reg_n
);
3027 return TCM_RESERVATION_CONFLICT
;
3030 * For an existing all registrants type reservation
3031 * with a zero SA rservation key, preempt the existing
3032 * reservation with the new PR type and scope.
3034 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
3035 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3036 (abort
) ? &preempt_and_abort_list
: NULL
,
3037 type
, scope
, abort
);
3040 core_scsi3_release_preempt_and_abort(
3041 &preempt_and_abort_list
, pr_reg_n
);
3043 spin_unlock(&dev
->dev_reservation_lock
);
3045 if (pr_tmpl
->pr_aptpl_active
) {
3046 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3047 &pr_reg_n
->pr_aptpl_buf
[0],
3048 pr_tmpl
->pr_aptpl_buf_len
)) {
3049 pr_debug("SPC-3 PR: Updated APTPL"
3050 " metadata for PREEMPT%s\n", (abort
) ?
3055 core_scsi3_put_pr_reg(pr_reg_n
);
3056 core_scsi3_pr_generation(cmd
->se_dev
);
3060 * The PREEMPTing SA reservation key matches that of the
3061 * existing persistent reservation, first, we check if
3062 * we are preempting our own reservation.
3063 * From spc4r17, section 5.7.11.4.3 Preempting
3064 * persistent reservations and registration handling
3066 * If an all registrants persistent reservation is not
3067 * present, it is not an error for the persistent
3068 * reservation holder to preempt itself (i.e., a
3069 * PERSISTENT RESERVE OUT with a PREEMPT service action
3070 * or a PREEMPT AND ABORT service action with the
3071 * SERVICE ACTION RESERVATION KEY value equal to the
3072 * persistent reservation holder's reservation key that
3073 * is received from the persistent reservation holder).
3074 * In that case, the device server shall establish the
3075 * new persistent reservation and maintain the
3078 prh_type
= pr_res_holder
->pr_res_type
;
3079 prh_scope
= pr_res_holder
->pr_res_scope
;
3081 * If the SERVICE ACTION RESERVATION KEY field identifies a
3082 * persistent reservation holder (see 5.7.10), the device
3083 * server shall perform a preempt by doing the following as
3084 * an uninterrupted series of actions:
3086 * a) Release the persistent reservation for the holder
3087 * identified by the SERVICE ACTION RESERVATION KEY field;
3089 if (pr_reg_n
!= pr_res_holder
)
3090 __core_scsi3_complete_pro_release(dev
,
3091 pr_res_holder
->pr_reg_nacl
,
3092 dev
->dev_pr_res_holder
, 0);
3094 * b) Remove the registrations for all I_T nexuses identified
3095 * by the SERVICE ACTION RESERVATION KEY field, except the
3096 * I_T nexus that is being used for the PERSISTENT RESERVE
3097 * OUT command. If an all registrants persistent reservation
3098 * is present and the SERVICE ACTION RESERVATION KEY field
3099 * is set to zero, then all registrations shall be removed
3100 * except for that of the I_T nexus that is being used for
3101 * the PERSISTENT RESERVE OUT command;
3103 spin_lock(&pr_tmpl
->registration_lock
);
3104 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3105 &pr_tmpl
->registration_list
, pr_reg_list
) {
3107 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3108 if (calling_it_nexus
)
3111 if (pr_reg
->pr_res_key
!= sa_res_key
)
3114 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3115 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3116 __core_scsi3_free_registration(dev
, pr_reg
,
3117 (abort
) ? &preempt_and_abort_list
: NULL
,
3120 * e) Establish a unit attention condition for the initiator
3121 * port associated with every I_T nexus that lost its
3122 * persistent reservation and/or registration, with the
3123 * additional sense code set to REGISTRATIONS PREEMPTED;
3125 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3126 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3128 spin_unlock(&pr_tmpl
->registration_lock
);
3130 * c) Establish a persistent reservation for the preempting
3131 * I_T nexus using the contents of the SCOPE and TYPE fields;
3133 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3134 (abort
) ? &preempt_and_abort_list
: NULL
,
3135 type
, scope
, abort
);
3137 * d) Process tasks as defined in 5.7.1;
3139 * f) If the type or scope has changed, then for every I_T nexus
3140 * whose reservation key was not removed, except for the I_T
3141 * nexus on which the PERSISTENT RESERVE OUT command was
3142 * received, the device server shall establish a unit
3143 * attention condition for the initiator port associated with
3144 * that I_T nexus, with the additional sense code set to
3145 * RESERVATIONS RELEASED. If the type or scope have not
3146 * changed, then no unit attention condition(s) shall be
3147 * established for this reason.
3149 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3150 spin_lock(&pr_tmpl
->registration_lock
);
3151 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3152 &pr_tmpl
->registration_list
, pr_reg_list
) {
3154 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3155 if (calling_it_nexus
)
3158 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3159 pr_reg
->pr_res_mapped_lun
, 0x2A,
3160 ASCQ_2AH_RESERVATIONS_RELEASED
);
3162 spin_unlock(&pr_tmpl
->registration_lock
);
3164 spin_unlock(&dev
->dev_reservation_lock
);
3166 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3167 * All received CDBs for the matching existing reservation and
3168 * registrations undergo ABORT_TASK logic.
3170 * From there, core_scsi3_release_preempt_and_abort() will
3171 * release every registration in the list (which have already
3172 * been removed from the primary pr_reg list), except the
3173 * new persistent reservation holder, the calling Initiator Port.
3176 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3177 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3181 if (pr_tmpl
->pr_aptpl_active
) {
3182 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3183 &pr_reg_n
->pr_aptpl_buf
[0],
3184 pr_tmpl
->pr_aptpl_buf_len
)) {
3185 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3186 "%s\n", abort
? "_AND_ABORT" : "");
3190 core_scsi3_put_pr_reg(pr_reg_n
);
3191 core_scsi3_pr_generation(cmd
->se_dev
);
3195 static sense_reason_t
3196 core_scsi3_emulate_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
,
3197 u64 res_key
, u64 sa_res_key
, int abort
)
3200 case PR_TYPE_WRITE_EXCLUSIVE
:
3201 case PR_TYPE_EXCLUSIVE_ACCESS
:
3202 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3203 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3204 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3205 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3206 return core_scsi3_pro_preempt(cmd
, type
, scope
, res_key
,
3209 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3210 " Type: 0x%02x\n", (abort
) ? "_AND_ABORT" : "", type
);
3211 return TCM_INVALID_CDB_FIELD
;
3216 static sense_reason_t
3217 core_scsi3_emulate_pro_register_and_move(struct se_cmd
*cmd
, u64 res_key
,
3218 u64 sa_res_key
, int aptpl
, int unreg
)
3220 struct se_session
*se_sess
= cmd
->se_sess
;
3221 struct se_device
*dev
= cmd
->se_dev
;
3222 struct se_dev_entry
*dest_se_deve
= NULL
;
3223 struct se_lun
*se_lun
= cmd
->se_lun
;
3224 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3225 struct se_port
*se_port
;
3226 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3227 struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3228 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3229 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3231 unsigned char *initiator_str
;
3232 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
3233 u32 tid_len
, tmp_tid_len
;
3234 int new_reg
= 0, type
, scope
, matching_iname
, prf_isid
;
3236 unsigned short rtpi
;
3237 unsigned char proto_ident
;
3239 if (!se_sess
|| !se_lun
) {
3240 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3241 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3244 memset(dest_iport
, 0, 64);
3245 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3246 se_tpg
= se_sess
->se_tpg
;
3247 tf_ops
= se_tpg
->se_tpg_tfo
;
3249 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3250 * Register behaviors for a REGISTER AND MOVE service action
3252 * Locate the existing *pr_reg via struct se_node_acl pointers
3254 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3257 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3258 " *pr_reg for REGISTER_AND_MOVE\n");
3259 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3262 * The provided reservation key much match the existing reservation key
3263 * provided during this initiator's I_T nexus registration.
3265 if (res_key
!= pr_reg
->pr_res_key
) {
3266 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3267 " res_key: 0x%016Lx does not match existing SA REGISTER"
3268 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3269 ret
= TCM_RESERVATION_CONFLICT
;
3270 goto out_put_pr_reg
;
3273 * The service active reservation key needs to be non zero
3276 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3278 ret
= TCM_INVALID_PARAMETER_LIST
;
3279 goto out_put_pr_reg
;
3283 * Determine the Relative Target Port Identifier where the reservation
3284 * will be moved to for the TransportID containing SCSI initiator WWN
3287 buf
= transport_kmap_data_sg(cmd
);
3289 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3290 goto out_put_pr_reg
;
3293 rtpi
= (buf
[18] & 0xff) << 8;
3294 rtpi
|= buf
[19] & 0xff;
3295 tid_len
= (buf
[20] & 0xff) << 24;
3296 tid_len
|= (buf
[21] & 0xff) << 16;
3297 tid_len
|= (buf
[22] & 0xff) << 8;
3298 tid_len
|= buf
[23] & 0xff;
3299 transport_kunmap_data_sg(cmd
);
3302 if ((tid_len
+ 24) != cmd
->data_length
) {
3303 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3304 " does not equal CDB data_length: %u\n", tid_len
,
3306 ret
= TCM_INVALID_PARAMETER_LIST
;
3307 goto out_put_pr_reg
;
3310 spin_lock(&dev
->se_port_lock
);
3311 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3312 if (se_port
->sep_rtpi
!= rtpi
)
3314 dest_se_tpg
= se_port
->sep_tpg
;
3317 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3321 atomic_inc(&dest_se_tpg
->tpg_pr_ref_count
);
3322 smp_mb__after_atomic_inc();
3323 spin_unlock(&dev
->se_port_lock
);
3325 if (core_scsi3_tpg_depend_item(dest_se_tpg
)) {
3326 pr_err("core_scsi3_tpg_depend_item() failed"
3327 " for dest_se_tpg\n");
3328 atomic_dec(&dest_se_tpg
->tpg_pr_ref_count
);
3329 smp_mb__after_atomic_dec();
3330 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3331 goto out_put_pr_reg
;
3334 spin_lock(&dev
->se_port_lock
);
3337 spin_unlock(&dev
->se_port_lock
);
3339 if (!dest_se_tpg
|| !dest_tf_ops
) {
3340 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3341 " fabric ops from Relative Target Port Identifier:"
3343 ret
= TCM_INVALID_PARAMETER_LIST
;
3344 goto out_put_pr_reg
;
3347 buf
= transport_kmap_data_sg(cmd
);
3349 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3350 goto out_put_pr_reg
;
3352 proto_ident
= (buf
[24] & 0x0f);
3354 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3355 " 0x%02x\n", proto_ident
);
3357 if (proto_ident
!= dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
)) {
3358 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3359 " proto_ident: 0x%02x does not match ident: 0x%02x"
3360 " from fabric: %s\n", proto_ident
,
3361 dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
),
3362 dest_tf_ops
->get_fabric_name());
3363 ret
= TCM_INVALID_PARAMETER_LIST
;
3366 if (dest_tf_ops
->tpg_parse_pr_out_transport_id
== NULL
) {
3367 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3368 " containg a valid tpg_parse_pr_out_transport_id"
3369 " function pointer\n");
3370 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3373 initiator_str
= dest_tf_ops
->tpg_parse_pr_out_transport_id(dest_se_tpg
,
3374 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3375 if (!initiator_str
) {
3376 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3377 " initiator_str from Transport ID\n");
3378 ret
= TCM_INVALID_PARAMETER_LIST
;
3382 transport_kunmap_data_sg(cmd
);
3385 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3386 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3387 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3390 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3391 * action specifies a TransportID that is the same as the initiator port
3392 * of the I_T nexus for the command received, then the command shall
3393 * be terminated with CHECK CONDITION status, with the sense key set to
3394 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3395 * IN PARAMETER LIST.
3397 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3398 matching_iname
= (!strcmp(initiator_str
,
3399 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3400 if (!matching_iname
)
3401 goto after_iport_check
;
3403 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3404 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3405 " matches: %s on received I_T Nexus\n", initiator_str
,
3406 pr_reg_nacl
->initiatorname
);
3407 ret
= TCM_INVALID_PARAMETER_LIST
;
3410 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3411 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3412 " matches: %s %s on received I_T Nexus\n",
3413 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3414 pr_reg
->pr_reg_isid
);
3415 ret
= TCM_INVALID_PARAMETER_LIST
;
3420 * Locate the destination struct se_node_acl from the received Transport ID
3422 spin_lock_irq(&dest_se_tpg
->acl_node_lock
);
3423 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3425 if (dest_node_acl
) {
3426 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
3427 smp_mb__after_atomic_inc();
3429 spin_unlock_irq(&dest_se_tpg
->acl_node_lock
);
3431 if (!dest_node_acl
) {
3432 pr_err("Unable to locate %s dest_node_acl for"
3433 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3435 ret
= TCM_INVALID_PARAMETER_LIST
;
3439 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
3440 pr_err("core_scsi3_nodeacl_depend_item() for"
3441 " dest_node_acl\n");
3442 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
3443 smp_mb__after_atomic_dec();
3444 dest_node_acl
= NULL
;
3445 ret
= TCM_INVALID_PARAMETER_LIST
;
3449 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3450 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3451 dest_node_acl
->initiatorname
);
3454 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3457 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3458 if (!dest_se_deve
) {
3459 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3460 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3461 ret
= TCM_INVALID_PARAMETER_LIST
;
3465 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
3466 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3467 atomic_dec(&dest_se_deve
->pr_ref_count
);
3468 smp_mb__after_atomic_dec();
3469 dest_se_deve
= NULL
;
3470 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3474 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3475 " ACL for dest_se_deve->mapped_lun: %u\n",
3476 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3477 dest_se_deve
->mapped_lun
);
3480 * A persistent reservation needs to already existing in order to
3481 * successfully complete the REGISTER_AND_MOVE service action..
3483 spin_lock(&dev
->dev_reservation_lock
);
3484 pr_res_holder
= dev
->dev_pr_res_holder
;
3485 if (!pr_res_holder
) {
3486 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3487 " currently held\n");
3488 spin_unlock(&dev
->dev_reservation_lock
);
3489 ret
= TCM_INVALID_CDB_FIELD
;
3493 * The received on I_T Nexus must be the reservation holder.
3495 * From spc4r17 section 5.7.8 Table 50 --
3496 * Register behaviors for a REGISTER AND MOVE service action
3498 if (pr_res_holder
!= pr_reg
) {
3499 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3500 " Nexus is not reservation holder\n");
3501 spin_unlock(&dev
->dev_reservation_lock
);
3502 ret
= TCM_RESERVATION_CONFLICT
;
3506 * From spc4r17 section 5.7.8: registering and moving reservation
3508 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3509 * action is received and the established persistent reservation is a
3510 * Write Exclusive - All Registrants type or Exclusive Access -
3511 * All Registrants type reservation, then the command shall be completed
3512 * with RESERVATION CONFLICT status.
3514 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3515 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3516 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3517 " reservation for type: %s\n",
3518 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3519 spin_unlock(&dev
->dev_reservation_lock
);
3520 ret
= TCM_RESERVATION_CONFLICT
;
3523 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3525 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3527 type
= pr_res_holder
->pr_res_type
;
3528 scope
= pr_res_holder
->pr_res_type
;
3530 * c) Associate the reservation key specified in the SERVICE ACTION
3531 * RESERVATION KEY field with the I_T nexus specified as the
3532 * destination of the register and move, where:
3533 * A) The I_T nexus is specified by the TransportID and the
3534 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3535 * B) Regardless of the TransportID format used, the association for
3536 * the initiator port is based on either the initiator port name
3537 * (see 3.1.71) on SCSI transport protocols where port names are
3538 * required or the initiator port identifier (see 3.1.70) on SCSI
3539 * transport protocols where port names are not required;
3540 * d) Register the reservation key specified in the SERVICE ACTION
3541 * RESERVATION KEY field;
3542 * e) Retain the reservation key specified in the SERVICE ACTION
3543 * RESERVATION KEY field and associated information;
3545 * Also, It is not an error for a REGISTER AND MOVE service action to
3546 * register an I_T nexus that is already registered with the same
3547 * reservation key or a different reservation key.
3549 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3552 if (core_scsi3_alloc_registration(cmd
->se_dev
,
3553 dest_node_acl
, dest_se_deve
, iport_ptr
,
3554 sa_res_key
, 0, aptpl
, 2, 1)) {
3555 spin_unlock(&dev
->dev_reservation_lock
);
3556 ret
= TCM_INVALID_PARAMETER_LIST
;
3559 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3564 * f) Release the persistent reservation for the persistent reservation
3565 * holder (i.e., the I_T nexus on which the
3567 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3568 dev
->dev_pr_res_holder
, 0);
3570 * g) Move the persistent reservation to the specified I_T nexus using
3571 * the same scope and type as the persistent reservation released in
3574 dev
->dev_pr_res_holder
= dest_pr_reg
;
3575 dest_pr_reg
->pr_res_holder
= 1;
3576 dest_pr_reg
->pr_res_type
= type
;
3577 pr_reg
->pr_res_scope
= scope
;
3578 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
3579 PR_REG_ISID_ID_LEN
);
3581 * Increment PRGeneration for existing registrations..
3584 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3585 spin_unlock(&dev
->dev_reservation_lock
);
3587 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3588 " created new reservation holder TYPE: %s on object RTPI:"
3589 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3590 core_scsi3_pr_dump_type(type
), rtpi
,
3591 dest_pr_reg
->pr_res_generation
);
3592 pr_debug("SPC-3 PR Successfully moved reservation from"
3593 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3594 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3595 (prf_isid
) ? &i_buf
[0] : "", dest_tf_ops
->get_fabric_name(),
3596 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3599 * It is now safe to release configfs group dependencies for destination
3600 * of Transport ID Initiator Device/Port Identifier
3602 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3603 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3604 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3606 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3607 * nexus on which PERSISTENT RESERVE OUT command was received.
3610 spin_lock(&pr_tmpl
->registration_lock
);
3611 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3612 spin_unlock(&pr_tmpl
->registration_lock
);
3614 core_scsi3_put_pr_reg(pr_reg
);
3617 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3618 * write out the updated metadata to struct file for this SCSI device.
3621 pr_tmpl
->pr_aptpl_active
= 0;
3622 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
3623 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3624 " REGISTER_AND_MOVE\n");
3626 pr_tmpl
->pr_aptpl_active
= 1;
3627 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3628 &dest_pr_reg
->pr_aptpl_buf
[0],
3629 pr_tmpl
->pr_aptpl_buf_len
)) {
3630 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3631 " REGISTER_AND_MOVE\n");
3635 transport_kunmap_data_sg(cmd
);
3637 core_scsi3_put_pr_reg(dest_pr_reg
);
3641 transport_kunmap_data_sg(cmd
);
3643 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3645 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3646 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3649 core_scsi3_put_pr_reg(pr_reg
);
3653 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3655 unsigned int __v1
, __v2
;
3657 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3658 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3660 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3664 * See spc4r17 section 6.14 Table 170
3667 target_scsi3_emulate_pr_out(struct se_cmd
*cmd
)
3669 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3671 u64 res_key
, sa_res_key
;
3672 int sa
, scope
, type
, aptpl
;
3673 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3677 * Following spc2r20 5.5.1 Reservations overview:
3679 * If a logical unit has been reserved by any RESERVE command and is
3680 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3681 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3682 * initiator or service action and shall terminate with a RESERVATION
3685 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
3686 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3687 " SPC-2 reservation is held, returning"
3688 " RESERVATION_CONFLICT\n");
3689 return TCM_RESERVATION_CONFLICT
;
3693 * FIXME: A NULL struct se_session pointer means an this is not coming from
3694 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3697 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3699 if (cmd
->data_length
< 24) {
3700 pr_warn("SPC-PR: Received PR OUT parameter list"
3701 " length too small: %u\n", cmd
->data_length
);
3702 return TCM_INVALID_PARAMETER_LIST
;
3706 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3708 sa
= (cdb
[1] & 0x1f);
3709 scope
= (cdb
[2] & 0xf0);
3710 type
= (cdb
[2] & 0x0f);
3712 buf
= transport_kmap_data_sg(cmd
);
3714 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3717 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3719 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3720 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3722 * REGISTER_AND_MOVE uses a different SA parameter list containing
3723 * SCSI TransportIDs.
3725 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3726 spec_i_pt
= (buf
[20] & 0x08);
3727 all_tg_pt
= (buf
[20] & 0x04);
3728 aptpl
= (buf
[20] & 0x01);
3730 aptpl
= (buf
[17] & 0x01);
3731 unreg
= (buf
[17] & 0x02);
3733 transport_kunmap_data_sg(cmd
);
3737 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3739 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
))
3740 return TCM_INVALID_PARAMETER_LIST
;
3743 * From spc4r17 section 6.14:
3745 * If the SPEC_I_PT bit is set to zero, the service action is not
3746 * REGISTER AND MOVE, and the parameter list length is not 24, then
3747 * the command shall be terminated with CHECK CONDITION status, with
3748 * the sense key set to ILLEGAL REQUEST, and the additional sense
3749 * code set to PARAMETER LIST LENGTH ERROR.
3751 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3752 (cmd
->data_length
!= 24)) {
3753 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3754 " list length: %u\n", cmd
->data_length
);
3755 return TCM_INVALID_PARAMETER_LIST
;
3759 * (core_scsi3_emulate_pro_* function parameters
3760 * are defined by spc4r17 Table 174:
3761 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3765 ret
= core_scsi3_emulate_pro_register(cmd
,
3766 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 0);
3769 ret
= core_scsi3_emulate_pro_reserve(cmd
, type
, scope
, res_key
);
3772 ret
= core_scsi3_emulate_pro_release(cmd
, type
, scope
, res_key
);
3775 ret
= core_scsi3_emulate_pro_clear(cmd
, res_key
);
3778 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3779 res_key
, sa_res_key
, 0);
3781 case PRO_PREEMPT_AND_ABORT
:
3782 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3783 res_key
, sa_res_key
, 1);
3785 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3786 ret
= core_scsi3_emulate_pro_register(cmd
,
3787 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 1);
3789 case PRO_REGISTER_AND_MOVE
:
3790 ret
= core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3791 sa_res_key
, aptpl
, unreg
);
3794 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3795 " action: 0x%02x\n", cdb
[1] & 0x1f);
3796 return TCM_INVALID_CDB_FIELD
;
3800 target_complete_cmd(cmd
, GOOD
);
3805 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3807 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3809 static sense_reason_t
3810 core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3812 struct se_device
*dev
= cmd
->se_dev
;
3813 struct t10_pr_registration
*pr_reg
;
3815 u32 add_len
= 0, off
= 8;
3817 if (cmd
->data_length
< 8) {
3818 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3819 " too small\n", cmd
->data_length
);
3820 return TCM_INVALID_CDB_FIELD
;
3823 buf
= transport_kmap_data_sg(cmd
);
3825 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3827 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3828 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3829 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3830 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3832 spin_lock(&dev
->t10_pr
.registration_lock
);
3833 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
3836 * Check for overflow of 8byte PRI READ_KEYS payload and
3837 * next reservation key list descriptor.
3839 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3842 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3843 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3844 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3845 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3846 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3847 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3848 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3849 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3853 spin_unlock(&dev
->t10_pr
.registration_lock
);
3855 buf
[4] = ((add_len
>> 24) & 0xff);
3856 buf
[5] = ((add_len
>> 16) & 0xff);
3857 buf
[6] = ((add_len
>> 8) & 0xff);
3858 buf
[7] = (add_len
& 0xff);
3860 transport_kunmap_data_sg(cmd
);
3866 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3868 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3870 static sense_reason_t
3871 core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
3873 struct se_device
*dev
= cmd
->se_dev
;
3874 struct t10_pr_registration
*pr_reg
;
3877 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
3879 if (cmd
->data_length
< 8) {
3880 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3881 " too small\n", cmd
->data_length
);
3882 return TCM_INVALID_CDB_FIELD
;
3885 buf
= transport_kmap_data_sg(cmd
);
3887 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3889 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3890 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3891 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3892 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3894 spin_lock(&dev
->dev_reservation_lock
);
3895 pr_reg
= dev
->dev_pr_res_holder
;
3898 * Set the hardcoded Additional Length
3900 buf
[4] = ((add_len
>> 24) & 0xff);
3901 buf
[5] = ((add_len
>> 16) & 0xff);
3902 buf
[6] = ((add_len
>> 8) & 0xff);
3903 buf
[7] = (add_len
& 0xff);
3905 if (cmd
->data_length
< 22)
3909 * Set the Reservation key.
3911 * From spc4r17, section 5.7.10:
3912 * A persistent reservation holder has its reservation key
3913 * returned in the parameter data from a PERSISTENT
3914 * RESERVE IN command with READ RESERVATION service action as
3916 * a) For a persistent reservation of the type Write Exclusive
3917 * - All Registrants or Exclusive Access  All Regitrants,
3918 * the reservation key shall be set to zero; or
3919 * b) For all other persistent reservation types, the
3920 * reservation key shall be set to the registered
3921 * reservation key for the I_T nexus that holds the
3922 * persistent reservation.
3924 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3925 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
3928 pr_res_key
= pr_reg
->pr_res_key
;
3930 buf
[8] = ((pr_res_key
>> 56) & 0xff);
3931 buf
[9] = ((pr_res_key
>> 48) & 0xff);
3932 buf
[10] = ((pr_res_key
>> 40) & 0xff);
3933 buf
[11] = ((pr_res_key
>> 32) & 0xff);
3934 buf
[12] = ((pr_res_key
>> 24) & 0xff);
3935 buf
[13] = ((pr_res_key
>> 16) & 0xff);
3936 buf
[14] = ((pr_res_key
>> 8) & 0xff);
3937 buf
[15] = (pr_res_key
& 0xff);
3939 * Set the SCOPE and TYPE
3941 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
3942 (pr_reg
->pr_res_type
& 0x0f);
3946 spin_unlock(&dev
->dev_reservation_lock
);
3947 transport_kunmap_data_sg(cmd
);
3953 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3955 * See spc4r17 section 6.13.4 Table 165
3957 static sense_reason_t
3958 core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
3960 struct se_device
*dev
= cmd
->se_dev
;
3961 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3963 u16 add_len
= 8; /* Hardcoded to 8. */
3965 if (cmd
->data_length
< 6) {
3966 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3967 " %u too small\n", cmd
->data_length
);
3968 return TCM_INVALID_CDB_FIELD
;
3971 buf
= transport_kmap_data_sg(cmd
);
3973 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3975 buf
[0] = ((add_len
<< 8) & 0xff);
3976 buf
[1] = (add_len
& 0xff);
3977 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3978 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3979 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3980 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3982 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3983 * set the TMV: Task Mask Valid bit.
3987 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3989 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3991 * PTPL_A: Persistence across Target Power Loss Active bit
3993 if (pr_tmpl
->pr_aptpl_active
)
3996 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3998 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3999 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4000 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4001 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4002 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4003 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4005 transport_kunmap_data_sg(cmd
);
4011 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4013 * See spc4r17 section 6.13.5 Table 168 and 169
4015 static sense_reason_t
4016 core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
4018 struct se_device
*dev
= cmd
->se_dev
;
4019 struct se_node_acl
*se_nacl
;
4020 struct se_portal_group
*se_tpg
;
4021 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
4022 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
4024 u32 add_desc_len
= 0, add_len
= 0, desc_len
, exp_desc_len
;
4025 u32 off
= 8; /* off into first Full Status descriptor */
4026 int format_code
= 0;
4028 if (cmd
->data_length
< 8) {
4029 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4030 " too small\n", cmd
->data_length
);
4031 return TCM_INVALID_CDB_FIELD
;
4034 buf
= transport_kmap_data_sg(cmd
);
4036 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
4038 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
4039 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
4040 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
4041 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
4043 spin_lock(&pr_tmpl
->registration_lock
);
4044 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
4045 &pr_tmpl
->registration_list
, pr_reg_list
) {
4047 se_nacl
= pr_reg
->pr_reg_nacl
;
4048 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
4051 atomic_inc(&pr_reg
->pr_res_holders
);
4052 smp_mb__after_atomic_inc();
4053 spin_unlock(&pr_tmpl
->registration_lock
);
4055 * Determine expected length of $FABRIC_MOD specific
4056 * TransportID full status descriptor..
4058 exp_desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id_len(
4059 se_tpg
, se_nacl
, pr_reg
, &format_code
);
4061 if ((exp_desc_len
+ add_len
) > cmd
->data_length
) {
4062 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4063 " out of buffer: %d\n", cmd
->data_length
);
4064 spin_lock(&pr_tmpl
->registration_lock
);
4065 atomic_dec(&pr_reg
->pr_res_holders
);
4066 smp_mb__after_atomic_dec();
4070 * Set RESERVATION KEY
4072 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
4073 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
4074 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
4075 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4076 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4077 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4078 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4079 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4080 off
+= 4; /* Skip Over Reserved area */
4083 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4085 if (pr_reg
->pr_reg_all_tg_pt
)
4088 * The struct se_lun pointer will be present for the
4089 * reservation holder for PR_HOLDER bit.
4091 * Also, if this registration is the reservation
4092 * holder, fill in SCOPE and TYPE in the next byte.
4094 if (pr_reg
->pr_res_holder
) {
4096 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4097 (pr_reg
->pr_res_type
& 0x0f);
4101 off
+= 4; /* Skip over reserved area */
4103 * From spc4r17 6.3.15:
4105 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4106 * IDENTIFIER field contains the relative port identifier (see
4107 * 3.1.120) of the target port that is part of the I_T nexus
4108 * described by this full status descriptor. If the ALL_TG_PT
4109 * bit is set to one, the contents of the RELATIVE TARGET PORT
4110 * IDENTIFIER field are not defined by this standard.
4112 if (!pr_reg
->pr_reg_all_tg_pt
) {
4113 struct se_port
*port
= pr_reg
->pr_reg_tg_pt_lun
->lun_sep
;
4115 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
4116 buf
[off
++] = (port
->sep_rtpi
& 0xff);
4118 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4121 * Now, have the $FABRIC_MOD fill in the protocol identifier
4123 desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id(se_tpg
,
4124 se_nacl
, pr_reg
, &format_code
, &buf
[off
+4]);
4126 spin_lock(&pr_tmpl
->registration_lock
);
4127 atomic_dec(&pr_reg
->pr_res_holders
);
4128 smp_mb__after_atomic_dec();
4130 * Set the ADDITIONAL DESCRIPTOR LENGTH
4132 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4133 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4134 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4135 buf
[off
++] = (desc_len
& 0xff);
4137 * Size of full desctipor header minus TransportID
4138 * containing $FABRIC_MOD specific) initiator device/port
4141 * See spc4r17 Section 6.13.5 Table 169
4143 add_desc_len
= (24 + desc_len
);
4146 add_len
+= add_desc_len
;
4148 spin_unlock(&pr_tmpl
->registration_lock
);
4150 * Set ADDITIONAL_LENGTH
4152 buf
[4] = ((add_len
>> 24) & 0xff);
4153 buf
[5] = ((add_len
>> 16) & 0xff);
4154 buf
[6] = ((add_len
>> 8) & 0xff);
4155 buf
[7] = (add_len
& 0xff);
4157 transport_kunmap_data_sg(cmd
);
4163 target_scsi3_emulate_pr_in(struct se_cmd
*cmd
)
4168 * Following spc2r20 5.5.1 Reservations overview:
4170 * If a logical unit has been reserved by any RESERVE command and is
4171 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4172 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4173 * initiator or service action and shall terminate with a RESERVATION
4176 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
4177 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4178 " SPC-2 reservation is held, returning"
4179 " RESERVATION_CONFLICT\n");
4180 return TCM_RESERVATION_CONFLICT
;
4183 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4185 ret
= core_scsi3_pri_read_keys(cmd
);
4187 case PRI_READ_RESERVATION
:
4188 ret
= core_scsi3_pri_read_reservation(cmd
);
4190 case PRI_REPORT_CAPABILITIES
:
4191 ret
= core_scsi3_pri_report_capabilities(cmd
);
4193 case PRI_READ_FULL_STATUS
:
4194 ret
= core_scsi3_pri_read_full_status(cmd
);
4197 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4198 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4199 return TCM_INVALID_CDB_FIELD
;
4203 target_complete_cmd(cmd
, GOOD
);
4208 target_check_reservation(struct se_cmd
*cmd
)
4210 struct se_device
*dev
= cmd
->se_dev
;
4215 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
4217 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
4220 spin_lock(&dev
->dev_reservation_lock
);
4221 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
4222 ret
= target_scsi2_reservation_check(cmd
);
4224 ret
= target_scsi3_pr_reservation_check(cmd
);
4225 spin_unlock(&dev
->dev_reservation_lock
);