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 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@kernel.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ******************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_pr.h"
42 #include "target_core_ua.h"
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 struct pr_transport_id_holder
{
49 struct t10_pr_registration
*dest_pr_reg
;
50 struct se_portal_group
*dest_tpg
;
51 struct se_node_acl
*dest_node_acl
;
52 struct se_dev_entry
*dest_se_deve
;
53 struct list_head dest_list
;
56 int core_pr_dump_initiator_port(
57 struct t10_pr_registration
*pr_reg
,
61 if (!pr_reg
->isid_present_at_reg
)
64 snprintf(buf
, size
, ",i,0x%s", &pr_reg
->pr_reg_isid
[0]);
68 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
69 struct t10_pr_registration
*, int);
71 static int core_scsi2_reservation_seq_non_holder(
88 static int core_scsi2_reservation_check(struct se_cmd
*cmd
, u32
*pr_reg_type
)
90 struct se_device
*dev
= cmd
->se_dev
;
91 struct se_session
*sess
= cmd
->se_sess
;
97 spin_lock(&dev
->dev_reservation_lock
);
98 if (!dev
->dev_reserved_node_acl
|| !sess
) {
99 spin_unlock(&dev
->dev_reservation_lock
);
102 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
) {
103 spin_unlock(&dev
->dev_reservation_lock
);
106 if (!(dev
->dev_flags
& DF_SPC2_RESERVATIONS_WITH_ISID
)) {
107 spin_unlock(&dev
->dev_reservation_lock
);
110 ret
= (dev
->dev_res_bin_isid
== sess
->sess_bin_isid
) ? 0 : -EINVAL
;
111 spin_unlock(&dev
->dev_reservation_lock
);
116 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
117 struct se_node_acl
*, struct se_session
*);
118 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
120 static int target_check_scsi2_reservation_conflict(struct se_cmd
*cmd
)
122 struct se_session
*se_sess
= cmd
->se_sess
;
123 struct se_subsystem_dev
*su_dev
= cmd
->se_dev
->se_sub_dev
;
124 struct t10_pr_registration
*pr_reg
;
125 struct t10_reservation
*pr_tmpl
= &su_dev
->t10_pr
;
126 int crh
= (su_dev
->t10_pr
.res_type
== SPC3_PERSISTENT_RESERVATIONS
);
132 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
156 if (pr_reg
->pr_res_holder
) {
157 core_scsi3_put_pr_reg(pr_reg
);
160 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
161 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
162 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
163 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
164 core_scsi3_put_pr_reg(pr_reg
);
167 core_scsi3_put_pr_reg(pr_reg
);
171 * Following spc2r20 5.5.1 Reservations overview:
173 * If a logical unit has executed a PERSISTENT RESERVE OUT
174 * command with the REGISTER or the REGISTER AND IGNORE
175 * EXISTING KEY service action and is still registered by any
176 * initiator, all RESERVE commands and all RELEASE commands
177 * regardless of initiator shall conflict and shall terminate
178 * with a RESERVATION CONFLICT status.
180 spin_lock(&pr_tmpl
->registration_lock
);
181 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
182 spin_unlock(&pr_tmpl
->registration_lock
);
186 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
187 " while active SPC-3 registrations exist,"
188 " returning RESERVATION_CONFLICT\n");
189 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
196 int target_scsi2_reservation_release(struct se_cmd
*cmd
)
198 struct se_device
*dev
= cmd
->se_dev
;
199 struct se_session
*sess
= cmd
->se_sess
;
200 struct se_portal_group
*tpg
= sess
->se_tpg
;
205 rc
= target_check_scsi2_reservation_conflict(cmd
);
209 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
215 spin_lock(&dev
->dev_reservation_lock
);
216 if (!dev
->dev_reserved_node_acl
|| !sess
)
219 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
222 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
225 dev
->dev_reserved_node_acl
= NULL
;
226 dev
->dev_flags
&= ~DF_SPC2_RESERVATIONS
;
227 if (dev
->dev_flags
& DF_SPC2_RESERVATIONS_WITH_ISID
) {
228 dev
->dev_res_bin_isid
= 0;
229 dev
->dev_flags
&= ~DF_SPC2_RESERVATIONS_WITH_ISID
;
231 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
232 " MAPPED LUN: %u for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
233 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
234 sess
->se_node_acl
->initiatorname
);
237 spin_unlock(&dev
->dev_reservation_lock
);
240 target_complete_cmd(cmd
, GOOD
);
244 int target_scsi2_reservation_reserve(struct se_cmd
*cmd
)
246 struct se_device
*dev
= cmd
->se_dev
;
247 struct se_session
*sess
= cmd
->se_sess
;
248 struct se_portal_group
*tpg
= sess
->se_tpg
;
251 if ((cmd
->t_task_cdb
[1] & 0x01) &&
252 (cmd
->t_task_cdb
[1] & 0x02)) {
253 pr_err("LongIO and Obselete Bits set, returning"
254 " ILLEGAL_REQUEST\n");
255 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
260 * This is currently the case for target_core_mod passthrough struct se_cmd
265 rc
= target_check_scsi2_reservation_conflict(cmd
);
269 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
275 spin_lock(&dev
->dev_reservation_lock
);
276 if (dev
->dev_reserved_node_acl
&&
277 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
278 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
279 tpg
->se_tpg_tfo
->get_fabric_name());
280 pr_err("Original reserver LUN: %u %s\n",
281 cmd
->se_lun
->unpacked_lun
,
282 dev
->dev_reserved_node_acl
->initiatorname
);
283 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
284 " from %s \n", cmd
->se_lun
->unpacked_lun
,
285 cmd
->se_deve
->mapped_lun
,
286 sess
->se_node_acl
->initiatorname
);
287 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
292 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
293 dev
->dev_flags
|= DF_SPC2_RESERVATIONS
;
294 if (sess
->sess_bin_isid
!= 0) {
295 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
296 dev
->dev_flags
|= DF_SPC2_RESERVATIONS_WITH_ISID
;
298 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
299 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
300 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
301 sess
->se_node_acl
->initiatorname
);
304 spin_unlock(&dev
->dev_reservation_lock
);
307 target_complete_cmd(cmd
, GOOD
);
313 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
315 * This function is called by those initiator ports who are *NOT*
316 * the active PR reservation holder when a reservation is present.
318 static int core_scsi3_pr_seq_non_holder(
323 struct se_dev_entry
*se_deve
;
324 struct se_session
*se_sess
= cmd
->se_sess
;
325 int other_cdb
= 0, ignore_reg
;
326 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
327 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
328 int we
= 0; /* Write Exclusive */
329 int legacy
= 0; /* Act like a legacy device and return
330 * RESERVATION CONFLICT on some CDBs */
332 * A legacy SPC-2 reservation is being held.
334 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
)
335 return core_scsi2_reservation_seq_non_holder(cmd
,
338 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
340 * Determine if the registration should be ignored due to
341 * non-matching ISIDs in core_scsi3_pr_reservation_check().
343 ignore_reg
= (pr_reg_type
& 0x80000000);
345 pr_reg_type
&= ~0x80000000;
347 switch (pr_reg_type
) {
348 case PR_TYPE_WRITE_EXCLUSIVE
:
350 case PR_TYPE_EXCLUSIVE_ACCESS
:
352 * Some commands are only allowed for the persistent reservation
355 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
356 registered_nexus
= 1;
358 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
360 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
362 * Some commands are only allowed for registered I_T Nexuses.
365 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
366 registered_nexus
= 1;
368 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
370 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
372 * Each registered I_T Nexus is a reservation holder.
375 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
376 registered_nexus
= 1;
382 * Referenced from spc4r17 table 45 for *NON* PR holder access
385 case SECURITY_PROTOCOL_IN
:
386 if (registered_nexus
)
394 case RECEIVE_DIAGNOSTIC
:
399 if (registered_nexus
) {
403 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
405 case PERSISTENT_RESERVE_OUT
:
407 * This follows PERSISTENT_RESERVE_OUT service actions that
408 * are allowed in the presence of various reservations.
409 * See spc4r17, table 46
411 switch (cdb
[1] & 0x1f) {
414 case PRO_PREEMPT_AND_ABORT
:
415 ret
= (registered_nexus
) ? 0 : 1;
418 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
421 case PRO_REGISTER_AND_MOVE
:
426 ret
= (registered_nexus
) ? 0 : 1;
429 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
430 " action: 0x%02x\n", cdb
[1] & 0x1f);
436 /* Handled by CRH=1 in target_scsi2_reservation_release() */
441 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
444 case TEST_UNIT_READY
:
445 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
448 switch (cdb
[1] & 0x1f) {
449 case MI_MANAGEMENT_PROTOCOL_IN
:
450 if (registered_nexus
) {
454 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
456 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
457 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
462 if (registered_nexus
) {
466 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
468 case MI_REPORT_ALIASES
:
469 case MI_REPORT_IDENTIFYING_INFORMATION
:
470 case MI_REPORT_PRIORITY
:
471 case MI_REPORT_TARGET_PGS
:
472 case MI_REPORT_TIMESTAMP
:
473 ret
= 0; /* Allowed */
476 pr_err("Unknown MI Service Action: 0x%02x\n",
481 case ACCESS_CONTROL_IN
:
482 case ACCESS_CONTROL_OUT
:
485 case READ_MEDIA_SERIAL_NUMBER
:
488 case PERSISTENT_RESERVE_IN
:
489 ret
= 0; /*/ Allowed CDBs */
496 * Case where the CDB is explicitly allowed in the above switch
499 if (!ret
&& !other_cdb
) {
500 pr_debug("Allowing explict CDB: 0x%02x for %s"
501 " reservation holder\n", cdb
[0],
502 core_scsi3_pr_dump_type(pr_reg_type
));
507 * Check if write exclusive initiator ports *NOT* holding the
508 * WRITE_EXCLUSIVE_* reservation.
510 if (we
&& !registered_nexus
) {
511 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
513 * Conflict for write exclusive
515 pr_debug("%s Conflict for unregistered nexus"
516 " %s CDB: 0x%02x to %s reservation\n",
517 transport_dump_cmd_direction(cmd
),
518 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
519 core_scsi3_pr_dump_type(pr_reg_type
));
523 * Allow non WRITE CDBs for all Write Exclusive
524 * PR TYPEs to pass for registered and
525 * non-registered_nexuxes NOT holding the reservation.
527 * We only make noise for the unregisterd nexuses,
528 * as we expect registered non-reservation holding
529 * nexuses to issue CDBs.
532 if (!registered_nexus
) {
533 pr_debug("Allowing implict CDB: 0x%02x"
534 " for %s reservation on unregistered"
536 core_scsi3_pr_dump_type(pr_reg_type
));
541 } else if ((reg_only
) || (all_reg
)) {
542 if (registered_nexus
) {
544 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
545 * allow commands from registered nexuses.
548 pr_debug("Allowing implict CDB: 0x%02x for %s"
549 " reservation\n", cdb
[0],
550 core_scsi3_pr_dump_type(pr_reg_type
));
555 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
556 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
557 (registered_nexus
) ? "" : "un",
558 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
559 core_scsi3_pr_dump_type(pr_reg_type
));
561 return 1; /* Conflict by default */
564 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
566 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
569 * PRGeneration field shall contain the value of a 32-bit wrapping
570 * counter mainted by the device server.
572 * Note that this is done regardless of Active Persist across
573 * Target PowerLoss (APTPL)
575 * See spc4r17 section 6.3.12 READ_KEYS service action
577 spin_lock(&dev
->dev_reservation_lock
);
578 prg
= su_dev
->t10_pr
.pr_generation
++;
579 spin_unlock(&dev
->dev_reservation_lock
);
584 static int core_scsi3_pr_reservation_check(
588 struct se_device
*dev
= cmd
->se_dev
;
589 struct se_session
*sess
= cmd
->se_sess
;
595 * A legacy SPC-2 reservation is being held.
597 if (dev
->dev_flags
& DF_SPC2_RESERVATIONS
)
598 return core_scsi2_reservation_check(cmd
, pr_reg_type
);
600 spin_lock(&dev
->dev_reservation_lock
);
601 if (!dev
->dev_pr_res_holder
) {
602 spin_unlock(&dev
->dev_reservation_lock
);
605 *pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
606 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
607 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
) {
608 spin_unlock(&dev
->dev_reservation_lock
);
611 if (!dev
->dev_pr_res_holder
->isid_present_at_reg
) {
612 spin_unlock(&dev
->dev_reservation_lock
);
615 ret
= (dev
->dev_pr_res_holder
->pr_reg_bin_isid
==
616 sess
->sess_bin_isid
) ? 0 : -EINVAL
;
618 * Use bit in *pr_reg_type to notify ISID mismatch in
619 * core_scsi3_pr_seq_non_holder().
622 *pr_reg_type
|= 0x80000000;
623 spin_unlock(&dev
->dev_reservation_lock
);
628 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
629 struct se_device
*dev
,
630 struct se_node_acl
*nacl
,
631 struct se_dev_entry
*deve
,
637 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
638 struct t10_pr_registration
*pr_reg
;
640 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
642 pr_err("Unable to allocate struct t10_pr_registration\n");
646 pr_reg
->pr_aptpl_buf
= kzalloc(su_dev
->t10_pr
.pr_aptpl_buf_len
,
648 if (!pr_reg
->pr_aptpl_buf
) {
649 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
650 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
654 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
655 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
656 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
657 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
658 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
659 atomic_set(&pr_reg
->pr_res_holders
, 0);
660 pr_reg
->pr_reg_nacl
= nacl
;
661 pr_reg
->pr_reg_deve
= deve
;
662 pr_reg
->pr_res_mapped_lun
= deve
->mapped_lun
;
663 pr_reg
->pr_aptpl_target_lun
= deve
->se_lun
->unpacked_lun
;
664 pr_reg
->pr_res_key
= sa_res_key
;
665 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
666 pr_reg
->pr_reg_aptpl
= aptpl
;
667 pr_reg
->pr_reg_tg_pt_lun
= deve
->se_lun
;
669 * If an ISID value for this SCSI Initiator Port exists,
670 * save it to the registration now.
673 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
674 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
675 pr_reg
->isid_present_at_reg
= 1;
681 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
682 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
685 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
688 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
689 struct se_device
*dev
,
690 struct se_node_acl
*nacl
,
691 struct se_dev_entry
*deve
,
697 struct se_dev_entry
*deve_tmp
;
698 struct se_node_acl
*nacl_tmp
;
699 struct se_port
*port
, *port_tmp
;
700 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
701 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
704 * Create a registration for the I_T Nexus upon which the
705 * PROUT REGISTER was received.
707 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, deve
, isid
,
708 sa_res_key
, all_tg_pt
, aptpl
);
712 * Return pointer to pr_reg for ALL_TG_PT=0
717 * Create list of matching SCSI Initiator Port registrations
720 spin_lock(&dev
->se_port_lock
);
721 list_for_each_entry_safe(port
, port_tmp
, &dev
->dev_sep_list
, sep_list
) {
722 atomic_inc(&port
->sep_tg_pt_ref_cnt
);
723 smp_mb__after_atomic_inc();
724 spin_unlock(&dev
->se_port_lock
);
726 spin_lock_bh(&port
->sep_alua_lock
);
727 list_for_each_entry(deve_tmp
, &port
->sep_alua_list
,
730 * This pointer will be NULL for demo mode MappedLUNs
731 * that have not been make explict via a ConfigFS
732 * MappedLUN group for the SCSI Initiator Node ACL.
734 if (!deve_tmp
->se_lun_acl
)
737 nacl_tmp
= deve_tmp
->se_lun_acl
->se_lun_nacl
;
739 * Skip the matching struct se_node_acl that is allocated
742 if (nacl
== nacl_tmp
)
745 * Only perform PR registrations for target ports on
746 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
749 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
752 * Look for a matching Initiator Node ACL in ASCII format
754 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
757 atomic_inc(&deve_tmp
->pr_ref_count
);
758 smp_mb__after_atomic_inc();
759 spin_unlock_bh(&port
->sep_alua_lock
);
761 * Grab a configfs group dependency that is released
762 * for the exception path at label out: below, or upon
763 * completion of adding ALL_TG_PT=1 registrations in
764 * __core_scsi3_add_registration()
766 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
768 pr_err("core_scsi3_lunacl_depend"
770 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
771 smp_mb__after_atomic_dec();
772 atomic_dec(&deve_tmp
->pr_ref_count
);
773 smp_mb__after_atomic_dec();
777 * Located a matching SCSI Initiator Port on a different
778 * port, allocate the pr_reg_atp and attach it to the
779 * pr_reg->pr_reg_atp_list that will be processed once
780 * the original *pr_reg is processed in
781 * __core_scsi3_add_registration()
783 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
784 nacl_tmp
, deve_tmp
, NULL
,
785 sa_res_key
, all_tg_pt
, aptpl
);
787 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
788 smp_mb__after_atomic_dec();
789 atomic_dec(&deve_tmp
->pr_ref_count
);
790 smp_mb__after_atomic_dec();
791 core_scsi3_lunacl_undepend_item(deve_tmp
);
795 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
796 &pr_reg
->pr_reg_atp_list
);
797 spin_lock_bh(&port
->sep_alua_lock
);
799 spin_unlock_bh(&port
->sep_alua_lock
);
801 spin_lock(&dev
->se_port_lock
);
802 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
803 smp_mb__after_atomic_dec();
805 spin_unlock(&dev
->se_port_lock
);
809 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
810 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
811 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
812 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
813 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
815 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
819 int core_scsi3_alloc_aptpl_registration(
820 struct t10_reservation
*pr_tmpl
,
822 unsigned char *i_port
,
825 unsigned char *t_port
,
832 struct t10_pr_registration
*pr_reg
;
834 if (!i_port
|| !t_port
|| !sa_res_key
) {
835 pr_err("Illegal parameters for APTPL registration\n");
839 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
841 pr_err("Unable to allocate struct t10_pr_registration\n");
844 pr_reg
->pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
, GFP_KERNEL
);
846 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
847 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
848 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
849 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
850 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
851 atomic_set(&pr_reg
->pr_res_holders
, 0);
852 pr_reg
->pr_reg_nacl
= NULL
;
853 pr_reg
->pr_reg_deve
= NULL
;
854 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
855 pr_reg
->pr_aptpl_target_lun
= target_lun
;
856 pr_reg
->pr_res_key
= sa_res_key
;
857 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
858 pr_reg
->pr_reg_aptpl
= 1;
859 pr_reg
->pr_reg_tg_pt_lun
= NULL
;
860 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
861 pr_reg
->pr_res_type
= type
;
863 * If an ISID value had been saved in APTPL metadata for this
864 * SCSI Initiator Port, restore it now.
867 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
868 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
869 pr_reg
->isid_present_at_reg
= 1;
872 * Copy the i_port and t_port information from caller.
874 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
875 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
876 pr_reg
->pr_reg_tpgt
= tpgt
;
878 * Set pr_res_holder from caller, the pr_reg who is the reservation
879 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
880 * the Initiator Node LUN ACL from the fabric module is created for
883 pr_reg
->pr_res_holder
= res_holder
;
885 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
886 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
887 " metadata\n", (res_holder
) ? "+reservation" : "");
891 static void core_scsi3_aptpl_reserve(
892 struct se_device
*dev
,
893 struct se_portal_group
*tpg
,
894 struct se_node_acl
*node_acl
,
895 struct t10_pr_registration
*pr_reg
)
897 char i_buf
[PR_REG_ISID_ID_LEN
];
900 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
901 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
904 spin_lock(&dev
->dev_reservation_lock
);
905 dev
->dev_pr_res_holder
= pr_reg
;
906 spin_unlock(&dev
->dev_reservation_lock
);
908 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
909 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
910 tpg
->se_tpg_tfo
->get_fabric_name(),
911 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
912 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
913 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
914 tpg
->se_tpg_tfo
->get_fabric_name(), node_acl
->initiatorname
,
915 (prf_isid
) ? &i_buf
[0] : "");
918 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
919 struct t10_pr_registration
*, int, int);
921 static int __core_scsi3_check_aptpl_registration(
922 struct se_device
*dev
,
923 struct se_portal_group
*tpg
,
926 struct se_node_acl
*nacl
,
927 struct se_dev_entry
*deve
)
929 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
930 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
931 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
932 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
935 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
936 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
938 * Copy Initiator Port information from struct se_node_acl
940 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
941 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
942 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
943 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
945 * Look for the matching registrations+reservation from those
946 * created from APTPL metadata. Note that multiple registrations
947 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
950 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
951 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
953 if (!strcmp(pr_reg
->pr_iport
, i_port
) &&
954 (pr_reg
->pr_res_mapped_lun
== deve
->mapped_lun
) &&
955 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
956 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
957 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
959 pr_reg
->pr_reg_nacl
= nacl
;
960 pr_reg
->pr_reg_deve
= deve
;
961 pr_reg
->pr_reg_tg_pt_lun
= lun
;
963 list_del(&pr_reg
->pr_reg_aptpl_list
);
964 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
966 * At this point all of the pointers in *pr_reg will
967 * be setup, so go ahead and add the registration.
970 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
972 * If this registration is the reservation holder,
973 * make that happen now..
975 if (pr_reg
->pr_res_holder
)
976 core_scsi3_aptpl_reserve(dev
, tpg
,
979 * Reenable pr_aptpl_active to accept new metadata
980 * updates once the SCSI device is active again..
982 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
983 pr_tmpl
->pr_aptpl_active
= 1;
986 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
991 int core_scsi3_check_aptpl_registration(
992 struct se_device
*dev
,
993 struct se_portal_group
*tpg
,
995 struct se_lun_acl
*lun_acl
)
997 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
998 struct se_node_acl
*nacl
= lun_acl
->se_lun_nacl
;
999 struct se_dev_entry
*deve
= nacl
->device_list
[lun_acl
->mapped_lun
];
1001 if (su_dev
->t10_pr
.res_type
!= SPC3_PERSISTENT_RESERVATIONS
)
1004 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
1005 lun
->unpacked_lun
, nacl
, deve
);
1008 static void __core_scsi3_dump_registration(
1009 struct target_core_fabric_ops
*tfo
,
1010 struct se_device
*dev
,
1011 struct se_node_acl
*nacl
,
1012 struct t10_pr_registration
*pr_reg
,
1015 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
1016 char i_buf
[PR_REG_ISID_ID_LEN
];
1019 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
1020 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1021 PR_REG_ISID_ID_LEN
);
1023 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1024 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== 2) ?
1025 "_AND_MOVE" : (register_type
== 1) ?
1026 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
1027 (prf_isid
) ? i_buf
: "");
1028 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1029 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
1030 tfo
->tpg_get_tag(se_tpg
));
1031 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1032 " Port(s)\n", tfo
->get_fabric_name(),
1033 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1034 dev
->transport
->name
);
1035 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1036 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
1037 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
1038 pr_reg
->pr_reg_aptpl
);
1042 * this function can be called with struct se_device->dev_reservation_lock
1043 * when register_move = 1
1045 static void __core_scsi3_add_registration(
1046 struct se_device
*dev
,
1047 struct se_node_acl
*nacl
,
1048 struct t10_pr_registration
*pr_reg
,
1052 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1053 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1054 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1055 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1058 * Increment PRgeneration counter for struct se_device upon a successful
1059 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1061 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1062 * action, the struct se_device->dev_reservation_lock will already be held,
1063 * so we do not call core_scsi3_pr_generation() which grabs the lock
1066 pr_reg
->pr_res_generation
= (register_move
) ?
1067 su_dev
->t10_pr
.pr_generation
++ :
1068 core_scsi3_pr_generation(dev
);
1070 spin_lock(&pr_tmpl
->registration_lock
);
1071 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1072 pr_reg
->pr_reg_deve
->def_pr_registered
= 1;
1074 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1075 spin_unlock(&pr_tmpl
->registration_lock
);
1077 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1079 if (!pr_reg
->pr_reg_all_tg_pt
|| register_move
)
1082 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1083 * allocated in __core_scsi3_alloc_registration()
1085 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1086 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1087 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1089 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1091 spin_lock(&pr_tmpl
->registration_lock
);
1092 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1093 &pr_tmpl
->registration_list
);
1094 pr_reg_tmp
->pr_reg_deve
->def_pr_registered
= 1;
1096 __core_scsi3_dump_registration(tfo
, dev
,
1097 pr_reg_tmp
->pr_reg_nacl
, pr_reg_tmp
,
1099 spin_unlock(&pr_tmpl
->registration_lock
);
1101 * Drop configfs group dependency reference from
1102 * __core_scsi3_alloc_registration()
1104 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1108 static int core_scsi3_alloc_registration(
1109 struct se_device
*dev
,
1110 struct se_node_acl
*nacl
,
1111 struct se_dev_entry
*deve
,
1112 unsigned char *isid
,
1119 struct t10_pr_registration
*pr_reg
;
1121 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, deve
, isid
,
1122 sa_res_key
, all_tg_pt
, aptpl
);
1126 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1127 register_type
, register_move
);
1131 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1132 struct se_device
*dev
,
1133 struct se_node_acl
*nacl
,
1134 unsigned char *isid
)
1136 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1137 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1138 struct se_portal_group
*tpg
;
1140 spin_lock(&pr_tmpl
->registration_lock
);
1141 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1142 &pr_tmpl
->registration_list
, pr_reg_list
) {
1144 * First look for a matching struct se_node_acl
1146 if (pr_reg
->pr_reg_nacl
!= nacl
)
1149 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1151 * If this registration does NOT contain a fabric provided
1152 * ISID, then we have found a match.
1154 if (!pr_reg
->isid_present_at_reg
) {
1156 * Determine if this SCSI device server requires that
1157 * SCSI Intiatior TransportID w/ ISIDs is enforced
1158 * for fabric modules (iSCSI) requiring them.
1160 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1161 if (dev
->se_sub_dev
->se_dev_attrib
.enforce_pr_isids
)
1164 atomic_inc(&pr_reg
->pr_res_holders
);
1165 smp_mb__after_atomic_inc();
1166 spin_unlock(&pr_tmpl
->registration_lock
);
1170 * If the *pr_reg contains a fabric defined ISID for multi-value
1171 * SCSI Initiator Port TransportIDs, then we expect a valid
1172 * matching ISID to be provided by the local SCSI Initiator Port.
1176 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1179 atomic_inc(&pr_reg
->pr_res_holders
);
1180 smp_mb__after_atomic_inc();
1181 spin_unlock(&pr_tmpl
->registration_lock
);
1184 spin_unlock(&pr_tmpl
->registration_lock
);
1189 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1190 struct se_device
*dev
,
1191 struct se_node_acl
*nacl
,
1192 struct se_session
*sess
)
1194 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1195 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1197 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1198 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1199 tpg
->se_tpg_tfo
->sess_get_initiator_sid(sess
, &buf
[0],
1204 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1207 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1209 atomic_dec(&pr_reg
->pr_res_holders
);
1210 smp_mb__after_atomic_dec();
1213 static int core_scsi3_check_implict_release(
1214 struct se_device
*dev
,
1215 struct t10_pr_registration
*pr_reg
)
1217 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1218 struct t10_pr_registration
*pr_res_holder
;
1221 spin_lock(&dev
->dev_reservation_lock
);
1222 pr_res_holder
= dev
->dev_pr_res_holder
;
1223 if (!pr_res_holder
) {
1224 spin_unlock(&dev
->dev_reservation_lock
);
1227 if (pr_res_holder
== pr_reg
) {
1229 * Perform an implict RELEASE if the registration that
1230 * is being released is holding the reservation.
1232 * From spc4r17, section 5.7.11.1:
1234 * e) If the I_T nexus is the persistent reservation holder
1235 * and the persistent reservation is not an all registrants
1236 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1237 * service action or REGISTER AND IGNORE EXISTING KEY
1238 * service action with the SERVICE ACTION RESERVATION KEY
1239 * field set to zero (see 5.7.11.3).
1241 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0);
1244 * For 'All Registrants' reservation types, all existing
1245 * registrations are still processed as reservation holders
1246 * in core_scsi3_pr_seq_non_holder() after the initial
1247 * reservation holder is implictly released here.
1249 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1250 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1251 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1252 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1253 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1254 " UNREGISTER while existing reservation with matching"
1255 " key 0x%016Lx is present from another SCSI Initiator"
1256 " Port\n", pr_reg
->pr_res_key
);
1259 spin_unlock(&dev
->dev_reservation_lock
);
1265 * Called with struct t10_reservation->registration_lock held.
1267 static void __core_scsi3_free_registration(
1268 struct se_device
*dev
,
1269 struct t10_pr_registration
*pr_reg
,
1270 struct list_head
*preempt_and_abort_list
,
1273 struct target_core_fabric_ops
*tfo
=
1274 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1275 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1276 char i_buf
[PR_REG_ISID_ID_LEN
];
1279 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1280 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1281 PR_REG_ISID_ID_LEN
);
1283 pr_reg
->pr_reg_deve
->def_pr_registered
= 0;
1284 pr_reg
->pr_reg_deve
->pr_res_key
= 0;
1285 list_del(&pr_reg
->pr_reg_list
);
1287 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1288 * so call core_scsi3_put_pr_reg() to decrement our reference.
1291 core_scsi3_put_pr_reg(pr_reg
);
1293 * Wait until all reference from any other I_T nexuses for this
1294 * *pr_reg have been released. Because list_del() is called above,
1295 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1296 * count back to zero, and we release *pr_reg.
1298 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1299 spin_unlock(&pr_tmpl
->registration_lock
);
1300 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1301 tfo
->get_fabric_name());
1303 spin_lock(&pr_tmpl
->registration_lock
);
1306 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1307 " Node: %s%s\n", tfo
->get_fabric_name(),
1308 pr_reg
->pr_reg_nacl
->initiatorname
,
1309 (prf_isid
) ? &i_buf
[0] : "");
1310 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1311 " Port(s)\n", tfo
->get_fabric_name(),
1312 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1313 dev
->transport
->name
);
1314 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1315 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1316 pr_reg
->pr_res_generation
);
1318 if (!preempt_and_abort_list
) {
1319 pr_reg
->pr_reg_deve
= NULL
;
1320 pr_reg
->pr_reg_nacl
= NULL
;
1321 kfree(pr_reg
->pr_aptpl_buf
);
1322 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1326 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1327 * are released once the ABORT_TASK_SET has completed..
1329 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1332 void core_scsi3_free_pr_reg_from_nacl(
1333 struct se_device
*dev
,
1334 struct se_node_acl
*nacl
)
1336 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1337 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1339 * If the passed se_node_acl matches the reservation holder,
1340 * release the reservation.
1342 spin_lock(&dev
->dev_reservation_lock
);
1343 pr_res_holder
= dev
->dev_pr_res_holder
;
1344 if ((pr_res_holder
!= NULL
) &&
1345 (pr_res_holder
->pr_reg_nacl
== nacl
))
1346 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0);
1347 spin_unlock(&dev
->dev_reservation_lock
);
1349 * Release any registration associated with the struct se_node_acl.
1351 spin_lock(&pr_tmpl
->registration_lock
);
1352 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1353 &pr_tmpl
->registration_list
, pr_reg_list
) {
1355 if (pr_reg
->pr_reg_nacl
!= nacl
)
1358 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1360 spin_unlock(&pr_tmpl
->registration_lock
);
1363 void core_scsi3_free_all_registrations(
1364 struct se_device
*dev
)
1366 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1367 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1369 spin_lock(&dev
->dev_reservation_lock
);
1370 pr_res_holder
= dev
->dev_pr_res_holder
;
1371 if (pr_res_holder
!= NULL
) {
1372 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1373 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1376 spin_unlock(&dev
->dev_reservation_lock
);
1378 spin_lock(&pr_tmpl
->registration_lock
);
1379 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1380 &pr_tmpl
->registration_list
, pr_reg_list
) {
1382 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1384 spin_unlock(&pr_tmpl
->registration_lock
);
1386 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1387 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1388 pr_reg_aptpl_list
) {
1389 list_del(&pr_reg
->pr_reg_aptpl_list
);
1390 kfree(pr_reg
->pr_aptpl_buf
);
1391 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1393 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1396 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1398 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1399 &tpg
->tpg_group
.cg_item
);
1402 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1404 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1405 &tpg
->tpg_group
.cg_item
);
1407 atomic_dec(&tpg
->tpg_pr_ref_count
);
1408 smp_mb__after_atomic_dec();
1411 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1413 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1415 if (nacl
->dynamic_node_acl
)
1418 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1419 &nacl
->acl_group
.cg_item
);
1422 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1424 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1426 if (nacl
->dynamic_node_acl
) {
1427 atomic_dec(&nacl
->acl_pr_ref_count
);
1428 smp_mb__after_atomic_dec();
1432 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1433 &nacl
->acl_group
.cg_item
);
1435 atomic_dec(&nacl
->acl_pr_ref_count
);
1436 smp_mb__after_atomic_dec();
1439 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1441 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1442 struct se_node_acl
*nacl
;
1443 struct se_portal_group
*tpg
;
1445 * For nacl->dynamic_node_acl=1
1450 nacl
= lun_acl
->se_lun_nacl
;
1453 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1454 &lun_acl
->se_lun_group
.cg_item
);
1457 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1459 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1460 struct se_node_acl
*nacl
;
1461 struct se_portal_group
*tpg
;
1463 * For nacl->dynamic_node_acl=1
1466 atomic_dec(&se_deve
->pr_ref_count
);
1467 smp_mb__after_atomic_dec();
1470 nacl
= lun_acl
->se_lun_nacl
;
1473 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1474 &lun_acl
->se_lun_group
.cg_item
);
1476 atomic_dec(&se_deve
->pr_ref_count
);
1477 smp_mb__after_atomic_dec();
1480 static int core_scsi3_decode_spec_i_port(
1482 struct se_portal_group
*tpg
,
1483 unsigned char *l_isid
,
1488 struct se_device
*dev
= cmd
->se_dev
;
1489 struct se_port
*tmp_port
;
1490 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1491 struct se_session
*se_sess
= cmd
->se_sess
;
1492 struct se_node_acl
*dest_node_acl
= NULL
;
1493 struct se_dev_entry
*dest_se_deve
= NULL
, *local_se_deve
;
1494 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1495 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1496 LIST_HEAD(tid_dest_list
);
1497 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1498 struct target_core_fabric_ops
*tmp_tf_ops
;
1500 unsigned char *ptr
, *i_str
= NULL
, proto_ident
, tmp_proto_ident
;
1501 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
1502 u32 tpdl
, tid_len
= 0;
1503 int ret
, dest_local_nexus
, prf_isid
;
1506 memset(dest_iport
, 0, 64);
1508 local_se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
1510 * Allocate a struct pr_transport_id_holder and setup the
1511 * local_node_acl and local_se_deve pointers and add to
1512 * struct list_head tid_dest_list for add registration
1513 * processing in the loop of tid_dest_list below.
1515 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1517 pr_err("Unable to allocate tidh_new\n");
1518 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1521 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1522 tidh_new
->dest_tpg
= tpg
;
1523 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1524 tidh_new
->dest_se_deve
= local_se_deve
;
1526 local_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1527 se_sess
->se_node_acl
, local_se_deve
, l_isid
,
1528 sa_res_key
, all_tg_pt
, aptpl
);
1529 if (!local_pr_reg
) {
1531 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1534 tidh_new
->dest_pr_reg
= local_pr_reg
;
1536 * The local I_T nexus does not hold any configfs dependances,
1537 * so we set tid_h->dest_local_nexus=1 to prevent the
1538 * configfs_undepend_item() calls in the tid_dest_list loops below.
1540 tidh_new
->dest_local_nexus
= 1;
1541 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1543 if (cmd
->data_length
< 28) {
1544 pr_warn("SPC-PR: Received PR OUT parameter list"
1545 " length too small: %u\n", cmd
->data_length
);
1546 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1551 buf
= transport_kmap_data_sg(cmd
);
1553 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1554 * first extract TransportID Parameter Data Length, and make sure
1555 * the value matches up to the SCSI expected data transfer length.
1557 tpdl
= (buf
[24] & 0xff) << 24;
1558 tpdl
|= (buf
[25] & 0xff) << 16;
1559 tpdl
|= (buf
[26] & 0xff) << 8;
1560 tpdl
|= buf
[27] & 0xff;
1562 if ((tpdl
+ 28) != cmd
->data_length
) {
1563 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1564 " does not equal CDB data_length: %u\n", tpdl
,
1566 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1571 * Start processing the received transport IDs using the
1572 * receiving I_T Nexus portal's fabric dependent methods to
1573 * obtain the SCSI Initiator Port/Device Identifiers.
1578 proto_ident
= (ptr
[0] & 0x0f);
1581 spin_lock(&dev
->se_port_lock
);
1582 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1583 tmp_tpg
= tmp_port
->sep_tpg
;
1586 tmp_tf_ops
= tmp_tpg
->se_tpg_tfo
;
1589 if (!tmp_tf_ops
->get_fabric_proto_ident
||
1590 !tmp_tf_ops
->tpg_parse_pr_out_transport_id
)
1593 * Look for the matching proto_ident provided by
1594 * the received TransportID
1596 tmp_proto_ident
= tmp_tf_ops
->get_fabric_proto_ident(tmp_tpg
);
1597 if (tmp_proto_ident
!= proto_ident
)
1599 dest_rtpi
= tmp_port
->sep_rtpi
;
1601 i_str
= tmp_tf_ops
->tpg_parse_pr_out_transport_id(
1602 tmp_tpg
, (const char *)ptr
, &tid_len
,
1607 atomic_inc(&tmp_tpg
->tpg_pr_ref_count
);
1608 smp_mb__after_atomic_inc();
1609 spin_unlock(&dev
->se_port_lock
);
1611 ret
= core_scsi3_tpg_depend_item(tmp_tpg
);
1613 pr_err(" core_scsi3_tpg_depend_item()"
1615 atomic_dec(&tmp_tpg
->tpg_pr_ref_count
);
1616 smp_mb__after_atomic_dec();
1617 cmd
->scsi_sense_reason
=
1618 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1623 * Locate the desination initiator ACL to be registered
1624 * from the decoded fabric module specific TransportID
1627 spin_lock_irq(&tmp_tpg
->acl_node_lock
);
1628 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1630 if (dest_node_acl
) {
1631 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
1632 smp_mb__after_atomic_inc();
1634 spin_unlock_irq(&tmp_tpg
->acl_node_lock
);
1636 if (!dest_node_acl
) {
1637 core_scsi3_tpg_undepend_item(tmp_tpg
);
1638 spin_lock(&dev
->se_port_lock
);
1642 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
1644 pr_err("configfs_depend_item() failed"
1645 " for dest_node_acl->acl_group\n");
1646 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
1647 smp_mb__after_atomic_dec();
1648 core_scsi3_tpg_undepend_item(tmp_tpg
);
1649 cmd
->scsi_sense_reason
=
1650 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1656 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1657 " %s Port RTPI: %hu\n",
1658 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1659 dest_node_acl
->initiatorname
, dest_rtpi
);
1661 spin_lock(&dev
->se_port_lock
);
1664 spin_unlock(&dev
->se_port_lock
);
1667 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1669 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1674 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1675 " tid_len: %d for %s + %s\n",
1676 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1677 tpdl
, tid_len
, i_str
, iport_ptr
);
1679 if (tid_len
> tpdl
) {
1680 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1681 " %u for Transport ID: %s\n", tid_len
, ptr
);
1682 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1683 core_scsi3_tpg_undepend_item(dest_tpg
);
1684 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1689 * Locate the desintation struct se_dev_entry pointer for matching
1690 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1693 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1695 if (!dest_se_deve
) {
1696 pr_err("Unable to locate %s dest_se_deve"
1697 " from destination RTPI: %hu\n",
1698 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1701 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1702 core_scsi3_tpg_undepend_item(dest_tpg
);
1703 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1708 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
1710 pr_err("core_scsi3_lunacl_depend_item()"
1712 atomic_dec(&dest_se_deve
->pr_ref_count
);
1713 smp_mb__after_atomic_dec();
1714 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1715 core_scsi3_tpg_undepend_item(dest_tpg
);
1716 cmd
->scsi_sense_reason
=
1717 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1722 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1723 " dest_se_deve mapped_lun: %u\n",
1724 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1725 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1728 * Skip any TransportIDs that already have a registration for
1731 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1734 core_scsi3_put_pr_reg(pr_reg_e
);
1735 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1736 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1737 core_scsi3_tpg_undepend_item(dest_tpg
);
1744 * Allocate a struct pr_transport_id_holder and setup
1745 * the dest_node_acl and dest_se_deve pointers for the
1748 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1751 pr_err("Unable to allocate tidh_new\n");
1752 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1753 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1754 core_scsi3_tpg_undepend_item(dest_tpg
);
1755 cmd
->scsi_sense_reason
=
1756 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1760 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1761 tidh_new
->dest_tpg
= dest_tpg
;
1762 tidh_new
->dest_node_acl
= dest_node_acl
;
1763 tidh_new
->dest_se_deve
= dest_se_deve
;
1766 * Allocate, but do NOT add the registration for the
1767 * TransportID referenced SCSI Initiator port. This
1768 * done because of the following from spc4r17 in section
1769 * 6.14.3 wrt SPEC_I_PT:
1771 * "If a registration fails for any initiator port (e.g., if th
1772 * logical unit does not have enough resources available to
1773 * hold the registration information), no registrations shall be
1774 * made, and the command shall be terminated with
1775 * CHECK CONDITION status."
1777 * That means we call __core_scsi3_alloc_registration() here,
1778 * and then call __core_scsi3_add_registration() in the
1779 * 2nd loop which will never fail.
1781 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1782 dest_node_acl
, dest_se_deve
, iport_ptr
,
1783 sa_res_key
, all_tg_pt
, aptpl
);
1785 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1786 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1787 core_scsi3_tpg_undepend_item(dest_tpg
);
1789 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1793 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1794 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1802 transport_kunmap_data_sg(cmd
);
1805 * Go ahead and create a registrations from tid_dest_list for the
1806 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1809 * The SA Reservation Key from the PROUT is set for the
1810 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1811 * means that the TransportID Initiator port will be
1812 * registered on all of the target ports in the SCSI target device
1813 * ALL_TG_PT=0 means the registration will only be for the
1814 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1817 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1818 dest_tpg
= tidh
->dest_tpg
;
1819 dest_node_acl
= tidh
->dest_node_acl
;
1820 dest_se_deve
= tidh
->dest_se_deve
;
1821 dest_pr_reg
= tidh
->dest_pr_reg
;
1822 dest_local_nexus
= tidh
->dest_local_nexus
;
1824 list_del(&tidh
->dest_list
);
1827 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1828 prf_isid
= core_pr_dump_initiator_port(dest_pr_reg
, &i_buf
[0],
1829 PR_REG_ISID_ID_LEN
);
1831 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1834 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1835 " registered Transport ID for Node: %s%s Mapped LUN:"
1836 " %u\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1837 dest_node_acl
->initiatorname
, (prf_isid
) ?
1838 &i_buf
[0] : "", dest_se_deve
->mapped_lun
);
1840 if (dest_local_nexus
)
1843 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1844 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1845 core_scsi3_tpg_undepend_item(dest_tpg
);
1850 transport_kunmap_data_sg(cmd
);
1852 * For the failure case, release everything from tid_dest_list
1853 * including *dest_pr_reg and the configfs dependances..
1855 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1856 dest_tpg
= tidh
->dest_tpg
;
1857 dest_node_acl
= tidh
->dest_node_acl
;
1858 dest_se_deve
= tidh
->dest_se_deve
;
1859 dest_pr_reg
= tidh
->dest_pr_reg
;
1860 dest_local_nexus
= tidh
->dest_local_nexus
;
1862 list_del(&tidh
->dest_list
);
1865 * Release any extra ALL_TG_PT=1 registrations for
1866 * the SPEC_I_PT=1 case.
1868 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1869 &dest_pr_reg
->pr_reg_atp_list
,
1870 pr_reg_atp_mem_list
) {
1871 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1872 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1873 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1876 kfree(dest_pr_reg
->pr_aptpl_buf
);
1877 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1879 if (dest_local_nexus
)
1882 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1883 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1884 core_scsi3_tpg_undepend_item(dest_tpg
);
1890 * Called with struct se_device->dev_reservation_lock held
1892 static int __core_scsi3_update_aptpl_buf(
1893 struct se_device
*dev
,
1895 u32 pr_aptpl_buf_len
,
1896 int clear_aptpl_metadata
)
1899 struct se_portal_group
*tpg
;
1900 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1901 struct t10_pr_registration
*pr_reg
;
1902 unsigned char tmp
[512], isid_buf
[32];
1906 memset(buf
, 0, pr_aptpl_buf_len
);
1908 * Called to clear metadata once APTPL has been deactivated.
1910 if (clear_aptpl_metadata
) {
1911 snprintf(buf
, pr_aptpl_buf_len
,
1912 "No Registrations or Reservations\n");
1916 * Walk the registration list..
1918 spin_lock(&su_dev
->t10_pr
.registration_lock
);
1919 list_for_each_entry(pr_reg
, &su_dev
->t10_pr
.registration_list
,
1924 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1925 lun
= pr_reg
->pr_reg_tg_pt_lun
;
1927 * Write out any ISID value to APTPL metadata that was included
1928 * in the original registration.
1930 if (pr_reg
->isid_present_at_reg
)
1931 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1932 pr_reg
->pr_reg_isid
);
1934 * Include special metadata if the pr_reg matches the
1935 * reservation holder.
1937 if (dev
->dev_pr_res_holder
== pr_reg
) {
1938 snprintf(tmp
, 512, "PR_REG_START: %d"
1939 "\ninitiator_fabric=%s\n"
1940 "initiator_node=%s\n%s"
1942 "res_holder=1\nres_type=%02x\n"
1943 "res_scope=%02x\nres_all_tg_pt=%d\n"
1944 "mapped_lun=%u\n", reg_count
,
1945 tpg
->se_tpg_tfo
->get_fabric_name(),
1946 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1947 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1948 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1949 pr_reg
->pr_res_mapped_lun
);
1951 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1952 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1953 "sa_res_key=%llu\nres_holder=0\n"
1954 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1955 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1956 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1957 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1958 pr_reg
->pr_res_mapped_lun
);
1961 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1962 pr_err("Unable to update renaming"
1963 " APTPL metadata\n");
1964 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1967 len
+= sprintf(buf
+len
, "%s", tmp
);
1970 * Include information about the associated SCSI target port.
1972 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1973 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1974 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1975 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1976 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1977 lun
->lun_sep
->sep_rtpi
, lun
->unpacked_lun
, reg_count
);
1979 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1980 pr_err("Unable to update renaming"
1981 " APTPL metadata\n");
1982 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1985 len
+= sprintf(buf
+len
, "%s", tmp
);
1988 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1991 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1996 static int core_scsi3_update_aptpl_buf(
1997 struct se_device
*dev
,
1999 u32 pr_aptpl_buf_len
,
2000 int clear_aptpl_metadata
)
2004 spin_lock(&dev
->dev_reservation_lock
);
2005 ret
= __core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
2006 clear_aptpl_metadata
);
2007 spin_unlock(&dev
->dev_reservation_lock
);
2013 * Called with struct se_device->aptpl_file_mutex held
2015 static int __core_scsi3_write_aptpl_to_file(
2016 struct se_device
*dev
,
2018 u32 pr_aptpl_buf_len
)
2020 struct t10_wwn
*wwn
= &dev
->se_sub_dev
->t10_wwn
;
2022 struct iovec iov
[1];
2023 mm_segment_t old_fs
;
2024 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
2028 memset(iov
, 0, sizeof(struct iovec
));
2029 memset(path
, 0, 512);
2031 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
2032 pr_err("WWN value for struct se_device does not fit"
2033 " into path buffer\n");
2037 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
2038 file
= filp_open(path
, flags
, 0600);
2039 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
2040 pr_err("filp_open(%s) for APTPL metadata"
2042 return IS_ERR(file
) ? PTR_ERR(file
) : -ENOENT
;
2045 iov
[0].iov_base
= &buf
[0];
2046 if (!pr_aptpl_buf_len
)
2047 iov
[0].iov_len
= (strlen(&buf
[0]) + 1); /* Add extra for NULL */
2049 iov
[0].iov_len
= pr_aptpl_buf_len
;
2053 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
2057 pr_debug("Error writing APTPL metadata file: %s\n", path
);
2058 filp_close(file
, NULL
);
2061 filp_close(file
, NULL
);
2066 static int core_scsi3_update_and_write_aptpl(
2067 struct se_device
*dev
,
2068 unsigned char *in_buf
,
2069 u32 in_pr_aptpl_buf_len
)
2071 unsigned char null_buf
[64], *buf
;
2072 u32 pr_aptpl_buf_len
;
2073 int ret
, clear_aptpl_metadata
= 0;
2075 * Can be called with a NULL pointer from PROUT service action CLEAR
2078 memset(null_buf
, 0, 64);
2081 * This will clear the APTPL metadata to:
2082 * "No Registrations or Reservations" status
2084 pr_aptpl_buf_len
= 64;
2085 clear_aptpl_metadata
= 1;
2088 pr_aptpl_buf_len
= in_pr_aptpl_buf_len
;
2091 ret
= core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
2092 clear_aptpl_metadata
);
2096 * __core_scsi3_write_aptpl_to_file() will call strlen()
2097 * on the passed buf to determine pr_aptpl_buf_len.
2099 ret
= __core_scsi3_write_aptpl_to_file(dev
, buf
, 0);
2106 static int core_scsi3_emulate_pro_register(
2115 struct se_session
*se_sess
= cmd
->se_sess
;
2116 struct se_device
*dev
= cmd
->se_dev
;
2117 struct se_dev_entry
*se_deve
;
2118 struct se_lun
*se_lun
= cmd
->se_lun
;
2119 struct se_portal_group
*se_tpg
;
2120 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
, *pr_reg_e
;
2121 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2122 /* Used for APTPL metadata w/ UNREGISTER */
2123 unsigned char *pr_aptpl_buf
= NULL
;
2124 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2125 int pr_holder
= 0, ret
= 0, type
;
2127 if (!se_sess
|| !se_lun
) {
2128 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2129 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2132 se_tpg
= se_sess
->se_tpg
;
2133 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2135 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2136 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2137 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2139 isid_ptr
= &isid_buf
[0];
2142 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2144 pr_reg_e
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2147 pr_warn("SPC-3 PR: Reservation Key non-zero"
2148 " for SA REGISTER, returning CONFLICT\n");
2149 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2153 * Do nothing but return GOOD status.
2160 * Perform the Service Action REGISTER on the Initiator
2161 * Port Endpoint that the PRO was received from on the
2162 * Logical Unit of the SCSI device server.
2164 ret
= core_scsi3_alloc_registration(cmd
->se_dev
,
2165 se_sess
->se_node_acl
, se_deve
, isid_ptr
,
2166 sa_res_key
, all_tg_pt
, aptpl
,
2169 pr_err("Unable to allocate"
2170 " struct t10_pr_registration\n");
2171 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
2176 * Register both the Initiator port that received
2177 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2178 * TransportID from Parameter list and loop through
2179 * fabric dependent parameter list while calling
2180 * logic from of core_scsi3_alloc_registration() for
2181 * each TransportID provided SCSI Initiator Port/Device
2183 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2184 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2189 * Nothing left to do for the APTPL=0 case.
2192 pr_tmpl
->pr_aptpl_active
= 0;
2193 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2194 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2199 * Locate the newly allocated local I_T Nexus *pr_reg, and
2200 * update the APTPL metadata information using its
2201 * preallocated *pr_reg->pr_aptpl_buf.
2203 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2204 se_sess
->se_node_acl
, se_sess
);
2206 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2207 &pr_reg
->pr_aptpl_buf
[0],
2208 pr_tmpl
->pr_aptpl_buf_len
);
2210 pr_tmpl
->pr_aptpl_active
= 1;
2211 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2214 core_scsi3_put_pr_reg(pr_reg
);
2218 * Locate the existing *pr_reg via struct se_node_acl pointers
2221 type
= pr_reg
->pr_res_type
;
2224 if (res_key
!= pr_reg
->pr_res_key
) {
2225 pr_err("SPC-3 PR REGISTER: Received"
2226 " res_key: 0x%016Lx does not match"
2227 " existing SA REGISTER res_key:"
2228 " 0x%016Lx\n", res_key
,
2229 pr_reg
->pr_res_key
);
2230 core_scsi3_put_pr_reg(pr_reg
);
2231 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2236 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2237 " set while sa_res_key=0\n");
2238 core_scsi3_put_pr_reg(pr_reg
);
2239 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
2243 * An existing ALL_TG_PT=1 registration being released
2244 * must also set ALL_TG_PT=1 in the incoming PROUT.
2246 if (pr_reg
->pr_reg_all_tg_pt
&& !(all_tg_pt
)) {
2247 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2248 " registration exists, but ALL_TG_PT=1 bit not"
2249 " present in received PROUT\n");
2250 core_scsi3_put_pr_reg(pr_reg
);
2251 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
2255 * Allocate APTPL metadata buffer used for UNREGISTER ops
2258 pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
,
2260 if (!pr_aptpl_buf
) {
2261 pr_err("Unable to allocate"
2263 core_scsi3_put_pr_reg(pr_reg
);
2264 cmd
->scsi_sense_reason
=
2265 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2270 * sa_res_key=0 Unregister Reservation Key for registered I_T
2271 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2275 pr_holder
= core_scsi3_check_implict_release(
2276 cmd
->se_dev
, pr_reg
);
2277 if (pr_holder
< 0) {
2278 kfree(pr_aptpl_buf
);
2279 core_scsi3_put_pr_reg(pr_reg
);
2280 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2284 spin_lock(&pr_tmpl
->registration_lock
);
2286 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2287 * and matching pr_res_key.
2289 if (pr_reg
->pr_reg_all_tg_pt
) {
2290 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2291 &pr_tmpl
->registration_list
,
2294 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2297 if (pr_reg_p
->pr_res_key
!= res_key
)
2300 if (pr_reg
== pr_reg_p
)
2303 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2304 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2307 __core_scsi3_free_registration(dev
,
2312 * Release the calling I_T Nexus registration now..
2314 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
,
2317 * From spc4r17, section 5.7.11.3 Unregistering
2319 * If the persistent reservation is a registrants only
2320 * type, the device server shall establish a unit
2321 * attention condition for the initiator port associated
2322 * with every registered I_T nexus except for the I_T
2323 * nexus on which the PERSISTENT RESERVE OUT command was
2324 * received, with the additional sense code set to
2325 * RESERVATIONS RELEASED.
2328 ((type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
2329 (type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
))) {
2330 list_for_each_entry(pr_reg_p
,
2331 &pr_tmpl
->registration_list
,
2334 core_scsi3_ua_allocate(
2335 pr_reg_p
->pr_reg_nacl
,
2336 pr_reg_p
->pr_res_mapped_lun
,
2338 ASCQ_2AH_RESERVATIONS_RELEASED
);
2341 spin_unlock(&pr_tmpl
->registration_lock
);
2344 pr_tmpl
->pr_aptpl_active
= 0;
2345 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2346 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2347 " for UNREGISTER\n");
2351 ret
= core_scsi3_update_and_write_aptpl(dev
,
2353 pr_tmpl
->pr_aptpl_buf_len
);
2355 pr_tmpl
->pr_aptpl_active
= 1;
2356 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2357 " for UNREGISTER\n");
2360 kfree(pr_aptpl_buf
);
2364 * Increment PRgeneration counter for struct se_device"
2365 * upon a successful REGISTER, see spc4r17 section 6.3.2
2366 * READ_KEYS service action.
2368 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(
2370 pr_reg
->pr_res_key
= sa_res_key
;
2371 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2372 " Key for %s to: 0x%016Lx PRgeneration:"
2373 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2374 (ignore_key
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2375 pr_reg
->pr_reg_nacl
->initiatorname
,
2376 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2379 pr_tmpl
->pr_aptpl_active
= 0;
2380 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2381 core_scsi3_put_pr_reg(pr_reg
);
2382 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2387 ret
= core_scsi3_update_and_write_aptpl(dev
,
2389 pr_tmpl
->pr_aptpl_buf_len
);
2391 pr_tmpl
->pr_aptpl_active
= 1;
2392 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2396 kfree(pr_aptpl_buf
);
2397 core_scsi3_put_pr_reg(pr_reg
);
2403 unsigned char *core_scsi3_pr_dump_type(int type
)
2406 case PR_TYPE_WRITE_EXCLUSIVE
:
2407 return "Write Exclusive Access";
2408 case PR_TYPE_EXCLUSIVE_ACCESS
:
2409 return "Exclusive Access";
2410 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2411 return "Write Exclusive Access, Registrants Only";
2412 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2413 return "Exclusive Access, Registrants Only";
2414 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2415 return "Write Exclusive Access, All Registrants";
2416 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2417 return "Exclusive Access, All Registrants";
2422 return "Unknown SPC-3 PR Type";
2425 static int core_scsi3_pro_reserve(
2427 struct se_device
*dev
,
2432 struct se_session
*se_sess
= cmd
->se_sess
;
2433 struct se_lun
*se_lun
= cmd
->se_lun
;
2434 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2435 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2436 char i_buf
[PR_REG_ISID_ID_LEN
];
2439 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2441 if (!se_sess
|| !se_lun
) {
2442 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2443 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2447 * Locate the existing *pr_reg via struct se_node_acl pointers
2449 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2452 pr_err("SPC-3 PR: Unable to locate"
2453 " PR_REGISTERED *pr_reg for RESERVE\n");
2454 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2458 * From spc4r17 Section 5.7.9: Reserving:
2460 * An application client creates a persistent reservation by issuing
2461 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2462 * a registered I_T nexus with the following parameters:
2463 * a) RESERVATION KEY set to the value of the reservation key that is
2464 * registered with the logical unit for the I_T nexus; and
2466 if (res_key
!= pr_reg
->pr_res_key
) {
2467 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2468 " does not match existing SA REGISTER res_key:"
2469 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2470 core_scsi3_put_pr_reg(pr_reg
);
2471 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2475 * From spc4r17 Section 5.7.9: Reserving:
2478 * b) TYPE field and SCOPE field set to the persistent reservation
2481 * Only one persistent reservation is allowed at a time per logical unit
2482 * and that persistent reservation has a scope of LU_SCOPE.
2484 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2485 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2486 core_scsi3_put_pr_reg(pr_reg
);
2487 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
2491 * See if we have an existing PR reservation holder pointer at
2492 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2495 spin_lock(&dev
->dev_reservation_lock
);
2496 pr_res_holder
= dev
->dev_pr_res_holder
;
2497 if (pr_res_holder
) {
2499 * From spc4r17 Section 5.7.9: Reserving:
2501 * If the device server receives a PERSISTENT RESERVE OUT
2502 * command from an I_T nexus other than a persistent reservation
2503 * holder (see 5.7.10) that attempts to create a persistent
2504 * reservation when a persistent reservation already exists for
2505 * the logical unit, then the command shall be completed with
2506 * RESERVATION CONFLICT status.
2508 if (pr_res_holder
!= pr_reg
) {
2509 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2510 pr_err("SPC-3 PR: Attempted RESERVE from"
2511 " [%s]: %s while reservation already held by"
2512 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2513 cmd
->se_tfo
->get_fabric_name(),
2514 se_sess
->se_node_acl
->initiatorname
,
2515 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2516 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2518 spin_unlock(&dev
->dev_reservation_lock
);
2519 core_scsi3_put_pr_reg(pr_reg
);
2520 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2524 * From spc4r17 Section 5.7.9: Reserving:
2526 * If a persistent reservation holder attempts to modify the
2527 * type or scope of an existing persistent reservation, the
2528 * command shall be completed with RESERVATION CONFLICT status.
2530 if ((pr_res_holder
->pr_res_type
!= type
) ||
2531 (pr_res_holder
->pr_res_scope
!= scope
)) {
2532 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2533 pr_err("SPC-3 PR: Attempted RESERVE from"
2534 " [%s]: %s trying to change TYPE and/or SCOPE,"
2535 " while reservation already held by [%s]: %s,"
2536 " returning RESERVATION_CONFLICT\n",
2537 cmd
->se_tfo
->get_fabric_name(),
2538 se_sess
->se_node_acl
->initiatorname
,
2539 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2540 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2542 spin_unlock(&dev
->dev_reservation_lock
);
2543 core_scsi3_put_pr_reg(pr_reg
);
2544 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2548 * From spc4r17 Section 5.7.9: Reserving:
2550 * If the device server receives a PERSISTENT RESERVE OUT
2551 * command with RESERVE service action where the TYPE field and
2552 * the SCOPE field contain the same values as the existing type
2553 * and scope from a persistent reservation holder, it shall not
2554 * make any change to the existing persistent reservation and
2555 * shall completethe command with GOOD status.
2557 spin_unlock(&dev
->dev_reservation_lock
);
2558 core_scsi3_put_pr_reg(pr_reg
);
2562 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2563 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2565 pr_reg
->pr_res_scope
= scope
;
2566 pr_reg
->pr_res_type
= type
;
2567 pr_reg
->pr_res_holder
= 1;
2568 dev
->dev_pr_res_holder
= pr_reg
;
2569 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2570 PR_REG_ISID_ID_LEN
);
2572 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2573 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2574 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2575 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2576 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2577 cmd
->se_tfo
->get_fabric_name(),
2578 se_sess
->se_node_acl
->initiatorname
,
2579 (prf_isid
) ? &i_buf
[0] : "");
2580 spin_unlock(&dev
->dev_reservation_lock
);
2582 if (pr_tmpl
->pr_aptpl_active
) {
2583 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2584 &pr_reg
->pr_aptpl_buf
[0],
2585 pr_tmpl
->pr_aptpl_buf_len
);
2587 pr_debug("SPC-3 PR: Updated APTPL metadata"
2591 core_scsi3_put_pr_reg(pr_reg
);
2595 static int core_scsi3_emulate_pro_reserve(
2601 struct se_device
*dev
= cmd
->se_dev
;
2605 case PR_TYPE_WRITE_EXCLUSIVE
:
2606 case PR_TYPE_EXCLUSIVE_ACCESS
:
2607 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2608 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2609 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2610 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2611 ret
= core_scsi3_pro_reserve(cmd
, dev
, type
, scope
, res_key
);
2614 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2616 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
2624 * Called with struct se_device->dev_reservation_lock held.
2626 static void __core_scsi3_complete_pro_release(
2627 struct se_device
*dev
,
2628 struct se_node_acl
*se_nacl
,
2629 struct t10_pr_registration
*pr_reg
,
2632 struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2633 char i_buf
[PR_REG_ISID_ID_LEN
];
2636 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2637 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2638 PR_REG_ISID_ID_LEN
);
2640 * Go ahead and release the current PR reservation holder.
2642 dev
->dev_pr_res_holder
= NULL
;
2644 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2645 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2646 tfo
->get_fabric_name(), (explict
) ? "explict" : "implict",
2647 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
2648 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2649 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2650 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2651 (prf_isid
) ? &i_buf
[0] : "");
2653 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2655 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2658 static int core_scsi3_emulate_pro_release(
2664 struct se_device
*dev
= cmd
->se_dev
;
2665 struct se_session
*se_sess
= cmd
->se_sess
;
2666 struct se_lun
*se_lun
= cmd
->se_lun
;
2667 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2668 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2669 int ret
, all_reg
= 0;
2671 if (!se_sess
|| !se_lun
) {
2672 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2673 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2677 * Locate the existing *pr_reg via struct se_node_acl pointers
2679 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2681 pr_err("SPC-3 PR: Unable to locate"
2682 " PR_REGISTERED *pr_reg for RELEASE\n");
2683 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2687 * From spc4r17 Section 5.7.11.2 Releasing:
2689 * If there is no persistent reservation or in response to a persistent
2690 * reservation release request from a registered I_T nexus that is not a
2691 * persistent reservation holder (see 5.7.10), the device server shall
2694 * a) Not release the persistent reservation, if any;
2695 * b) Not remove any registrations; and
2696 * c) Complete the command with GOOD status.
2698 spin_lock(&dev
->dev_reservation_lock
);
2699 pr_res_holder
= dev
->dev_pr_res_holder
;
2700 if (!pr_res_holder
) {
2702 * No persistent reservation, return GOOD status.
2704 spin_unlock(&dev
->dev_reservation_lock
);
2705 core_scsi3_put_pr_reg(pr_reg
);
2708 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2709 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
2712 if ((all_reg
== 0) && (pr_res_holder
!= pr_reg
)) {
2714 * Non 'All Registrants' PR Type cases..
2715 * Release request from a registered I_T nexus that is not a
2716 * persistent reservation holder. return GOOD status.
2718 spin_unlock(&dev
->dev_reservation_lock
);
2719 core_scsi3_put_pr_reg(pr_reg
);
2723 * From spc4r17 Section 5.7.11.2 Releasing:
2725 * Only the persistent reservation holder (see 5.7.10) is allowed to
2726 * release a persistent reservation.
2728 * An application client releases the persistent reservation by issuing
2729 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2730 * an I_T nexus that is a persistent reservation holder with the
2731 * following parameters:
2733 * a) RESERVATION KEY field set to the value of the reservation key
2734 * that is registered with the logical unit for the I_T nexus;
2736 if (res_key
!= pr_reg
->pr_res_key
) {
2737 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2738 " does not match existing SA REGISTER res_key:"
2739 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2740 spin_unlock(&dev
->dev_reservation_lock
);
2741 core_scsi3_put_pr_reg(pr_reg
);
2742 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2746 * From spc4r17 Section 5.7.11.2 Releasing and above:
2748 * b) TYPE field and SCOPE field set to match the persistent
2749 * reservation being released.
2751 if ((pr_res_holder
->pr_res_type
!= type
) ||
2752 (pr_res_holder
->pr_res_scope
!= scope
)) {
2753 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2754 pr_err("SPC-3 PR RELEASE: Attempted to release"
2755 " reservation from [%s]: %s with different TYPE "
2756 "and/or SCOPE while reservation already held by"
2757 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2758 cmd
->se_tfo
->get_fabric_name(),
2759 se_sess
->se_node_acl
->initiatorname
,
2760 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2761 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2763 spin_unlock(&dev
->dev_reservation_lock
);
2764 core_scsi3_put_pr_reg(pr_reg
);
2765 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2769 * In response to a persistent reservation release request from the
2770 * persistent reservation holder the device server shall perform a
2771 * release by doing the following as an uninterrupted series of actions:
2772 * a) Release the persistent reservation;
2773 * b) Not remove any registration(s);
2774 * c) If the released persistent reservation is a registrants only type
2775 * or all registrants type persistent reservation,
2776 * the device server shall establish a unit attention condition for
2777 * the initiator port associated with every regis-
2778 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2779 * RESERVE OUT command with RELEASE service action was received,
2780 * with the additional sense code set to RESERVATIONS RELEASED; and
2781 * d) If the persistent reservation is of any other type, the device
2782 * server shall not establish a unit attention condition.
2784 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2787 spin_unlock(&dev
->dev_reservation_lock
);
2789 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2790 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2791 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2792 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2794 * If no UNIT ATTENTION conditions will be established for
2795 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2796 * go ahead and check for APTPL=1 update+write below
2801 spin_lock(&pr_tmpl
->registration_lock
);
2802 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2805 * Do not establish a UNIT ATTENTION condition
2806 * for the calling I_T Nexus
2808 if (pr_reg_p
== pr_reg
)
2811 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2812 pr_reg_p
->pr_res_mapped_lun
,
2813 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2815 spin_unlock(&pr_tmpl
->registration_lock
);
2818 if (pr_tmpl
->pr_aptpl_active
) {
2819 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2820 &pr_reg
->pr_aptpl_buf
[0],
2821 pr_tmpl
->pr_aptpl_buf_len
);
2823 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2826 core_scsi3_put_pr_reg(pr_reg
);
2830 static int core_scsi3_emulate_pro_clear(
2834 struct se_device
*dev
= cmd
->se_dev
;
2835 struct se_node_acl
*pr_reg_nacl
;
2836 struct se_session
*se_sess
= cmd
->se_sess
;
2837 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2838 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2839 u32 pr_res_mapped_lun
= 0;
2840 int calling_it_nexus
= 0;
2842 * Locate the existing *pr_reg via struct se_node_acl pointers
2844 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2845 se_sess
->se_node_acl
, se_sess
);
2847 pr_err("SPC-3 PR: Unable to locate"
2848 " PR_REGISTERED *pr_reg for CLEAR\n");
2849 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2853 * From spc4r17 section 5.7.11.6, Clearing:
2855 * Any application client may release the persistent reservation and
2856 * remove all registrations from a device server by issuing a
2857 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2858 * registered I_T nexus with the following parameter:
2860 * a) RESERVATION KEY field set to the value of the reservation key
2861 * that is registered with the logical unit for the I_T nexus.
2863 if (res_key
!= pr_reg_n
->pr_res_key
) {
2864 pr_err("SPC-3 PR REGISTER: Received"
2865 " res_key: 0x%016Lx does not match"
2866 " existing SA REGISTER res_key:"
2867 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2868 core_scsi3_put_pr_reg(pr_reg_n
);
2869 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2873 * a) Release the persistent reservation, if any;
2875 spin_lock(&dev
->dev_reservation_lock
);
2876 pr_res_holder
= dev
->dev_pr_res_holder
;
2877 if (pr_res_holder
) {
2878 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2879 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2882 spin_unlock(&dev
->dev_reservation_lock
);
2884 * b) Remove all registration(s) (see spc4r17 5.7.7);
2886 spin_lock(&pr_tmpl
->registration_lock
);
2887 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2888 &pr_tmpl
->registration_list
, pr_reg_list
) {
2890 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2891 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2892 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2893 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2896 * e) Establish a unit attention condition for the initiator
2897 * port associated with every registered I_T nexus other
2898 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2899 * command with CLEAR service action was received, with the
2900 * additional sense code set to RESERVATIONS PREEMPTED.
2902 if (!calling_it_nexus
)
2903 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2904 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2906 spin_unlock(&pr_tmpl
->registration_lock
);
2908 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2909 cmd
->se_tfo
->get_fabric_name());
2911 if (pr_tmpl
->pr_aptpl_active
) {
2912 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2913 pr_debug("SPC-3 PR: Updated APTPL metadata"
2917 core_scsi3_pr_generation(dev
);
2922 * Called with struct se_device->dev_reservation_lock held.
2924 static void __core_scsi3_complete_pro_preempt(
2925 struct se_device
*dev
,
2926 struct t10_pr_registration
*pr_reg
,
2927 struct list_head
*preempt_and_abort_list
,
2932 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2933 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2934 char i_buf
[PR_REG_ISID_ID_LEN
];
2937 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2938 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2939 PR_REG_ISID_ID_LEN
);
2941 * Do an implict RELEASE of the existing reservation.
2943 if (dev
->dev_pr_res_holder
)
2944 __core_scsi3_complete_pro_release(dev
, nacl
,
2945 dev
->dev_pr_res_holder
, 0);
2947 dev
->dev_pr_res_holder
= pr_reg
;
2948 pr_reg
->pr_res_holder
= 1;
2949 pr_reg
->pr_res_type
= type
;
2950 pr_reg
->pr_res_scope
= scope
;
2952 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2953 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2954 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2955 core_scsi3_pr_dump_type(type
),
2956 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2957 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2958 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2959 nacl
->initiatorname
, (prf_isid
) ? &i_buf
[0] : "");
2961 * For PREEMPT_AND_ABORT, add the preempting reservation's
2962 * struct t10_pr_registration to the list that will be compared
2963 * against received CDBs..
2965 if (preempt_and_abort_list
)
2966 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2967 preempt_and_abort_list
);
2970 static void core_scsi3_release_preempt_and_abort(
2971 struct list_head
*preempt_and_abort_list
,
2972 struct t10_pr_registration
*pr_reg_holder
)
2974 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2976 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2977 pr_reg_abort_list
) {
2979 list_del(&pr_reg
->pr_reg_abort_list
);
2980 if (pr_reg_holder
== pr_reg
)
2982 if (pr_reg
->pr_res_holder
) {
2983 pr_warn("pr_reg->pr_res_holder still set\n");
2987 pr_reg
->pr_reg_deve
= NULL
;
2988 pr_reg
->pr_reg_nacl
= NULL
;
2989 kfree(pr_reg
->pr_aptpl_buf
);
2990 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2994 static int core_scsi3_pro_preempt(
3002 struct se_device
*dev
= cmd
->se_dev
;
3003 struct se_node_acl
*pr_reg_nacl
;
3004 struct se_session
*se_sess
= cmd
->se_sess
;
3005 LIST_HEAD(preempt_and_abort_list
);
3006 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
3007 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
3008 u32 pr_res_mapped_lun
= 0;
3009 int all_reg
= 0, calling_it_nexus
= 0, released_regs
= 0;
3010 int prh_type
= 0, prh_scope
= 0, ret
;
3013 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3017 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3020 pr_err("SPC-3 PR: Unable to locate"
3021 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3022 (abort
) ? "_AND_ABORT" : "");
3023 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3026 if (pr_reg_n
->pr_res_key
!= res_key
) {
3027 core_scsi3_put_pr_reg(pr_reg_n
);
3028 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3031 if (scope
!= PR_SCOPE_LU_SCOPE
) {
3032 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
3033 core_scsi3_put_pr_reg(pr_reg_n
);
3034 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3038 spin_lock(&dev
->dev_reservation_lock
);
3039 pr_res_holder
= dev
->dev_pr_res_holder
;
3040 if (pr_res_holder
&&
3041 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3042 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
3045 if (!all_reg
&& !sa_res_key
) {
3046 spin_unlock(&dev
->dev_reservation_lock
);
3047 core_scsi3_put_pr_reg(pr_reg_n
);
3048 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3052 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3054 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3055 * persistent reservation holder or there is no persistent reservation
3056 * holder (i.e., there is no persistent reservation), then the device
3057 * server shall perform a preempt by doing the following in an
3058 * uninterrupted series of actions. (See below..)
3060 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
3062 * No existing or SA Reservation Key matching reservations..
3064 * PROUT SA PREEMPT with All Registrant type reservations are
3065 * allowed to be processed without a matching SA Reservation Key
3067 spin_lock(&pr_tmpl
->registration_lock
);
3068 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3069 &pr_tmpl
->registration_list
, pr_reg_list
) {
3071 * Removing of registrations in non all registrants
3072 * type reservations without a matching SA reservation
3075 * a) Remove the registrations for all I_T nexuses
3076 * specified by the SERVICE ACTION RESERVATION KEY
3078 * b) Ignore the contents of the SCOPE and TYPE fields;
3079 * c) Process tasks as defined in 5.7.1; and
3080 * d) Establish a unit attention condition for the
3081 * initiator port associated with every I_T nexus
3082 * that lost its registration other than the I_T
3083 * nexus on which the PERSISTENT RESERVE OUT command
3084 * was received, with the additional sense code set
3085 * to REGISTRATIONS PREEMPTED.
3088 if (pr_reg
->pr_res_key
!= sa_res_key
)
3091 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3092 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3093 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3094 __core_scsi3_free_registration(dev
, pr_reg
,
3095 (abort
) ? &preempt_and_abort_list
:
3096 NULL
, calling_it_nexus
);
3100 * Case for any existing all registrants type
3101 * reservation, follow logic in spc4r17 section
3102 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3104 * For a ZERO SA Reservation key, release
3105 * all other registrations and do an implict
3106 * release of active persistent reservation.
3108 * For a non-ZERO SA Reservation key, only
3109 * release the matching reservation key from
3113 (pr_reg
->pr_res_key
!= sa_res_key
))
3116 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3117 if (calling_it_nexus
)
3120 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3121 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3122 __core_scsi3_free_registration(dev
, pr_reg
,
3123 (abort
) ? &preempt_and_abort_list
:
3127 if (!calling_it_nexus
)
3128 core_scsi3_ua_allocate(pr_reg_nacl
,
3129 pr_res_mapped_lun
, 0x2A,
3130 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3132 spin_unlock(&pr_tmpl
->registration_lock
);
3134 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3135 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3136 * RESERVATION KEY field to a value that does not match any
3137 * registered reservation key, then the device server shall
3138 * complete the command with RESERVATION CONFLICT status.
3140 if (!released_regs
) {
3141 spin_unlock(&dev
->dev_reservation_lock
);
3142 core_scsi3_put_pr_reg(pr_reg_n
);
3143 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3147 * For an existing all registrants type reservation
3148 * with a zero SA rservation key, preempt the existing
3149 * reservation with the new PR type and scope.
3151 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
3152 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3153 (abort
) ? &preempt_and_abort_list
: NULL
,
3154 type
, scope
, abort
);
3157 core_scsi3_release_preempt_and_abort(
3158 &preempt_and_abort_list
, pr_reg_n
);
3160 spin_unlock(&dev
->dev_reservation_lock
);
3162 if (pr_tmpl
->pr_aptpl_active
) {
3163 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3164 &pr_reg_n
->pr_aptpl_buf
[0],
3165 pr_tmpl
->pr_aptpl_buf_len
);
3167 pr_debug("SPC-3 PR: Updated APTPL"
3168 " metadata for PREEMPT%s\n", (abort
) ?
3172 core_scsi3_put_pr_reg(pr_reg_n
);
3173 core_scsi3_pr_generation(cmd
->se_dev
);
3177 * The PREEMPTing SA reservation key matches that of the
3178 * existing persistent reservation, first, we check if
3179 * we are preempting our own reservation.
3180 * From spc4r17, section 5.7.11.4.3 Preempting
3181 * persistent reservations and registration handling
3183 * If an all registrants persistent reservation is not
3184 * present, it is not an error for the persistent
3185 * reservation holder to preempt itself (i.e., a
3186 * PERSISTENT RESERVE OUT with a PREEMPT service action
3187 * or a PREEMPT AND ABORT service action with the
3188 * SERVICE ACTION RESERVATION KEY value equal to the
3189 * persistent reservation holder's reservation key that
3190 * is received from the persistent reservation holder).
3191 * In that case, the device server shall establish the
3192 * new persistent reservation and maintain the
3195 prh_type
= pr_res_holder
->pr_res_type
;
3196 prh_scope
= pr_res_holder
->pr_res_scope
;
3198 * If the SERVICE ACTION RESERVATION KEY field identifies a
3199 * persistent reservation holder (see 5.7.10), the device
3200 * server shall perform a preempt by doing the following as
3201 * an uninterrupted series of actions:
3203 * a) Release the persistent reservation for the holder
3204 * identified by the SERVICE ACTION RESERVATION KEY field;
3206 if (pr_reg_n
!= pr_res_holder
)
3207 __core_scsi3_complete_pro_release(dev
,
3208 pr_res_holder
->pr_reg_nacl
,
3209 dev
->dev_pr_res_holder
, 0);
3211 * b) Remove the registrations for all I_T nexuses identified
3212 * by the SERVICE ACTION RESERVATION KEY field, except the
3213 * I_T nexus that is being used for the PERSISTENT RESERVE
3214 * OUT command. If an all registrants persistent reservation
3215 * is present and the SERVICE ACTION RESERVATION KEY field
3216 * is set to zero, then all registrations shall be removed
3217 * except for that of the I_T nexus that is being used for
3218 * the PERSISTENT RESERVE OUT command;
3220 spin_lock(&pr_tmpl
->registration_lock
);
3221 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3222 &pr_tmpl
->registration_list
, pr_reg_list
) {
3224 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3225 if (calling_it_nexus
)
3228 if (pr_reg
->pr_res_key
!= sa_res_key
)
3231 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3232 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3233 __core_scsi3_free_registration(dev
, pr_reg
,
3234 (abort
) ? &preempt_and_abort_list
: NULL
,
3237 * e) Establish a unit attention condition for the initiator
3238 * port associated with every I_T nexus that lost its
3239 * persistent reservation and/or registration, with the
3240 * additional sense code set to REGISTRATIONS PREEMPTED;
3242 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3243 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3245 spin_unlock(&pr_tmpl
->registration_lock
);
3247 * c) Establish a persistent reservation for the preempting
3248 * I_T nexus using the contents of the SCOPE and TYPE fields;
3250 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3251 (abort
) ? &preempt_and_abort_list
: NULL
,
3252 type
, scope
, abort
);
3254 * d) Process tasks as defined in 5.7.1;
3256 * f) If the type or scope has changed, then for every I_T nexus
3257 * whose reservation key was not removed, except for the I_T
3258 * nexus on which the PERSISTENT RESERVE OUT command was
3259 * received, the device server shall establish a unit
3260 * attention condition for the initiator port associated with
3261 * that I_T nexus, with the additional sense code set to
3262 * RESERVATIONS RELEASED. If the type or scope have not
3263 * changed, then no unit attention condition(s) shall be
3264 * established for this reason.
3266 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3267 spin_lock(&pr_tmpl
->registration_lock
);
3268 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3269 &pr_tmpl
->registration_list
, pr_reg_list
) {
3271 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3272 if (calling_it_nexus
)
3275 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3276 pr_reg
->pr_res_mapped_lun
, 0x2A,
3277 ASCQ_2AH_RESERVATIONS_RELEASED
);
3279 spin_unlock(&pr_tmpl
->registration_lock
);
3281 spin_unlock(&dev
->dev_reservation_lock
);
3283 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3284 * All received CDBs for the matching existing reservation and
3285 * registrations undergo ABORT_TASK logic.
3287 * From there, core_scsi3_release_preempt_and_abort() will
3288 * release every registration in the list (which have already
3289 * been removed from the primary pr_reg list), except the
3290 * new persistent reservation holder, the calling Initiator Port.
3293 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3294 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3298 if (pr_tmpl
->pr_aptpl_active
) {
3299 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3300 &pr_reg_n
->pr_aptpl_buf
[0],
3301 pr_tmpl
->pr_aptpl_buf_len
);
3303 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3304 "%s\n", (abort
) ? "_AND_ABORT" : "");
3307 core_scsi3_put_pr_reg(pr_reg_n
);
3308 core_scsi3_pr_generation(cmd
->se_dev
);
3312 static int core_scsi3_emulate_pro_preempt(
3323 case PR_TYPE_WRITE_EXCLUSIVE
:
3324 case PR_TYPE_EXCLUSIVE_ACCESS
:
3325 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3326 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3327 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3328 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3329 ret
= core_scsi3_pro_preempt(cmd
, type
, scope
,
3330 res_key
, sa_res_key
, abort
);
3333 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3334 " Type: 0x%02x\n", (abort
) ? "_AND_ABORT" : "", type
);
3335 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3343 static int core_scsi3_emulate_pro_register_and_move(
3350 struct se_session
*se_sess
= cmd
->se_sess
;
3351 struct se_device
*dev
= cmd
->se_dev
;
3352 struct se_dev_entry
*dest_se_deve
= NULL
;
3353 struct se_lun
*se_lun
= cmd
->se_lun
;
3354 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3355 struct se_port
*se_port
;
3356 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3357 struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3358 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3359 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
3361 unsigned char *initiator_str
;
3362 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
3363 u32 tid_len
, tmp_tid_len
;
3364 int new_reg
= 0, type
, scope
, ret
, matching_iname
, prf_isid
;
3365 unsigned short rtpi
;
3366 unsigned char proto_ident
;
3368 if (!se_sess
|| !se_lun
) {
3369 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3370 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3373 memset(dest_iport
, 0, 64);
3374 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3375 se_tpg
= se_sess
->se_tpg
;
3376 tf_ops
= se_tpg
->se_tpg_tfo
;
3378 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3379 * Register behaviors for a REGISTER AND MOVE service action
3381 * Locate the existing *pr_reg via struct se_node_acl pointers
3383 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3386 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3387 " *pr_reg for REGISTER_AND_MOVE\n");
3388 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3392 * The provided reservation key much match the existing reservation key
3393 * provided during this initiator's I_T nexus registration.
3395 if (res_key
!= pr_reg
->pr_res_key
) {
3396 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3397 " res_key: 0x%016Lx does not match existing SA REGISTER"
3398 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3399 core_scsi3_put_pr_reg(pr_reg
);
3400 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3404 * The service active reservation key needs to be non zero
3407 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3409 core_scsi3_put_pr_reg(pr_reg
);
3410 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3415 * Determine the Relative Target Port Identifier where the reservation
3416 * will be moved to for the TransportID containing SCSI initiator WWN
3419 buf
= transport_kmap_data_sg(cmd
);
3420 rtpi
= (buf
[18] & 0xff) << 8;
3421 rtpi
|= buf
[19] & 0xff;
3422 tid_len
= (buf
[20] & 0xff) << 24;
3423 tid_len
|= (buf
[21] & 0xff) << 16;
3424 tid_len
|= (buf
[22] & 0xff) << 8;
3425 tid_len
|= buf
[23] & 0xff;
3426 transport_kunmap_data_sg(cmd
);
3429 if ((tid_len
+ 24) != cmd
->data_length
) {
3430 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3431 " does not equal CDB data_length: %u\n", tid_len
,
3433 core_scsi3_put_pr_reg(pr_reg
);
3434 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3438 spin_lock(&dev
->se_port_lock
);
3439 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3440 if (se_port
->sep_rtpi
!= rtpi
)
3442 dest_se_tpg
= se_port
->sep_tpg
;
3445 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3449 atomic_inc(&dest_se_tpg
->tpg_pr_ref_count
);
3450 smp_mb__after_atomic_inc();
3451 spin_unlock(&dev
->se_port_lock
);
3453 ret
= core_scsi3_tpg_depend_item(dest_se_tpg
);
3455 pr_err("core_scsi3_tpg_depend_item() failed"
3456 " for dest_se_tpg\n");
3457 atomic_dec(&dest_se_tpg
->tpg_pr_ref_count
);
3458 smp_mb__after_atomic_dec();
3459 core_scsi3_put_pr_reg(pr_reg
);
3460 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3464 spin_lock(&dev
->se_port_lock
);
3467 spin_unlock(&dev
->se_port_lock
);
3469 if (!dest_se_tpg
|| !dest_tf_ops
) {
3470 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3471 " fabric ops from Relative Target Port Identifier:"
3473 core_scsi3_put_pr_reg(pr_reg
);
3474 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3478 buf
= transport_kmap_data_sg(cmd
);
3479 proto_ident
= (buf
[24] & 0x0f);
3481 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3482 " 0x%02x\n", proto_ident
);
3484 if (proto_ident
!= dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
)) {
3485 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3486 " proto_ident: 0x%02x does not match ident: 0x%02x"
3487 " from fabric: %s\n", proto_ident
,
3488 dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
),
3489 dest_tf_ops
->get_fabric_name());
3490 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3494 if (dest_tf_ops
->tpg_parse_pr_out_transport_id
== NULL
) {
3495 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3496 " containg a valid tpg_parse_pr_out_transport_id"
3497 " function pointer\n");
3498 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3502 initiator_str
= dest_tf_ops
->tpg_parse_pr_out_transport_id(dest_se_tpg
,
3503 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3504 if (!initiator_str
) {
3505 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3506 " initiator_str from Transport ID\n");
3507 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3512 transport_kunmap_data_sg(cmd
);
3515 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3516 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3517 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3520 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3521 * action specifies a TransportID that is the same as the initiator port
3522 * of the I_T nexus for the command received, then the command shall
3523 * be terminated with CHECK CONDITION status, with the sense key set to
3524 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3525 * IN PARAMETER LIST.
3527 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3528 matching_iname
= (!strcmp(initiator_str
,
3529 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3530 if (!matching_iname
)
3531 goto after_iport_check
;
3533 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3534 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3535 " matches: %s on received I_T Nexus\n", initiator_str
,
3536 pr_reg_nacl
->initiatorname
);
3537 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3541 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3542 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3543 " matches: %s %s on received I_T Nexus\n",
3544 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3545 pr_reg
->pr_reg_isid
);
3546 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3552 * Locate the destination struct se_node_acl from the received Transport ID
3554 spin_lock_irq(&dest_se_tpg
->acl_node_lock
);
3555 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3557 if (dest_node_acl
) {
3558 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
3559 smp_mb__after_atomic_inc();
3561 spin_unlock_irq(&dest_se_tpg
->acl_node_lock
);
3563 if (!dest_node_acl
) {
3564 pr_err("Unable to locate %s dest_node_acl for"
3565 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3567 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3571 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
3573 pr_err("core_scsi3_nodeacl_depend_item() for"
3574 " dest_node_acl\n");
3575 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
3576 smp_mb__after_atomic_dec();
3577 dest_node_acl
= NULL
;
3578 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3583 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3584 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3585 dest_node_acl
->initiatorname
);
3588 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3591 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3592 if (!dest_se_deve
) {
3593 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3594 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3595 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3600 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
3602 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3603 atomic_dec(&dest_se_deve
->pr_ref_count
);
3604 smp_mb__after_atomic_dec();
3605 dest_se_deve
= NULL
;
3606 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3611 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3612 " ACL for dest_se_deve->mapped_lun: %u\n",
3613 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3614 dest_se_deve
->mapped_lun
);
3617 * A persistent reservation needs to already existing in order to
3618 * successfully complete the REGISTER_AND_MOVE service action..
3620 spin_lock(&dev
->dev_reservation_lock
);
3621 pr_res_holder
= dev
->dev_pr_res_holder
;
3622 if (!pr_res_holder
) {
3623 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3624 " currently held\n");
3625 spin_unlock(&dev
->dev_reservation_lock
);
3626 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3631 * The received on I_T Nexus must be the reservation holder.
3633 * From spc4r17 section 5.7.8 Table 50 --
3634 * Register behaviors for a REGISTER AND MOVE service action
3636 if (pr_res_holder
!= pr_reg
) {
3637 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3638 " Nexus is not reservation holder\n");
3639 spin_unlock(&dev
->dev_reservation_lock
);
3640 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3645 * From spc4r17 section 5.7.8: registering and moving reservation
3647 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3648 * action is received and the established persistent reservation is a
3649 * Write Exclusive - All Registrants type or Exclusive Access -
3650 * All Registrants type reservation, then the command shall be completed
3651 * with RESERVATION CONFLICT status.
3653 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3654 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3655 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3656 " reservation for type: %s\n",
3657 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3658 spin_unlock(&dev
->dev_reservation_lock
);
3659 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3663 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3665 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3667 type
= pr_res_holder
->pr_res_type
;
3668 scope
= pr_res_holder
->pr_res_type
;
3670 * c) Associate the reservation key specified in the SERVICE ACTION
3671 * RESERVATION KEY field with the I_T nexus specified as the
3672 * destination of the register and move, where:
3673 * A) The I_T nexus is specified by the TransportID and the
3674 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3675 * B) Regardless of the TransportID format used, the association for
3676 * the initiator port is based on either the initiator port name
3677 * (see 3.1.71) on SCSI transport protocols where port names are
3678 * required or the initiator port identifier (see 3.1.70) on SCSI
3679 * transport protocols where port names are not required;
3680 * d) Register the reservation key specified in the SERVICE ACTION
3681 * RESERVATION KEY field;
3682 * e) Retain the reservation key specified in the SERVICE ACTION
3683 * RESERVATION KEY field and associated information;
3685 * Also, It is not an error for a REGISTER AND MOVE service action to
3686 * register an I_T nexus that is already registered with the same
3687 * reservation key or a different reservation key.
3689 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3692 ret
= core_scsi3_alloc_registration(cmd
->se_dev
,
3693 dest_node_acl
, dest_se_deve
, iport_ptr
,
3694 sa_res_key
, 0, aptpl
, 2, 1);
3696 spin_unlock(&dev
->dev_reservation_lock
);
3697 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3701 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3706 * f) Release the persistent reservation for the persistent reservation
3707 * holder (i.e., the I_T nexus on which the
3709 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3710 dev
->dev_pr_res_holder
, 0);
3712 * g) Move the persistent reservation to the specified I_T nexus using
3713 * the same scope and type as the persistent reservation released in
3716 dev
->dev_pr_res_holder
= dest_pr_reg
;
3717 dest_pr_reg
->pr_res_holder
= 1;
3718 dest_pr_reg
->pr_res_type
= type
;
3719 pr_reg
->pr_res_scope
= scope
;
3720 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
3721 PR_REG_ISID_ID_LEN
);
3723 * Increment PRGeneration for existing registrations..
3726 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3727 spin_unlock(&dev
->dev_reservation_lock
);
3729 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3730 " created new reservation holder TYPE: %s on object RTPI:"
3731 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3732 core_scsi3_pr_dump_type(type
), rtpi
,
3733 dest_pr_reg
->pr_res_generation
);
3734 pr_debug("SPC-3 PR Successfully moved reservation from"
3735 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3736 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3737 (prf_isid
) ? &i_buf
[0] : "", dest_tf_ops
->get_fabric_name(),
3738 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3741 * It is now safe to release configfs group dependencies for destination
3742 * of Transport ID Initiator Device/Port Identifier
3744 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3745 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3746 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3748 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3749 * nexus on which PERSISTENT RESERVE OUT command was received.
3752 spin_lock(&pr_tmpl
->registration_lock
);
3753 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3754 spin_unlock(&pr_tmpl
->registration_lock
);
3756 core_scsi3_put_pr_reg(pr_reg
);
3759 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3760 * write out the updated metadata to struct file for this SCSI device.
3763 pr_tmpl
->pr_aptpl_active
= 0;
3764 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
3765 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3766 " REGISTER_AND_MOVE\n");
3768 pr_tmpl
->pr_aptpl_active
= 1;
3769 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3770 &dest_pr_reg
->pr_aptpl_buf
[0],
3771 pr_tmpl
->pr_aptpl_buf_len
);
3773 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3774 " REGISTER_AND_MOVE\n");
3777 transport_kunmap_data_sg(cmd
);
3779 core_scsi3_put_pr_reg(dest_pr_reg
);
3783 transport_kunmap_data_sg(cmd
);
3785 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3787 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3788 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3789 core_scsi3_put_pr_reg(pr_reg
);
3793 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3795 unsigned int __v1
, __v2
;
3797 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3798 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3800 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3804 * See spc4r17 section 6.14 Table 170
3806 int target_scsi3_emulate_pr_out(struct se_cmd
*cmd
)
3808 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3810 u64 res_key
, sa_res_key
;
3811 int sa
, scope
, type
, aptpl
;
3812 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3816 * Following spc2r20 5.5.1 Reservations overview:
3818 * If a logical unit has been reserved by any RESERVE command and is
3819 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3820 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3821 * initiator or service action and shall terminate with a RESERVATION
3824 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
) {
3825 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3826 " SPC-2 reservation is held, returning"
3827 " RESERVATION_CONFLICT\n");
3828 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3834 * FIXME: A NULL struct se_session pointer means an this is not coming from
3835 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3837 if (!cmd
->se_sess
) {
3838 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3843 if (cmd
->data_length
< 24) {
3844 pr_warn("SPC-PR: Received PR OUT parameter list"
3845 " length too small: %u\n", cmd
->data_length
);
3846 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3851 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3853 sa
= (cdb
[1] & 0x1f);
3854 scope
= (cdb
[2] & 0xf0);
3855 type
= (cdb
[2] & 0x0f);
3857 buf
= transport_kmap_data_sg(cmd
);
3859 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3861 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3862 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3864 * REGISTER_AND_MOVE uses a different SA parameter list containing
3865 * SCSI TransportIDs.
3867 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3868 spec_i_pt
= (buf
[20] & 0x08);
3869 all_tg_pt
= (buf
[20] & 0x04);
3870 aptpl
= (buf
[20] & 0x01);
3872 aptpl
= (buf
[17] & 0x01);
3873 unreg
= (buf
[17] & 0x02);
3875 transport_kunmap_data_sg(cmd
);
3879 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3881 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
)) {
3882 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3888 * From spc4r17 section 6.14:
3890 * If the SPEC_I_PT bit is set to zero, the service action is not
3891 * REGISTER AND MOVE, and the parameter list length is not 24, then
3892 * the command shall be terminated with CHECK CONDITION status, with
3893 * the sense key set to ILLEGAL REQUEST, and the additional sense
3894 * code set to PARAMETER LIST LENGTH ERROR.
3896 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3897 (cmd
->data_length
!= 24)) {
3898 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3899 " list length: %u\n", cmd
->data_length
);
3900 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3905 * (core_scsi3_emulate_pro_* function parameters
3906 * are defined by spc4r17 Table 174:
3907 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3911 ret
= core_scsi3_emulate_pro_register(cmd
,
3912 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 0);
3915 ret
= core_scsi3_emulate_pro_reserve(cmd
, type
, scope
, res_key
);
3918 ret
= core_scsi3_emulate_pro_release(cmd
, type
, scope
, res_key
);
3921 ret
= core_scsi3_emulate_pro_clear(cmd
, res_key
);
3924 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3925 res_key
, sa_res_key
, 0);
3927 case PRO_PREEMPT_AND_ABORT
:
3928 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3929 res_key
, sa_res_key
, 1);
3931 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3932 ret
= core_scsi3_emulate_pro_register(cmd
,
3933 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 1);
3935 case PRO_REGISTER_AND_MOVE
:
3936 ret
= core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3937 sa_res_key
, aptpl
, unreg
);
3940 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3941 " action: 0x%02x\n", cdb
[1] & 0x1f);
3942 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3949 target_complete_cmd(cmd
, GOOD
);
3954 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3956 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3958 static int core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3960 struct se_device
*se_dev
= cmd
->se_dev
;
3961 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
3962 struct t10_pr_registration
*pr_reg
;
3964 u32 add_len
= 0, off
= 8;
3966 if (cmd
->data_length
< 8) {
3967 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3968 " too small\n", cmd
->data_length
);
3969 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3973 buf
= transport_kmap_data_sg(cmd
);
3974 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3975 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3976 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3977 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
3979 spin_lock(&su_dev
->t10_pr
.registration_lock
);
3980 list_for_each_entry(pr_reg
, &su_dev
->t10_pr
.registration_list
,
3983 * Check for overflow of 8byte PRI READ_KEYS payload and
3984 * next reservation key list descriptor.
3986 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3989 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3990 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3991 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3992 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3993 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3994 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3995 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3996 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4000 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
4002 buf
[4] = ((add_len
>> 24) & 0xff);
4003 buf
[5] = ((add_len
>> 16) & 0xff);
4004 buf
[6] = ((add_len
>> 8) & 0xff);
4005 buf
[7] = (add_len
& 0xff);
4007 transport_kunmap_data_sg(cmd
);
4013 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4015 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4017 static int core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
4019 struct se_device
*se_dev
= cmd
->se_dev
;
4020 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
4021 struct t10_pr_registration
*pr_reg
;
4024 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
4026 if (cmd
->data_length
< 8) {
4027 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4028 " too small\n", cmd
->data_length
);
4029 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4033 buf
= transport_kmap_data_sg(cmd
);
4034 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
4035 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
4036 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
4037 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
4039 spin_lock(&se_dev
->dev_reservation_lock
);
4040 pr_reg
= se_dev
->dev_pr_res_holder
;
4043 * Set the hardcoded Additional Length
4045 buf
[4] = ((add_len
>> 24) & 0xff);
4046 buf
[5] = ((add_len
>> 16) & 0xff);
4047 buf
[6] = ((add_len
>> 8) & 0xff);
4048 buf
[7] = (add_len
& 0xff);
4050 if (cmd
->data_length
< 22)
4054 * Set the Reservation key.
4056 * From spc4r17, section 5.7.10:
4057 * A persistent reservation holder has its reservation key
4058 * returned in the parameter data from a PERSISTENT
4059 * RESERVE IN command with READ RESERVATION service action as
4061 * a) For a persistent reservation of the type Write Exclusive
4062 * - All Registrants or Exclusive Access  All Regitrants,
4063 * the reservation key shall be set to zero; or
4064 * b) For all other persistent reservation types, the
4065 * reservation key shall be set to the registered
4066 * reservation key for the I_T nexus that holds the
4067 * persistent reservation.
4069 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
4070 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
4073 pr_res_key
= pr_reg
->pr_res_key
;
4075 buf
[8] = ((pr_res_key
>> 56) & 0xff);
4076 buf
[9] = ((pr_res_key
>> 48) & 0xff);
4077 buf
[10] = ((pr_res_key
>> 40) & 0xff);
4078 buf
[11] = ((pr_res_key
>> 32) & 0xff);
4079 buf
[12] = ((pr_res_key
>> 24) & 0xff);
4080 buf
[13] = ((pr_res_key
>> 16) & 0xff);
4081 buf
[14] = ((pr_res_key
>> 8) & 0xff);
4082 buf
[15] = (pr_res_key
& 0xff);
4084 * Set the SCOPE and TYPE
4086 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
4087 (pr_reg
->pr_res_type
& 0x0f);
4091 spin_unlock(&se_dev
->dev_reservation_lock
);
4092 transport_kunmap_data_sg(cmd
);
4098 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4100 * See spc4r17 section 6.13.4 Table 165
4102 static int core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
4104 struct se_device
*dev
= cmd
->se_dev
;
4105 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
4107 u16 add_len
= 8; /* Hardcoded to 8. */
4109 if (cmd
->data_length
< 6) {
4110 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4111 " %u too small\n", cmd
->data_length
);
4112 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4116 buf
= transport_kmap_data_sg(cmd
);
4118 buf
[0] = ((add_len
<< 8) & 0xff);
4119 buf
[1] = (add_len
& 0xff);
4120 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4121 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4122 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4123 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4125 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4126 * set the TMV: Task Mask Valid bit.
4130 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4132 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4134 * PTPL_A: Persistence across Target Power Loss Active bit
4136 if (pr_tmpl
->pr_aptpl_active
)
4139 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4141 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4142 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4143 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4144 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4145 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4146 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4148 transport_kunmap_data_sg(cmd
);
4154 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4156 * See spc4r17 section 6.13.5 Table 168 and 169
4158 static int core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
4160 struct se_device
*se_dev
= cmd
->se_dev
;
4161 struct se_node_acl
*se_nacl
;
4162 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
4163 struct se_portal_group
*se_tpg
;
4164 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
4165 struct t10_reservation
*pr_tmpl
= &se_dev
->se_sub_dev
->t10_pr
;
4167 u32 add_desc_len
= 0, add_len
= 0, desc_len
, exp_desc_len
;
4168 u32 off
= 8; /* off into first Full Status descriptor */
4169 int format_code
= 0;
4171 if (cmd
->data_length
< 8) {
4172 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4173 " too small\n", cmd
->data_length
);
4174 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4178 buf
= transport_kmap_data_sg(cmd
);
4180 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
4181 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
4182 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
4183 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
4185 spin_lock(&pr_tmpl
->registration_lock
);
4186 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
4187 &pr_tmpl
->registration_list
, pr_reg_list
) {
4189 se_nacl
= pr_reg
->pr_reg_nacl
;
4190 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
4193 atomic_inc(&pr_reg
->pr_res_holders
);
4194 smp_mb__after_atomic_inc();
4195 spin_unlock(&pr_tmpl
->registration_lock
);
4197 * Determine expected length of $FABRIC_MOD specific
4198 * TransportID full status descriptor..
4200 exp_desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id_len(
4201 se_tpg
, se_nacl
, pr_reg
, &format_code
);
4203 if ((exp_desc_len
+ add_len
) > cmd
->data_length
) {
4204 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4205 " out of buffer: %d\n", cmd
->data_length
);
4206 spin_lock(&pr_tmpl
->registration_lock
);
4207 atomic_dec(&pr_reg
->pr_res_holders
);
4208 smp_mb__after_atomic_dec();
4212 * Set RESERVATION KEY
4214 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
4215 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
4216 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
4217 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4218 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4219 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4220 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4221 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4222 off
+= 4; /* Skip Over Reserved area */
4225 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4227 if (pr_reg
->pr_reg_all_tg_pt
)
4230 * The struct se_lun pointer will be present for the
4231 * reservation holder for PR_HOLDER bit.
4233 * Also, if this registration is the reservation
4234 * holder, fill in SCOPE and TYPE in the next byte.
4236 if (pr_reg
->pr_res_holder
) {
4238 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4239 (pr_reg
->pr_res_type
& 0x0f);
4243 off
+= 4; /* Skip over reserved area */
4245 * From spc4r17 6.3.15:
4247 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4248 * IDENTIFIER field contains the relative port identifier (see
4249 * 3.1.120) of the target port that is part of the I_T nexus
4250 * described by this full status descriptor. If the ALL_TG_PT
4251 * bit is set to one, the contents of the RELATIVE TARGET PORT
4252 * IDENTIFIER field are not defined by this standard.
4254 if (!pr_reg
->pr_reg_all_tg_pt
) {
4255 struct se_port
*port
= pr_reg
->pr_reg_tg_pt_lun
->lun_sep
;
4257 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
4258 buf
[off
++] = (port
->sep_rtpi
& 0xff);
4260 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4263 * Now, have the $FABRIC_MOD fill in the protocol identifier
4265 desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id(se_tpg
,
4266 se_nacl
, pr_reg
, &format_code
, &buf
[off
+4]);
4268 spin_lock(&pr_tmpl
->registration_lock
);
4269 atomic_dec(&pr_reg
->pr_res_holders
);
4270 smp_mb__after_atomic_dec();
4272 * Set the ADDITIONAL DESCRIPTOR LENGTH
4274 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4275 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4276 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4277 buf
[off
++] = (desc_len
& 0xff);
4279 * Size of full desctipor header minus TransportID
4280 * containing $FABRIC_MOD specific) initiator device/port
4283 * See spc4r17 Section 6.13.5 Table 169
4285 add_desc_len
= (24 + desc_len
);
4288 add_len
+= add_desc_len
;
4290 spin_unlock(&pr_tmpl
->registration_lock
);
4292 * Set ADDITIONAL_LENGTH
4294 buf
[4] = ((add_len
>> 24) & 0xff);
4295 buf
[5] = ((add_len
>> 16) & 0xff);
4296 buf
[6] = ((add_len
>> 8) & 0xff);
4297 buf
[7] = (add_len
& 0xff);
4299 transport_kunmap_data_sg(cmd
);
4304 int target_scsi3_emulate_pr_in(struct se_cmd
*cmd
)
4309 * Following spc2r20 5.5.1 Reservations overview:
4311 * If a logical unit has been reserved by any RESERVE command and is
4312 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4313 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4314 * initiator or service action and shall terminate with a RESERVATION
4317 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
) {
4318 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4319 " SPC-2 reservation is held, returning"
4320 " RESERVATION_CONFLICT\n");
4321 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
4325 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4327 ret
= core_scsi3_pri_read_keys(cmd
);
4329 case PRI_READ_RESERVATION
:
4330 ret
= core_scsi3_pri_read_reservation(cmd
);
4332 case PRI_REPORT_CAPABILITIES
:
4333 ret
= core_scsi3_pri_report_capabilities(cmd
);
4335 case PRI_READ_FULL_STATUS
:
4336 ret
= core_scsi3_pri_read_full_status(cmd
);
4339 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4340 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4341 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4347 target_complete_cmd(cmd
, GOOD
);
4351 static int core_pt_reservation_check(struct se_cmd
*cmd
, u32
*pr_res_type
)
4356 static int core_pt_seq_non_holder(
4364 int core_setup_reservations(struct se_device
*dev
, int force_pt
)
4366 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
4367 struct t10_reservation
*rest
= &su_dev
->t10_pr
;
4369 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4370 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4371 * cause a problem because libata and some SATA RAID HBAs appear
4372 * under Linux/SCSI, but to emulate reservations themselves.
4374 if (((dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
4375 !(dev
->se_sub_dev
->se_dev_attrib
.emulate_reservations
)) || force_pt
) {
4376 rest
->res_type
= SPC_PASSTHROUGH
;
4377 rest
->pr_ops
.t10_reservation_check
= &core_pt_reservation_check
;
4378 rest
->pr_ops
.t10_seq_non_holder
= &core_pt_seq_non_holder
;
4379 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4380 " emulation\n", dev
->transport
->name
);
4384 * If SPC-3 or above is reported by real or emulated struct se_device,
4385 * use emulated Persistent Reservations.
4387 if (dev
->transport
->get_device_rev(dev
) >= SCSI_3
) {
4388 rest
->res_type
= SPC3_PERSISTENT_RESERVATIONS
;
4389 rest
->pr_ops
.t10_reservation_check
= &core_scsi3_pr_reservation_check
;
4390 rest
->pr_ops
.t10_seq_non_holder
= &core_scsi3_pr_seq_non_holder
;
4391 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4392 " emulation\n", dev
->transport
->name
);
4394 rest
->res_type
= SPC2_RESERVATIONS
;
4395 rest
->pr_ops
.t10_reservation_check
= &core_scsi2_reservation_check
;
4396 rest
->pr_ops
.t10_seq_non_holder
=
4397 &core_scsi2_reservation_seq_non_holder
;
4398 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4399 dev
->transport
->name
);