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 buf
= transport_kmap_data_sg(cmd
);
1545 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1546 * first extract TransportID Parameter Data Length, and make sure
1547 * the value matches up to the SCSI expected data transfer length.
1549 tpdl
= (buf
[24] & 0xff) << 24;
1550 tpdl
|= (buf
[25] & 0xff) << 16;
1551 tpdl
|= (buf
[26] & 0xff) << 8;
1552 tpdl
|= buf
[27] & 0xff;
1554 if ((tpdl
+ 28) != cmd
->data_length
) {
1555 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1556 " does not equal CDB data_length: %u\n", tpdl
,
1558 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1563 * Start processing the received transport IDs using the
1564 * receiving I_T Nexus portal's fabric dependent methods to
1565 * obtain the SCSI Initiator Port/Device Identifiers.
1570 proto_ident
= (ptr
[0] & 0x0f);
1573 spin_lock(&dev
->se_port_lock
);
1574 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1575 tmp_tpg
= tmp_port
->sep_tpg
;
1578 tmp_tf_ops
= tmp_tpg
->se_tpg_tfo
;
1581 if (!tmp_tf_ops
->get_fabric_proto_ident
||
1582 !tmp_tf_ops
->tpg_parse_pr_out_transport_id
)
1585 * Look for the matching proto_ident provided by
1586 * the received TransportID
1588 tmp_proto_ident
= tmp_tf_ops
->get_fabric_proto_ident(tmp_tpg
);
1589 if (tmp_proto_ident
!= proto_ident
)
1591 dest_rtpi
= tmp_port
->sep_rtpi
;
1593 i_str
= tmp_tf_ops
->tpg_parse_pr_out_transport_id(
1594 tmp_tpg
, (const char *)ptr
, &tid_len
,
1599 atomic_inc(&tmp_tpg
->tpg_pr_ref_count
);
1600 smp_mb__after_atomic_inc();
1601 spin_unlock(&dev
->se_port_lock
);
1603 ret
= core_scsi3_tpg_depend_item(tmp_tpg
);
1605 pr_err(" core_scsi3_tpg_depend_item()"
1607 atomic_dec(&tmp_tpg
->tpg_pr_ref_count
);
1608 smp_mb__after_atomic_dec();
1609 cmd
->scsi_sense_reason
=
1610 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1615 * Locate the desination initiator ACL to be registered
1616 * from the decoded fabric module specific TransportID
1619 spin_lock_irq(&tmp_tpg
->acl_node_lock
);
1620 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1622 if (dest_node_acl
) {
1623 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
1624 smp_mb__after_atomic_inc();
1626 spin_unlock_irq(&tmp_tpg
->acl_node_lock
);
1628 if (!dest_node_acl
) {
1629 core_scsi3_tpg_undepend_item(tmp_tpg
);
1630 spin_lock(&dev
->se_port_lock
);
1634 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
1636 pr_err("configfs_depend_item() failed"
1637 " for dest_node_acl->acl_group\n");
1638 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
1639 smp_mb__after_atomic_dec();
1640 core_scsi3_tpg_undepend_item(tmp_tpg
);
1641 cmd
->scsi_sense_reason
=
1642 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1648 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1649 " %s Port RTPI: %hu\n",
1650 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1651 dest_node_acl
->initiatorname
, dest_rtpi
);
1653 spin_lock(&dev
->se_port_lock
);
1656 spin_unlock(&dev
->se_port_lock
);
1659 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1661 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1666 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1667 " tid_len: %d for %s + %s\n",
1668 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1669 tpdl
, tid_len
, i_str
, iport_ptr
);
1671 if (tid_len
> tpdl
) {
1672 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1673 " %u for Transport ID: %s\n", tid_len
, ptr
);
1674 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1675 core_scsi3_tpg_undepend_item(dest_tpg
);
1676 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1681 * Locate the desintation struct se_dev_entry pointer for matching
1682 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1685 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1687 if (!dest_se_deve
) {
1688 pr_err("Unable to locate %s dest_se_deve"
1689 " from destination RTPI: %hu\n",
1690 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1693 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1694 core_scsi3_tpg_undepend_item(dest_tpg
);
1695 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1700 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
1702 pr_err("core_scsi3_lunacl_depend_item()"
1704 atomic_dec(&dest_se_deve
->pr_ref_count
);
1705 smp_mb__after_atomic_dec();
1706 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1707 core_scsi3_tpg_undepend_item(dest_tpg
);
1708 cmd
->scsi_sense_reason
=
1709 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1714 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1715 " dest_se_deve mapped_lun: %u\n",
1716 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1717 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1720 * Skip any TransportIDs that already have a registration for
1723 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1726 core_scsi3_put_pr_reg(pr_reg_e
);
1727 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1728 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1729 core_scsi3_tpg_undepend_item(dest_tpg
);
1736 * Allocate a struct pr_transport_id_holder and setup
1737 * the dest_node_acl and dest_se_deve pointers for the
1740 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1743 pr_err("Unable to allocate tidh_new\n");
1744 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1745 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1746 core_scsi3_tpg_undepend_item(dest_tpg
);
1747 cmd
->scsi_sense_reason
=
1748 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1752 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1753 tidh_new
->dest_tpg
= dest_tpg
;
1754 tidh_new
->dest_node_acl
= dest_node_acl
;
1755 tidh_new
->dest_se_deve
= dest_se_deve
;
1758 * Allocate, but do NOT add the registration for the
1759 * TransportID referenced SCSI Initiator port. This
1760 * done because of the following from spc4r17 in section
1761 * 6.14.3 wrt SPEC_I_PT:
1763 * "If a registration fails for any initiator port (e.g., if th
1764 * logical unit does not have enough resources available to
1765 * hold the registration information), no registrations shall be
1766 * made, and the command shall be terminated with
1767 * CHECK CONDITION status."
1769 * That means we call __core_scsi3_alloc_registration() here,
1770 * and then call __core_scsi3_add_registration() in the
1771 * 2nd loop which will never fail.
1773 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1774 dest_node_acl
, dest_se_deve
, iport_ptr
,
1775 sa_res_key
, all_tg_pt
, aptpl
);
1777 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1778 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1779 core_scsi3_tpg_undepend_item(dest_tpg
);
1781 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1785 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1786 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1794 transport_kunmap_data_sg(cmd
);
1797 * Go ahead and create a registrations from tid_dest_list for the
1798 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1801 * The SA Reservation Key from the PROUT is set for the
1802 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1803 * means that the TransportID Initiator port will be
1804 * registered on all of the target ports in the SCSI target device
1805 * ALL_TG_PT=0 means the registration will only be for the
1806 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1809 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1810 dest_tpg
= tidh
->dest_tpg
;
1811 dest_node_acl
= tidh
->dest_node_acl
;
1812 dest_se_deve
= tidh
->dest_se_deve
;
1813 dest_pr_reg
= tidh
->dest_pr_reg
;
1814 dest_local_nexus
= tidh
->dest_local_nexus
;
1816 list_del(&tidh
->dest_list
);
1819 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1820 prf_isid
= core_pr_dump_initiator_port(dest_pr_reg
, &i_buf
[0],
1821 PR_REG_ISID_ID_LEN
);
1823 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1826 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1827 " registered Transport ID for Node: %s%s Mapped LUN:"
1828 " %u\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1829 dest_node_acl
->initiatorname
, (prf_isid
) ?
1830 &i_buf
[0] : "", dest_se_deve
->mapped_lun
);
1832 if (dest_local_nexus
)
1835 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1836 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1837 core_scsi3_tpg_undepend_item(dest_tpg
);
1842 transport_kunmap_data_sg(cmd
);
1844 * For the failure case, release everything from tid_dest_list
1845 * including *dest_pr_reg and the configfs dependances..
1847 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1848 dest_tpg
= tidh
->dest_tpg
;
1849 dest_node_acl
= tidh
->dest_node_acl
;
1850 dest_se_deve
= tidh
->dest_se_deve
;
1851 dest_pr_reg
= tidh
->dest_pr_reg
;
1852 dest_local_nexus
= tidh
->dest_local_nexus
;
1854 list_del(&tidh
->dest_list
);
1857 * Release any extra ALL_TG_PT=1 registrations for
1858 * the SPEC_I_PT=1 case.
1860 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1861 &dest_pr_reg
->pr_reg_atp_list
,
1862 pr_reg_atp_mem_list
) {
1863 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1864 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1865 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1868 kfree(dest_pr_reg
->pr_aptpl_buf
);
1869 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1871 if (dest_local_nexus
)
1874 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1875 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1876 core_scsi3_tpg_undepend_item(dest_tpg
);
1882 * Called with struct se_device->dev_reservation_lock held
1884 static int __core_scsi3_update_aptpl_buf(
1885 struct se_device
*dev
,
1887 u32 pr_aptpl_buf_len
,
1888 int clear_aptpl_metadata
)
1891 struct se_portal_group
*tpg
;
1892 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1893 struct t10_pr_registration
*pr_reg
;
1894 unsigned char tmp
[512], isid_buf
[32];
1898 memset(buf
, 0, pr_aptpl_buf_len
);
1900 * Called to clear metadata once APTPL has been deactivated.
1902 if (clear_aptpl_metadata
) {
1903 snprintf(buf
, pr_aptpl_buf_len
,
1904 "No Registrations or Reservations\n");
1908 * Walk the registration list..
1910 spin_lock(&su_dev
->t10_pr
.registration_lock
);
1911 list_for_each_entry(pr_reg
, &su_dev
->t10_pr
.registration_list
,
1916 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1917 lun
= pr_reg
->pr_reg_tg_pt_lun
;
1919 * Write out any ISID value to APTPL metadata that was included
1920 * in the original registration.
1922 if (pr_reg
->isid_present_at_reg
)
1923 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1924 pr_reg
->pr_reg_isid
);
1926 * Include special metadata if the pr_reg matches the
1927 * reservation holder.
1929 if (dev
->dev_pr_res_holder
== pr_reg
) {
1930 snprintf(tmp
, 512, "PR_REG_START: %d"
1931 "\ninitiator_fabric=%s\n"
1932 "initiator_node=%s\n%s"
1934 "res_holder=1\nres_type=%02x\n"
1935 "res_scope=%02x\nres_all_tg_pt=%d\n"
1936 "mapped_lun=%u\n", reg_count
,
1937 tpg
->se_tpg_tfo
->get_fabric_name(),
1938 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1939 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1940 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1941 pr_reg
->pr_res_mapped_lun
);
1943 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1944 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1945 "sa_res_key=%llu\nres_holder=0\n"
1946 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1947 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1948 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1949 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1950 pr_reg
->pr_res_mapped_lun
);
1953 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1954 pr_err("Unable to update renaming"
1955 " APTPL metadata\n");
1956 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1959 len
+= sprintf(buf
+len
, "%s", tmp
);
1962 * Include information about the associated SCSI target port.
1964 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1965 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1966 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1967 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1968 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1969 lun
->lun_sep
->sep_rtpi
, lun
->unpacked_lun
, reg_count
);
1971 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1972 pr_err("Unable to update renaming"
1973 " APTPL metadata\n");
1974 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1977 len
+= sprintf(buf
+len
, "%s", tmp
);
1980 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1983 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1988 static int core_scsi3_update_aptpl_buf(
1989 struct se_device
*dev
,
1991 u32 pr_aptpl_buf_len
,
1992 int clear_aptpl_metadata
)
1996 spin_lock(&dev
->dev_reservation_lock
);
1997 ret
= __core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
1998 clear_aptpl_metadata
);
1999 spin_unlock(&dev
->dev_reservation_lock
);
2005 * Called with struct se_device->aptpl_file_mutex held
2007 static int __core_scsi3_write_aptpl_to_file(
2008 struct se_device
*dev
,
2010 u32 pr_aptpl_buf_len
)
2012 struct t10_wwn
*wwn
= &dev
->se_sub_dev
->t10_wwn
;
2014 struct iovec iov
[1];
2015 mm_segment_t old_fs
;
2016 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
2020 memset(iov
, 0, sizeof(struct iovec
));
2021 memset(path
, 0, 512);
2023 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
2024 pr_err("WWN value for struct se_device does not fit"
2025 " into path buffer\n");
2029 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
2030 file
= filp_open(path
, flags
, 0600);
2031 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
2032 pr_err("filp_open(%s) for APTPL metadata"
2034 return IS_ERR(file
) ? PTR_ERR(file
) : -ENOENT
;
2037 iov
[0].iov_base
= &buf
[0];
2038 if (!pr_aptpl_buf_len
)
2039 iov
[0].iov_len
= (strlen(&buf
[0]) + 1); /* Add extra for NULL */
2041 iov
[0].iov_len
= pr_aptpl_buf_len
;
2045 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
2049 pr_debug("Error writing APTPL metadata file: %s\n", path
);
2050 filp_close(file
, NULL
);
2053 filp_close(file
, NULL
);
2058 static int core_scsi3_update_and_write_aptpl(
2059 struct se_device
*dev
,
2060 unsigned char *in_buf
,
2061 u32 in_pr_aptpl_buf_len
)
2063 unsigned char null_buf
[64], *buf
;
2064 u32 pr_aptpl_buf_len
;
2065 int ret
, clear_aptpl_metadata
= 0;
2067 * Can be called with a NULL pointer from PROUT service action CLEAR
2070 memset(null_buf
, 0, 64);
2073 * This will clear the APTPL metadata to:
2074 * "No Registrations or Reservations" status
2076 pr_aptpl_buf_len
= 64;
2077 clear_aptpl_metadata
= 1;
2080 pr_aptpl_buf_len
= in_pr_aptpl_buf_len
;
2083 ret
= core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
2084 clear_aptpl_metadata
);
2088 * __core_scsi3_write_aptpl_to_file() will call strlen()
2089 * on the passed buf to determine pr_aptpl_buf_len.
2091 ret
= __core_scsi3_write_aptpl_to_file(dev
, buf
, 0);
2098 static int core_scsi3_emulate_pro_register(
2107 struct se_session
*se_sess
= cmd
->se_sess
;
2108 struct se_device
*dev
= cmd
->se_dev
;
2109 struct se_dev_entry
*se_deve
;
2110 struct se_lun
*se_lun
= cmd
->se_lun
;
2111 struct se_portal_group
*se_tpg
;
2112 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
, *pr_reg_e
;
2113 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2114 /* Used for APTPL metadata w/ UNREGISTER */
2115 unsigned char *pr_aptpl_buf
= NULL
;
2116 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2117 int pr_holder
= 0, ret
= 0, type
;
2119 if (!se_sess
|| !se_lun
) {
2120 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2121 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2124 se_tpg
= se_sess
->se_tpg
;
2125 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2127 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2128 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2129 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2131 isid_ptr
= &isid_buf
[0];
2134 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2136 pr_reg_e
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2139 pr_warn("SPC-3 PR: Reservation Key non-zero"
2140 " for SA REGISTER, returning CONFLICT\n");
2141 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2145 * Do nothing but return GOOD status.
2152 * Perform the Service Action REGISTER on the Initiator
2153 * Port Endpoint that the PRO was received from on the
2154 * Logical Unit of the SCSI device server.
2156 ret
= core_scsi3_alloc_registration(cmd
->se_dev
,
2157 se_sess
->se_node_acl
, se_deve
, isid_ptr
,
2158 sa_res_key
, all_tg_pt
, aptpl
,
2161 pr_err("Unable to allocate"
2162 " struct t10_pr_registration\n");
2163 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
2168 * Register both the Initiator port that received
2169 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2170 * TransportID from Parameter list and loop through
2171 * fabric dependent parameter list while calling
2172 * logic from of core_scsi3_alloc_registration() for
2173 * each TransportID provided SCSI Initiator Port/Device
2175 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2176 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2181 * Nothing left to do for the APTPL=0 case.
2184 pr_tmpl
->pr_aptpl_active
= 0;
2185 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2186 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2191 * Locate the newly allocated local I_T Nexus *pr_reg, and
2192 * update the APTPL metadata information using its
2193 * preallocated *pr_reg->pr_aptpl_buf.
2195 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2196 se_sess
->se_node_acl
, se_sess
);
2198 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2199 &pr_reg
->pr_aptpl_buf
[0],
2200 pr_tmpl
->pr_aptpl_buf_len
);
2202 pr_tmpl
->pr_aptpl_active
= 1;
2203 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2206 core_scsi3_put_pr_reg(pr_reg
);
2210 * Locate the existing *pr_reg via struct se_node_acl pointers
2213 type
= pr_reg
->pr_res_type
;
2216 if (res_key
!= pr_reg
->pr_res_key
) {
2217 pr_err("SPC-3 PR REGISTER: Received"
2218 " res_key: 0x%016Lx does not match"
2219 " existing SA REGISTER res_key:"
2220 " 0x%016Lx\n", res_key
,
2221 pr_reg
->pr_res_key
);
2222 core_scsi3_put_pr_reg(pr_reg
);
2223 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2228 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2229 " set while sa_res_key=0\n");
2230 core_scsi3_put_pr_reg(pr_reg
);
2231 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
2235 * An existing ALL_TG_PT=1 registration being released
2236 * must also set ALL_TG_PT=1 in the incoming PROUT.
2238 if (pr_reg
->pr_reg_all_tg_pt
&& !(all_tg_pt
)) {
2239 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2240 " registration exists, but ALL_TG_PT=1 bit not"
2241 " present in received PROUT\n");
2242 core_scsi3_put_pr_reg(pr_reg
);
2243 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
2247 * Allocate APTPL metadata buffer used for UNREGISTER ops
2250 pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
,
2252 if (!pr_aptpl_buf
) {
2253 pr_err("Unable to allocate"
2255 core_scsi3_put_pr_reg(pr_reg
);
2256 cmd
->scsi_sense_reason
=
2257 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2262 * sa_res_key=0 Unregister Reservation Key for registered I_T
2263 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2267 pr_holder
= core_scsi3_check_implict_release(
2268 cmd
->se_dev
, pr_reg
);
2269 if (pr_holder
< 0) {
2270 kfree(pr_aptpl_buf
);
2271 core_scsi3_put_pr_reg(pr_reg
);
2272 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2276 spin_lock(&pr_tmpl
->registration_lock
);
2278 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2279 * and matching pr_res_key.
2281 if (pr_reg
->pr_reg_all_tg_pt
) {
2282 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2283 &pr_tmpl
->registration_list
,
2286 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2289 if (pr_reg_p
->pr_res_key
!= res_key
)
2292 if (pr_reg
== pr_reg_p
)
2295 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2296 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2299 __core_scsi3_free_registration(dev
,
2304 * Release the calling I_T Nexus registration now..
2306 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
,
2309 * From spc4r17, section 5.7.11.3 Unregistering
2311 * If the persistent reservation is a registrants only
2312 * type, the device server shall establish a unit
2313 * attention condition for the initiator port associated
2314 * with every registered I_T nexus except for the I_T
2315 * nexus on which the PERSISTENT RESERVE OUT command was
2316 * received, with the additional sense code set to
2317 * RESERVATIONS RELEASED.
2320 ((type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
2321 (type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
))) {
2322 list_for_each_entry(pr_reg_p
,
2323 &pr_tmpl
->registration_list
,
2326 core_scsi3_ua_allocate(
2327 pr_reg_p
->pr_reg_nacl
,
2328 pr_reg_p
->pr_res_mapped_lun
,
2330 ASCQ_2AH_RESERVATIONS_RELEASED
);
2333 spin_unlock(&pr_tmpl
->registration_lock
);
2336 pr_tmpl
->pr_aptpl_active
= 0;
2337 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2338 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2339 " for UNREGISTER\n");
2343 ret
= core_scsi3_update_and_write_aptpl(dev
,
2345 pr_tmpl
->pr_aptpl_buf_len
);
2347 pr_tmpl
->pr_aptpl_active
= 1;
2348 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2349 " for UNREGISTER\n");
2352 kfree(pr_aptpl_buf
);
2356 * Increment PRgeneration counter for struct se_device"
2357 * upon a successful REGISTER, see spc4r17 section 6.3.2
2358 * READ_KEYS service action.
2360 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(
2362 pr_reg
->pr_res_key
= sa_res_key
;
2363 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2364 " Key for %s to: 0x%016Lx PRgeneration:"
2365 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2366 (ignore_key
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2367 pr_reg
->pr_reg_nacl
->initiatorname
,
2368 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2371 pr_tmpl
->pr_aptpl_active
= 0;
2372 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2373 core_scsi3_put_pr_reg(pr_reg
);
2374 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2379 ret
= core_scsi3_update_and_write_aptpl(dev
,
2381 pr_tmpl
->pr_aptpl_buf_len
);
2383 pr_tmpl
->pr_aptpl_active
= 1;
2384 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2388 kfree(pr_aptpl_buf
);
2389 core_scsi3_put_pr_reg(pr_reg
);
2395 unsigned char *core_scsi3_pr_dump_type(int type
)
2398 case PR_TYPE_WRITE_EXCLUSIVE
:
2399 return "Write Exclusive Access";
2400 case PR_TYPE_EXCLUSIVE_ACCESS
:
2401 return "Exclusive Access";
2402 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2403 return "Write Exclusive Access, Registrants Only";
2404 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2405 return "Exclusive Access, Registrants Only";
2406 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2407 return "Write Exclusive Access, All Registrants";
2408 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2409 return "Exclusive Access, All Registrants";
2414 return "Unknown SPC-3 PR Type";
2417 static int core_scsi3_pro_reserve(
2419 struct se_device
*dev
,
2424 struct se_session
*se_sess
= cmd
->se_sess
;
2425 struct se_lun
*se_lun
= cmd
->se_lun
;
2426 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2427 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2428 char i_buf
[PR_REG_ISID_ID_LEN
];
2431 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2433 if (!se_sess
|| !se_lun
) {
2434 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2435 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2439 * Locate the existing *pr_reg via struct se_node_acl pointers
2441 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2444 pr_err("SPC-3 PR: Unable to locate"
2445 " PR_REGISTERED *pr_reg for RESERVE\n");
2446 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2450 * From spc4r17 Section 5.7.9: Reserving:
2452 * An application client creates a persistent reservation by issuing
2453 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2454 * a registered I_T nexus with the following parameters:
2455 * a) RESERVATION KEY set to the value of the reservation key that is
2456 * registered with the logical unit for the I_T nexus; and
2458 if (res_key
!= pr_reg
->pr_res_key
) {
2459 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2460 " does not match existing SA REGISTER res_key:"
2461 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2462 core_scsi3_put_pr_reg(pr_reg
);
2463 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2467 * From spc4r17 Section 5.7.9: Reserving:
2470 * b) TYPE field and SCOPE field set to the persistent reservation
2473 * Only one persistent reservation is allowed at a time per logical unit
2474 * and that persistent reservation has a scope of LU_SCOPE.
2476 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2477 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2478 core_scsi3_put_pr_reg(pr_reg
);
2479 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
2483 * See if we have an existing PR reservation holder pointer at
2484 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2487 spin_lock(&dev
->dev_reservation_lock
);
2488 pr_res_holder
= dev
->dev_pr_res_holder
;
2489 if (pr_res_holder
) {
2491 * From spc4r17 Section 5.7.9: Reserving:
2493 * If the device server receives a PERSISTENT RESERVE OUT
2494 * command from an I_T nexus other than a persistent reservation
2495 * holder (see 5.7.10) that attempts to create a persistent
2496 * reservation when a persistent reservation already exists for
2497 * the logical unit, then the command shall be completed with
2498 * RESERVATION CONFLICT status.
2500 if (pr_res_holder
!= pr_reg
) {
2501 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2502 pr_err("SPC-3 PR: Attempted RESERVE from"
2503 " [%s]: %s while reservation already held by"
2504 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2505 cmd
->se_tfo
->get_fabric_name(),
2506 se_sess
->se_node_acl
->initiatorname
,
2507 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2508 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2510 spin_unlock(&dev
->dev_reservation_lock
);
2511 core_scsi3_put_pr_reg(pr_reg
);
2512 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2516 * From spc4r17 Section 5.7.9: Reserving:
2518 * If a persistent reservation holder attempts to modify the
2519 * type or scope of an existing persistent reservation, the
2520 * command shall be completed with RESERVATION CONFLICT status.
2522 if ((pr_res_holder
->pr_res_type
!= type
) ||
2523 (pr_res_holder
->pr_res_scope
!= scope
)) {
2524 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2525 pr_err("SPC-3 PR: Attempted RESERVE from"
2526 " [%s]: %s trying to change TYPE and/or SCOPE,"
2527 " while reservation already held by [%s]: %s,"
2528 " returning RESERVATION_CONFLICT\n",
2529 cmd
->se_tfo
->get_fabric_name(),
2530 se_sess
->se_node_acl
->initiatorname
,
2531 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2532 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2534 spin_unlock(&dev
->dev_reservation_lock
);
2535 core_scsi3_put_pr_reg(pr_reg
);
2536 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2540 * From spc4r17 Section 5.7.9: Reserving:
2542 * If the device server receives a PERSISTENT RESERVE OUT
2543 * command with RESERVE service action where the TYPE field and
2544 * the SCOPE field contain the same values as the existing type
2545 * and scope from a persistent reservation holder, it shall not
2546 * make any change to the existing persistent reservation and
2547 * shall completethe command with GOOD status.
2549 spin_unlock(&dev
->dev_reservation_lock
);
2550 core_scsi3_put_pr_reg(pr_reg
);
2554 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2555 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2557 pr_reg
->pr_res_scope
= scope
;
2558 pr_reg
->pr_res_type
= type
;
2559 pr_reg
->pr_res_holder
= 1;
2560 dev
->dev_pr_res_holder
= pr_reg
;
2561 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2562 PR_REG_ISID_ID_LEN
);
2564 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2565 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2566 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2567 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2568 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2569 cmd
->se_tfo
->get_fabric_name(),
2570 se_sess
->se_node_acl
->initiatorname
,
2571 (prf_isid
) ? &i_buf
[0] : "");
2572 spin_unlock(&dev
->dev_reservation_lock
);
2574 if (pr_tmpl
->pr_aptpl_active
) {
2575 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2576 &pr_reg
->pr_aptpl_buf
[0],
2577 pr_tmpl
->pr_aptpl_buf_len
);
2579 pr_debug("SPC-3 PR: Updated APTPL metadata"
2583 core_scsi3_put_pr_reg(pr_reg
);
2587 static int core_scsi3_emulate_pro_reserve(
2593 struct se_device
*dev
= cmd
->se_dev
;
2597 case PR_TYPE_WRITE_EXCLUSIVE
:
2598 case PR_TYPE_EXCLUSIVE_ACCESS
:
2599 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2600 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2601 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2602 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2603 ret
= core_scsi3_pro_reserve(cmd
, dev
, type
, scope
, res_key
);
2606 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2608 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
2616 * Called with struct se_device->dev_reservation_lock held.
2618 static void __core_scsi3_complete_pro_release(
2619 struct se_device
*dev
,
2620 struct se_node_acl
*se_nacl
,
2621 struct t10_pr_registration
*pr_reg
,
2624 struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2625 char i_buf
[PR_REG_ISID_ID_LEN
];
2628 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2629 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2630 PR_REG_ISID_ID_LEN
);
2632 * Go ahead and release the current PR reservation holder.
2634 dev
->dev_pr_res_holder
= NULL
;
2636 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2637 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2638 tfo
->get_fabric_name(), (explict
) ? "explict" : "implict",
2639 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
2640 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2641 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2642 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2643 (prf_isid
) ? &i_buf
[0] : "");
2645 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2647 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2650 static int core_scsi3_emulate_pro_release(
2656 struct se_device
*dev
= cmd
->se_dev
;
2657 struct se_session
*se_sess
= cmd
->se_sess
;
2658 struct se_lun
*se_lun
= cmd
->se_lun
;
2659 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2660 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2661 int ret
, all_reg
= 0;
2663 if (!se_sess
|| !se_lun
) {
2664 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2665 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2669 * Locate the existing *pr_reg via struct se_node_acl pointers
2671 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2673 pr_err("SPC-3 PR: Unable to locate"
2674 " PR_REGISTERED *pr_reg for RELEASE\n");
2675 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2679 * From spc4r17 Section 5.7.11.2 Releasing:
2681 * If there is no persistent reservation or in response to a persistent
2682 * reservation release request from a registered I_T nexus that is not a
2683 * persistent reservation holder (see 5.7.10), the device server shall
2686 * a) Not release the persistent reservation, if any;
2687 * b) Not remove any registrations; and
2688 * c) Complete the command with GOOD status.
2690 spin_lock(&dev
->dev_reservation_lock
);
2691 pr_res_holder
= dev
->dev_pr_res_holder
;
2692 if (!pr_res_holder
) {
2694 * No persistent reservation, return GOOD status.
2696 spin_unlock(&dev
->dev_reservation_lock
);
2697 core_scsi3_put_pr_reg(pr_reg
);
2700 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2701 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
2704 if ((all_reg
== 0) && (pr_res_holder
!= pr_reg
)) {
2706 * Non 'All Registrants' PR Type cases..
2707 * Release request from a registered I_T nexus that is not a
2708 * persistent reservation holder. return GOOD status.
2710 spin_unlock(&dev
->dev_reservation_lock
);
2711 core_scsi3_put_pr_reg(pr_reg
);
2715 * From spc4r17 Section 5.7.11.2 Releasing:
2717 * Only the persistent reservation holder (see 5.7.10) is allowed to
2718 * release a persistent reservation.
2720 * An application client releases the persistent reservation by issuing
2721 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2722 * an I_T nexus that is a persistent reservation holder with the
2723 * following parameters:
2725 * a) RESERVATION KEY field set to the value of the reservation key
2726 * that is registered with the logical unit for the I_T nexus;
2728 if (res_key
!= pr_reg
->pr_res_key
) {
2729 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2730 " does not match existing SA REGISTER res_key:"
2731 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2732 spin_unlock(&dev
->dev_reservation_lock
);
2733 core_scsi3_put_pr_reg(pr_reg
);
2734 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2738 * From spc4r17 Section 5.7.11.2 Releasing and above:
2740 * b) TYPE field and SCOPE field set to match the persistent
2741 * reservation being released.
2743 if ((pr_res_holder
->pr_res_type
!= type
) ||
2744 (pr_res_holder
->pr_res_scope
!= scope
)) {
2745 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2746 pr_err("SPC-3 PR RELEASE: Attempted to release"
2747 " reservation from [%s]: %s with different TYPE "
2748 "and/or SCOPE while reservation already held by"
2749 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2750 cmd
->se_tfo
->get_fabric_name(),
2751 se_sess
->se_node_acl
->initiatorname
,
2752 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2753 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2755 spin_unlock(&dev
->dev_reservation_lock
);
2756 core_scsi3_put_pr_reg(pr_reg
);
2757 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2761 * In response to a persistent reservation release request from the
2762 * persistent reservation holder the device server shall perform a
2763 * release by doing the following as an uninterrupted series of actions:
2764 * a) Release the persistent reservation;
2765 * b) Not remove any registration(s);
2766 * c) If the released persistent reservation is a registrants only type
2767 * or all registrants type persistent reservation,
2768 * the device server shall establish a unit attention condition for
2769 * the initiator port associated with every regis-
2770 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2771 * RESERVE OUT command with RELEASE service action was received,
2772 * with the additional sense code set to RESERVATIONS RELEASED; and
2773 * d) If the persistent reservation is of any other type, the device
2774 * server shall not establish a unit attention condition.
2776 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2779 spin_unlock(&dev
->dev_reservation_lock
);
2781 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2782 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2783 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2784 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2786 * If no UNIT ATTENTION conditions will be established for
2787 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2788 * go ahead and check for APTPL=1 update+write below
2793 spin_lock(&pr_tmpl
->registration_lock
);
2794 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2797 * Do not establish a UNIT ATTENTION condition
2798 * for the calling I_T Nexus
2800 if (pr_reg_p
== pr_reg
)
2803 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2804 pr_reg_p
->pr_res_mapped_lun
,
2805 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2807 spin_unlock(&pr_tmpl
->registration_lock
);
2810 if (pr_tmpl
->pr_aptpl_active
) {
2811 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2812 &pr_reg
->pr_aptpl_buf
[0],
2813 pr_tmpl
->pr_aptpl_buf_len
);
2815 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2818 core_scsi3_put_pr_reg(pr_reg
);
2822 static int core_scsi3_emulate_pro_clear(
2826 struct se_device
*dev
= cmd
->se_dev
;
2827 struct se_node_acl
*pr_reg_nacl
;
2828 struct se_session
*se_sess
= cmd
->se_sess
;
2829 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2830 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2831 u32 pr_res_mapped_lun
= 0;
2832 int calling_it_nexus
= 0;
2834 * Locate the existing *pr_reg via struct se_node_acl pointers
2836 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2837 se_sess
->se_node_acl
, se_sess
);
2839 pr_err("SPC-3 PR: Unable to locate"
2840 " PR_REGISTERED *pr_reg for CLEAR\n");
2841 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2845 * From spc4r17 section 5.7.11.6, Clearing:
2847 * Any application client may release the persistent reservation and
2848 * remove all registrations from a device server by issuing a
2849 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2850 * registered I_T nexus with the following parameter:
2852 * a) RESERVATION KEY field set to the value of the reservation key
2853 * that is registered with the logical unit for the I_T nexus.
2855 if (res_key
!= pr_reg_n
->pr_res_key
) {
2856 pr_err("SPC-3 PR REGISTER: Received"
2857 " res_key: 0x%016Lx does not match"
2858 " existing SA REGISTER res_key:"
2859 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2860 core_scsi3_put_pr_reg(pr_reg_n
);
2861 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
2865 * a) Release the persistent reservation, if any;
2867 spin_lock(&dev
->dev_reservation_lock
);
2868 pr_res_holder
= dev
->dev_pr_res_holder
;
2869 if (pr_res_holder
) {
2870 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2871 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2874 spin_unlock(&dev
->dev_reservation_lock
);
2876 * b) Remove all registration(s) (see spc4r17 5.7.7);
2878 spin_lock(&pr_tmpl
->registration_lock
);
2879 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2880 &pr_tmpl
->registration_list
, pr_reg_list
) {
2882 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2883 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2884 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2885 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2888 * e) Establish a unit attention condition for the initiator
2889 * port associated with every registered I_T nexus other
2890 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2891 * command with CLEAR service action was received, with the
2892 * additional sense code set to RESERVATIONS PREEMPTED.
2894 if (!calling_it_nexus
)
2895 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2896 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2898 spin_unlock(&pr_tmpl
->registration_lock
);
2900 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2901 cmd
->se_tfo
->get_fabric_name());
2903 if (pr_tmpl
->pr_aptpl_active
) {
2904 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2905 pr_debug("SPC-3 PR: Updated APTPL metadata"
2909 core_scsi3_pr_generation(dev
);
2914 * Called with struct se_device->dev_reservation_lock held.
2916 static void __core_scsi3_complete_pro_preempt(
2917 struct se_device
*dev
,
2918 struct t10_pr_registration
*pr_reg
,
2919 struct list_head
*preempt_and_abort_list
,
2924 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2925 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2926 char i_buf
[PR_REG_ISID_ID_LEN
];
2929 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2930 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2931 PR_REG_ISID_ID_LEN
);
2933 * Do an implict RELEASE of the existing reservation.
2935 if (dev
->dev_pr_res_holder
)
2936 __core_scsi3_complete_pro_release(dev
, nacl
,
2937 dev
->dev_pr_res_holder
, 0);
2939 dev
->dev_pr_res_holder
= pr_reg
;
2940 pr_reg
->pr_res_holder
= 1;
2941 pr_reg
->pr_res_type
= type
;
2942 pr_reg
->pr_res_scope
= scope
;
2944 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2945 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2946 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2947 core_scsi3_pr_dump_type(type
),
2948 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2949 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2950 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2951 nacl
->initiatorname
, (prf_isid
) ? &i_buf
[0] : "");
2953 * For PREEMPT_AND_ABORT, add the preempting reservation's
2954 * struct t10_pr_registration to the list that will be compared
2955 * against received CDBs..
2957 if (preempt_and_abort_list
)
2958 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2959 preempt_and_abort_list
);
2962 static void core_scsi3_release_preempt_and_abort(
2963 struct list_head
*preempt_and_abort_list
,
2964 struct t10_pr_registration
*pr_reg_holder
)
2966 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2968 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2969 pr_reg_abort_list
) {
2971 list_del(&pr_reg
->pr_reg_abort_list
);
2972 if (pr_reg_holder
== pr_reg
)
2974 if (pr_reg
->pr_res_holder
) {
2975 pr_warn("pr_reg->pr_res_holder still set\n");
2979 pr_reg
->pr_reg_deve
= NULL
;
2980 pr_reg
->pr_reg_nacl
= NULL
;
2981 kfree(pr_reg
->pr_aptpl_buf
);
2982 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2986 static int core_scsi3_pro_preempt(
2994 struct se_device
*dev
= cmd
->se_dev
;
2995 struct se_node_acl
*pr_reg_nacl
;
2996 struct se_session
*se_sess
= cmd
->se_sess
;
2997 LIST_HEAD(preempt_and_abort_list
);
2998 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2999 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
3000 u32 pr_res_mapped_lun
= 0;
3001 int all_reg
= 0, calling_it_nexus
= 0, released_regs
= 0;
3002 int prh_type
= 0, prh_scope
= 0, ret
;
3005 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3009 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3012 pr_err("SPC-3 PR: Unable to locate"
3013 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3014 (abort
) ? "_AND_ABORT" : "");
3015 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3018 if (pr_reg_n
->pr_res_key
!= res_key
) {
3019 core_scsi3_put_pr_reg(pr_reg_n
);
3020 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3023 if (scope
!= PR_SCOPE_LU_SCOPE
) {
3024 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
3025 core_scsi3_put_pr_reg(pr_reg_n
);
3026 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3030 spin_lock(&dev
->dev_reservation_lock
);
3031 pr_res_holder
= dev
->dev_pr_res_holder
;
3032 if (pr_res_holder
&&
3033 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3034 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
3037 if (!all_reg
&& !sa_res_key
) {
3038 spin_unlock(&dev
->dev_reservation_lock
);
3039 core_scsi3_put_pr_reg(pr_reg_n
);
3040 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3044 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3046 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3047 * persistent reservation holder or there is no persistent reservation
3048 * holder (i.e., there is no persistent reservation), then the device
3049 * server shall perform a preempt by doing the following in an
3050 * uninterrupted series of actions. (See below..)
3052 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
3054 * No existing or SA Reservation Key matching reservations..
3056 * PROUT SA PREEMPT with All Registrant type reservations are
3057 * allowed to be processed without a matching SA Reservation Key
3059 spin_lock(&pr_tmpl
->registration_lock
);
3060 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3061 &pr_tmpl
->registration_list
, pr_reg_list
) {
3063 * Removing of registrations in non all registrants
3064 * type reservations without a matching SA reservation
3067 * a) Remove the registrations for all I_T nexuses
3068 * specified by the SERVICE ACTION RESERVATION KEY
3070 * b) Ignore the contents of the SCOPE and TYPE fields;
3071 * c) Process tasks as defined in 5.7.1; and
3072 * d) Establish a unit attention condition for the
3073 * initiator port associated with every I_T nexus
3074 * that lost its registration other than the I_T
3075 * nexus on which the PERSISTENT RESERVE OUT command
3076 * was received, with the additional sense code set
3077 * to REGISTRATIONS PREEMPTED.
3080 if (pr_reg
->pr_res_key
!= sa_res_key
)
3083 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3084 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3085 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3086 __core_scsi3_free_registration(dev
, pr_reg
,
3087 (abort
) ? &preempt_and_abort_list
:
3088 NULL
, calling_it_nexus
);
3092 * Case for any existing all registrants type
3093 * reservation, follow logic in spc4r17 section
3094 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3096 * For a ZERO SA Reservation key, release
3097 * all other registrations and do an implict
3098 * release of active persistent reservation.
3100 * For a non-ZERO SA Reservation key, only
3101 * release the matching reservation key from
3105 (pr_reg
->pr_res_key
!= sa_res_key
))
3108 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3109 if (calling_it_nexus
)
3112 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3113 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3114 __core_scsi3_free_registration(dev
, pr_reg
,
3115 (abort
) ? &preempt_and_abort_list
:
3119 if (!calling_it_nexus
)
3120 core_scsi3_ua_allocate(pr_reg_nacl
,
3121 pr_res_mapped_lun
, 0x2A,
3122 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3124 spin_unlock(&pr_tmpl
->registration_lock
);
3126 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3127 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3128 * RESERVATION KEY field to a value that does not match any
3129 * registered reservation key, then the device server shall
3130 * complete the command with RESERVATION CONFLICT status.
3132 if (!released_regs
) {
3133 spin_unlock(&dev
->dev_reservation_lock
);
3134 core_scsi3_put_pr_reg(pr_reg_n
);
3135 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3139 * For an existing all registrants type reservation
3140 * with a zero SA rservation key, preempt the existing
3141 * reservation with the new PR type and scope.
3143 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
3144 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3145 (abort
) ? &preempt_and_abort_list
: NULL
,
3146 type
, scope
, abort
);
3149 core_scsi3_release_preempt_and_abort(
3150 &preempt_and_abort_list
, pr_reg_n
);
3152 spin_unlock(&dev
->dev_reservation_lock
);
3154 if (pr_tmpl
->pr_aptpl_active
) {
3155 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3156 &pr_reg_n
->pr_aptpl_buf
[0],
3157 pr_tmpl
->pr_aptpl_buf_len
);
3159 pr_debug("SPC-3 PR: Updated APTPL"
3160 " metadata for PREEMPT%s\n", (abort
) ?
3164 core_scsi3_put_pr_reg(pr_reg_n
);
3165 core_scsi3_pr_generation(cmd
->se_dev
);
3169 * The PREEMPTing SA reservation key matches that of the
3170 * existing persistent reservation, first, we check if
3171 * we are preempting our own reservation.
3172 * From spc4r17, section 5.7.11.4.3 Preempting
3173 * persistent reservations and registration handling
3175 * If an all registrants persistent reservation is not
3176 * present, it is not an error for the persistent
3177 * reservation holder to preempt itself (i.e., a
3178 * PERSISTENT RESERVE OUT with a PREEMPT service action
3179 * or a PREEMPT AND ABORT service action with the
3180 * SERVICE ACTION RESERVATION KEY value equal to the
3181 * persistent reservation holder's reservation key that
3182 * is received from the persistent reservation holder).
3183 * In that case, the device server shall establish the
3184 * new persistent reservation and maintain the
3187 prh_type
= pr_res_holder
->pr_res_type
;
3188 prh_scope
= pr_res_holder
->pr_res_scope
;
3190 * If the SERVICE ACTION RESERVATION KEY field identifies a
3191 * persistent reservation holder (see 5.7.10), the device
3192 * server shall perform a preempt by doing the following as
3193 * an uninterrupted series of actions:
3195 * a) Release the persistent reservation for the holder
3196 * identified by the SERVICE ACTION RESERVATION KEY field;
3198 if (pr_reg_n
!= pr_res_holder
)
3199 __core_scsi3_complete_pro_release(dev
,
3200 pr_res_holder
->pr_reg_nacl
,
3201 dev
->dev_pr_res_holder
, 0);
3203 * b) Remove the registrations for all I_T nexuses identified
3204 * by the SERVICE ACTION RESERVATION KEY field, except the
3205 * I_T nexus that is being used for the PERSISTENT RESERVE
3206 * OUT command. If an all registrants persistent reservation
3207 * is present and the SERVICE ACTION RESERVATION KEY field
3208 * is set to zero, then all registrations shall be removed
3209 * except for that of the I_T nexus that is being used for
3210 * the PERSISTENT RESERVE OUT command;
3212 spin_lock(&pr_tmpl
->registration_lock
);
3213 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3214 &pr_tmpl
->registration_list
, pr_reg_list
) {
3216 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3217 if (calling_it_nexus
)
3220 if (pr_reg
->pr_res_key
!= sa_res_key
)
3223 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3224 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3225 __core_scsi3_free_registration(dev
, pr_reg
,
3226 (abort
) ? &preempt_and_abort_list
: NULL
,
3229 * e) Establish a unit attention condition for the initiator
3230 * port associated with every I_T nexus that lost its
3231 * persistent reservation and/or registration, with the
3232 * additional sense code set to REGISTRATIONS PREEMPTED;
3234 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3235 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3237 spin_unlock(&pr_tmpl
->registration_lock
);
3239 * c) Establish a persistent reservation for the preempting
3240 * I_T nexus using the contents of the SCOPE and TYPE fields;
3242 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3243 (abort
) ? &preempt_and_abort_list
: NULL
,
3244 type
, scope
, abort
);
3246 * d) Process tasks as defined in 5.7.1;
3248 * f) If the type or scope has changed, then for every I_T nexus
3249 * whose reservation key was not removed, except for the I_T
3250 * nexus on which the PERSISTENT RESERVE OUT command was
3251 * received, the device server shall establish a unit
3252 * attention condition for the initiator port associated with
3253 * that I_T nexus, with the additional sense code set to
3254 * RESERVATIONS RELEASED. If the type or scope have not
3255 * changed, then no unit attention condition(s) shall be
3256 * established for this reason.
3258 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3259 spin_lock(&pr_tmpl
->registration_lock
);
3260 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3261 &pr_tmpl
->registration_list
, pr_reg_list
) {
3263 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3264 if (calling_it_nexus
)
3267 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3268 pr_reg
->pr_res_mapped_lun
, 0x2A,
3269 ASCQ_2AH_RESERVATIONS_RELEASED
);
3271 spin_unlock(&pr_tmpl
->registration_lock
);
3273 spin_unlock(&dev
->dev_reservation_lock
);
3275 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3276 * All received CDBs for the matching existing reservation and
3277 * registrations undergo ABORT_TASK logic.
3279 * From there, core_scsi3_release_preempt_and_abort() will
3280 * release every registration in the list (which have already
3281 * been removed from the primary pr_reg list), except the
3282 * new persistent reservation holder, the calling Initiator Port.
3285 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3286 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3290 if (pr_tmpl
->pr_aptpl_active
) {
3291 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3292 &pr_reg_n
->pr_aptpl_buf
[0],
3293 pr_tmpl
->pr_aptpl_buf_len
);
3295 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3296 "%s\n", (abort
) ? "_AND_ABORT" : "");
3299 core_scsi3_put_pr_reg(pr_reg_n
);
3300 core_scsi3_pr_generation(cmd
->se_dev
);
3304 static int core_scsi3_emulate_pro_preempt(
3315 case PR_TYPE_WRITE_EXCLUSIVE
:
3316 case PR_TYPE_EXCLUSIVE_ACCESS
:
3317 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3318 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3319 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3320 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3321 ret
= core_scsi3_pro_preempt(cmd
, type
, scope
,
3322 res_key
, sa_res_key
, abort
);
3325 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3326 " Type: 0x%02x\n", (abort
) ? "_AND_ABORT" : "", type
);
3327 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3335 static int core_scsi3_emulate_pro_register_and_move(
3342 struct se_session
*se_sess
= cmd
->se_sess
;
3343 struct se_device
*dev
= cmd
->se_dev
;
3344 struct se_dev_entry
*dest_se_deve
= NULL
;
3345 struct se_lun
*se_lun
= cmd
->se_lun
;
3346 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3347 struct se_port
*se_port
;
3348 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3349 struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3350 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3351 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
3353 unsigned char *initiator_str
;
3354 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
3355 u32 tid_len
, tmp_tid_len
;
3356 int new_reg
= 0, type
, scope
, ret
, matching_iname
, prf_isid
;
3357 unsigned short rtpi
;
3358 unsigned char proto_ident
;
3360 if (!se_sess
|| !se_lun
) {
3361 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3362 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3365 memset(dest_iport
, 0, 64);
3366 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3367 se_tpg
= se_sess
->se_tpg
;
3368 tf_ops
= se_tpg
->se_tpg_tfo
;
3370 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3371 * Register behaviors for a REGISTER AND MOVE service action
3373 * Locate the existing *pr_reg via struct se_node_acl pointers
3375 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3378 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3379 " *pr_reg for REGISTER_AND_MOVE\n");
3380 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3384 * The provided reservation key much match the existing reservation key
3385 * provided during this initiator's I_T nexus registration.
3387 if (res_key
!= pr_reg
->pr_res_key
) {
3388 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3389 " res_key: 0x%016Lx does not match existing SA REGISTER"
3390 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3391 core_scsi3_put_pr_reg(pr_reg
);
3392 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3396 * The service active reservation key needs to be non zero
3399 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3401 core_scsi3_put_pr_reg(pr_reg
);
3402 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3407 * Determine the Relative Target Port Identifier where the reservation
3408 * will be moved to for the TransportID containing SCSI initiator WWN
3411 buf
= transport_kmap_data_sg(cmd
);
3412 rtpi
= (buf
[18] & 0xff) << 8;
3413 rtpi
|= buf
[19] & 0xff;
3414 tid_len
= (buf
[20] & 0xff) << 24;
3415 tid_len
|= (buf
[21] & 0xff) << 16;
3416 tid_len
|= (buf
[22] & 0xff) << 8;
3417 tid_len
|= buf
[23] & 0xff;
3418 transport_kunmap_data_sg(cmd
);
3421 if ((tid_len
+ 24) != cmd
->data_length
) {
3422 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3423 " does not equal CDB data_length: %u\n", tid_len
,
3425 core_scsi3_put_pr_reg(pr_reg
);
3426 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3430 spin_lock(&dev
->se_port_lock
);
3431 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3432 if (se_port
->sep_rtpi
!= rtpi
)
3434 dest_se_tpg
= se_port
->sep_tpg
;
3437 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3441 atomic_inc(&dest_se_tpg
->tpg_pr_ref_count
);
3442 smp_mb__after_atomic_inc();
3443 spin_unlock(&dev
->se_port_lock
);
3445 ret
= core_scsi3_tpg_depend_item(dest_se_tpg
);
3447 pr_err("core_scsi3_tpg_depend_item() failed"
3448 " for dest_se_tpg\n");
3449 atomic_dec(&dest_se_tpg
->tpg_pr_ref_count
);
3450 smp_mb__after_atomic_dec();
3451 core_scsi3_put_pr_reg(pr_reg
);
3452 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3456 spin_lock(&dev
->se_port_lock
);
3459 spin_unlock(&dev
->se_port_lock
);
3461 if (!dest_se_tpg
|| !dest_tf_ops
) {
3462 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3463 " fabric ops from Relative Target Port Identifier:"
3465 core_scsi3_put_pr_reg(pr_reg
);
3466 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3470 buf
= transport_kmap_data_sg(cmd
);
3471 proto_ident
= (buf
[24] & 0x0f);
3473 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3474 " 0x%02x\n", proto_ident
);
3476 if (proto_ident
!= dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
)) {
3477 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3478 " proto_ident: 0x%02x does not match ident: 0x%02x"
3479 " from fabric: %s\n", proto_ident
,
3480 dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
),
3481 dest_tf_ops
->get_fabric_name());
3482 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3486 if (dest_tf_ops
->tpg_parse_pr_out_transport_id
== NULL
) {
3487 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3488 " containg a valid tpg_parse_pr_out_transport_id"
3489 " function pointer\n");
3490 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3494 initiator_str
= dest_tf_ops
->tpg_parse_pr_out_transport_id(dest_se_tpg
,
3495 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3496 if (!initiator_str
) {
3497 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3498 " initiator_str from Transport ID\n");
3499 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3504 transport_kunmap_data_sg(cmd
);
3507 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3508 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3509 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3512 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3513 * action specifies a TransportID that is the same as the initiator port
3514 * of the I_T nexus for the command received, then the command shall
3515 * be terminated with CHECK CONDITION status, with the sense key set to
3516 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3517 * IN PARAMETER LIST.
3519 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3520 matching_iname
= (!strcmp(initiator_str
,
3521 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3522 if (!matching_iname
)
3523 goto after_iport_check
;
3525 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3526 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3527 " matches: %s on received I_T Nexus\n", initiator_str
,
3528 pr_reg_nacl
->initiatorname
);
3529 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3533 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3534 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3535 " matches: %s %s on received I_T Nexus\n",
3536 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3537 pr_reg
->pr_reg_isid
);
3538 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3544 * Locate the destination struct se_node_acl from the received Transport ID
3546 spin_lock_irq(&dest_se_tpg
->acl_node_lock
);
3547 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3549 if (dest_node_acl
) {
3550 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
3551 smp_mb__after_atomic_inc();
3553 spin_unlock_irq(&dest_se_tpg
->acl_node_lock
);
3555 if (!dest_node_acl
) {
3556 pr_err("Unable to locate %s dest_node_acl for"
3557 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3559 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3563 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
3565 pr_err("core_scsi3_nodeacl_depend_item() for"
3566 " dest_node_acl\n");
3567 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
3568 smp_mb__after_atomic_dec();
3569 dest_node_acl
= NULL
;
3570 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3575 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3576 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3577 dest_node_acl
->initiatorname
);
3580 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3583 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3584 if (!dest_se_deve
) {
3585 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3586 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3587 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3592 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
3594 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3595 atomic_dec(&dest_se_deve
->pr_ref_count
);
3596 smp_mb__after_atomic_dec();
3597 dest_se_deve
= NULL
;
3598 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3603 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3604 " ACL for dest_se_deve->mapped_lun: %u\n",
3605 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3606 dest_se_deve
->mapped_lun
);
3609 * A persistent reservation needs to already existing in order to
3610 * successfully complete the REGISTER_AND_MOVE service action..
3612 spin_lock(&dev
->dev_reservation_lock
);
3613 pr_res_holder
= dev
->dev_pr_res_holder
;
3614 if (!pr_res_holder
) {
3615 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3616 " currently held\n");
3617 spin_unlock(&dev
->dev_reservation_lock
);
3618 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3623 * The received on I_T Nexus must be the reservation holder.
3625 * From spc4r17 section 5.7.8 Table 50 --
3626 * Register behaviors for a REGISTER AND MOVE service action
3628 if (pr_res_holder
!= pr_reg
) {
3629 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3630 " Nexus is not reservation holder\n");
3631 spin_unlock(&dev
->dev_reservation_lock
);
3632 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3637 * From spc4r17 section 5.7.8: registering and moving reservation
3639 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3640 * action is received and the established persistent reservation is a
3641 * Write Exclusive - All Registrants type or Exclusive Access -
3642 * All Registrants type reservation, then the command shall be completed
3643 * with RESERVATION CONFLICT status.
3645 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3646 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3647 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3648 " reservation for type: %s\n",
3649 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3650 spin_unlock(&dev
->dev_reservation_lock
);
3651 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3655 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3657 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3659 type
= pr_res_holder
->pr_res_type
;
3660 scope
= pr_res_holder
->pr_res_type
;
3662 * c) Associate the reservation key specified in the SERVICE ACTION
3663 * RESERVATION KEY field with the I_T nexus specified as the
3664 * destination of the register and move, where:
3665 * A) The I_T nexus is specified by the TransportID and the
3666 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3667 * B) Regardless of the TransportID format used, the association for
3668 * the initiator port is based on either the initiator port name
3669 * (see 3.1.71) on SCSI transport protocols where port names are
3670 * required or the initiator port identifier (see 3.1.70) on SCSI
3671 * transport protocols where port names are not required;
3672 * d) Register the reservation key specified in the SERVICE ACTION
3673 * RESERVATION KEY field;
3674 * e) Retain the reservation key specified in the SERVICE ACTION
3675 * RESERVATION KEY field and associated information;
3677 * Also, It is not an error for a REGISTER AND MOVE service action to
3678 * register an I_T nexus that is already registered with the same
3679 * reservation key or a different reservation key.
3681 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3684 ret
= core_scsi3_alloc_registration(cmd
->se_dev
,
3685 dest_node_acl
, dest_se_deve
, iport_ptr
,
3686 sa_res_key
, 0, aptpl
, 2, 1);
3688 spin_unlock(&dev
->dev_reservation_lock
);
3689 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3693 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3698 * f) Release the persistent reservation for the persistent reservation
3699 * holder (i.e., the I_T nexus on which the
3701 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3702 dev
->dev_pr_res_holder
, 0);
3704 * g) Move the persistent reservation to the specified I_T nexus using
3705 * the same scope and type as the persistent reservation released in
3708 dev
->dev_pr_res_holder
= dest_pr_reg
;
3709 dest_pr_reg
->pr_res_holder
= 1;
3710 dest_pr_reg
->pr_res_type
= type
;
3711 pr_reg
->pr_res_scope
= scope
;
3712 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
3713 PR_REG_ISID_ID_LEN
);
3715 * Increment PRGeneration for existing registrations..
3718 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3719 spin_unlock(&dev
->dev_reservation_lock
);
3721 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3722 " created new reservation holder TYPE: %s on object RTPI:"
3723 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3724 core_scsi3_pr_dump_type(type
), rtpi
,
3725 dest_pr_reg
->pr_res_generation
);
3726 pr_debug("SPC-3 PR Successfully moved reservation from"
3727 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3728 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3729 (prf_isid
) ? &i_buf
[0] : "", dest_tf_ops
->get_fabric_name(),
3730 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3733 * It is now safe to release configfs group dependencies for destination
3734 * of Transport ID Initiator Device/Port Identifier
3736 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3737 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3738 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3740 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3741 * nexus on which PERSISTENT RESERVE OUT command was received.
3744 spin_lock(&pr_tmpl
->registration_lock
);
3745 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3746 spin_unlock(&pr_tmpl
->registration_lock
);
3748 core_scsi3_put_pr_reg(pr_reg
);
3751 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3752 * write out the updated metadata to struct file for this SCSI device.
3755 pr_tmpl
->pr_aptpl_active
= 0;
3756 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
3757 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3758 " REGISTER_AND_MOVE\n");
3760 pr_tmpl
->pr_aptpl_active
= 1;
3761 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3762 &dest_pr_reg
->pr_aptpl_buf
[0],
3763 pr_tmpl
->pr_aptpl_buf_len
);
3765 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3766 " REGISTER_AND_MOVE\n");
3769 transport_kunmap_data_sg(cmd
);
3771 core_scsi3_put_pr_reg(dest_pr_reg
);
3775 transport_kunmap_data_sg(cmd
);
3777 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3779 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3780 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3781 core_scsi3_put_pr_reg(pr_reg
);
3785 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3787 unsigned int __v1
, __v2
;
3789 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3790 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3792 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3796 * See spc4r17 section 6.14 Table 170
3798 int target_scsi3_emulate_pr_out(struct se_cmd
*cmd
)
3800 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3802 u64 res_key
, sa_res_key
;
3803 int sa
, scope
, type
, aptpl
;
3804 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3808 * Following spc2r20 5.5.1 Reservations overview:
3810 * If a logical unit has been reserved by any RESERVE command and is
3811 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3812 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3813 * initiator or service action and shall terminate with a RESERVATION
3816 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
) {
3817 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3818 " SPC-2 reservation is held, returning"
3819 " RESERVATION_CONFLICT\n");
3820 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
3826 * FIXME: A NULL struct se_session pointer means an this is not coming from
3827 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3829 if (!cmd
->se_sess
) {
3830 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3835 if (cmd
->data_length
< 24) {
3836 pr_warn("SPC-PR: Received PR OUT parameter list"
3837 " length too small: %u\n", cmd
->data_length
);
3838 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3843 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3845 sa
= (cdb
[1] & 0x1f);
3846 scope
= (cdb
[2] & 0xf0);
3847 type
= (cdb
[2] & 0x0f);
3849 buf
= transport_kmap_data_sg(cmd
);
3851 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3853 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3854 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3856 * REGISTER_AND_MOVE uses a different SA parameter list containing
3857 * SCSI TransportIDs.
3859 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3860 spec_i_pt
= (buf
[20] & 0x08);
3861 all_tg_pt
= (buf
[20] & 0x04);
3862 aptpl
= (buf
[20] & 0x01);
3864 aptpl
= (buf
[17] & 0x01);
3865 unreg
= (buf
[17] & 0x02);
3867 transport_kunmap_data_sg(cmd
);
3871 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3873 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
)) {
3874 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3880 * From spc4r17 section 6.14:
3882 * If the SPEC_I_PT bit is set to zero, the service action is not
3883 * REGISTER AND MOVE, and the parameter list length is not 24, then
3884 * the command shall be terminated with CHECK CONDITION status, with
3885 * the sense key set to ILLEGAL REQUEST, and the additional sense
3886 * code set to PARAMETER LIST LENGTH ERROR.
3888 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3889 (cmd
->data_length
!= 24)) {
3890 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3891 " list length: %u\n", cmd
->data_length
);
3892 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
3897 * (core_scsi3_emulate_pro_* function parameters
3898 * are defined by spc4r17 Table 174:
3899 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3903 ret
= core_scsi3_emulate_pro_register(cmd
,
3904 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 0);
3907 ret
= core_scsi3_emulate_pro_reserve(cmd
, type
, scope
, res_key
);
3910 ret
= core_scsi3_emulate_pro_release(cmd
, type
, scope
, res_key
);
3913 ret
= core_scsi3_emulate_pro_clear(cmd
, res_key
);
3916 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3917 res_key
, sa_res_key
, 0);
3919 case PRO_PREEMPT_AND_ABORT
:
3920 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3921 res_key
, sa_res_key
, 1);
3923 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3924 ret
= core_scsi3_emulate_pro_register(cmd
,
3925 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 1);
3927 case PRO_REGISTER_AND_MOVE
:
3928 ret
= core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3929 sa_res_key
, aptpl
, unreg
);
3932 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3933 " action: 0x%02x\n", cdb
[1] & 0x1f);
3934 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3941 target_complete_cmd(cmd
, GOOD
);
3946 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3948 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3950 static int core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3952 struct se_device
*se_dev
= cmd
->se_dev
;
3953 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
3954 struct t10_pr_registration
*pr_reg
;
3956 u32 add_len
= 0, off
= 8;
3958 if (cmd
->data_length
< 8) {
3959 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3960 " too small\n", cmd
->data_length
);
3961 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3965 buf
= transport_kmap_data_sg(cmd
);
3966 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3967 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3968 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3969 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
3971 spin_lock(&su_dev
->t10_pr
.registration_lock
);
3972 list_for_each_entry(pr_reg
, &su_dev
->t10_pr
.registration_list
,
3975 * Check for overflow of 8byte PRI READ_KEYS payload and
3976 * next reservation key list descriptor.
3978 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3981 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3982 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3983 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3984 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3985 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3986 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3987 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3988 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3992 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
3994 buf
[4] = ((add_len
>> 24) & 0xff);
3995 buf
[5] = ((add_len
>> 16) & 0xff);
3996 buf
[6] = ((add_len
>> 8) & 0xff);
3997 buf
[7] = (add_len
& 0xff);
3999 transport_kunmap_data_sg(cmd
);
4005 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4007 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4009 static int core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
4011 struct se_device
*se_dev
= cmd
->se_dev
;
4012 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
4013 struct t10_pr_registration
*pr_reg
;
4016 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
4018 if (cmd
->data_length
< 8) {
4019 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4020 " too small\n", cmd
->data_length
);
4021 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4025 buf
= transport_kmap_data_sg(cmd
);
4026 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
4027 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
4028 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
4029 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
4031 spin_lock(&se_dev
->dev_reservation_lock
);
4032 pr_reg
= se_dev
->dev_pr_res_holder
;
4035 * Set the hardcoded Additional Length
4037 buf
[4] = ((add_len
>> 24) & 0xff);
4038 buf
[5] = ((add_len
>> 16) & 0xff);
4039 buf
[6] = ((add_len
>> 8) & 0xff);
4040 buf
[7] = (add_len
& 0xff);
4042 if (cmd
->data_length
< 22)
4046 * Set the Reservation key.
4048 * From spc4r17, section 5.7.10:
4049 * A persistent reservation holder has its reservation key
4050 * returned in the parameter data from a PERSISTENT
4051 * RESERVE IN command with READ RESERVATION service action as
4053 * a) For a persistent reservation of the type Write Exclusive
4054 * - All Registrants or Exclusive Access  All Regitrants,
4055 * the reservation key shall be set to zero; or
4056 * b) For all other persistent reservation types, the
4057 * reservation key shall be set to the registered
4058 * reservation key for the I_T nexus that holds the
4059 * persistent reservation.
4061 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
4062 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
4065 pr_res_key
= pr_reg
->pr_res_key
;
4067 buf
[8] = ((pr_res_key
>> 56) & 0xff);
4068 buf
[9] = ((pr_res_key
>> 48) & 0xff);
4069 buf
[10] = ((pr_res_key
>> 40) & 0xff);
4070 buf
[11] = ((pr_res_key
>> 32) & 0xff);
4071 buf
[12] = ((pr_res_key
>> 24) & 0xff);
4072 buf
[13] = ((pr_res_key
>> 16) & 0xff);
4073 buf
[14] = ((pr_res_key
>> 8) & 0xff);
4074 buf
[15] = (pr_res_key
& 0xff);
4076 * Set the SCOPE and TYPE
4078 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
4079 (pr_reg
->pr_res_type
& 0x0f);
4083 spin_unlock(&se_dev
->dev_reservation_lock
);
4084 transport_kunmap_data_sg(cmd
);
4090 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4092 * See spc4r17 section 6.13.4 Table 165
4094 static int core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
4096 struct se_device
*dev
= cmd
->se_dev
;
4097 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
4099 u16 add_len
= 8; /* Hardcoded to 8. */
4101 if (cmd
->data_length
< 6) {
4102 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4103 " %u too small\n", cmd
->data_length
);
4104 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4108 buf
= transport_kmap_data_sg(cmd
);
4110 buf
[0] = ((add_len
<< 8) & 0xff);
4111 buf
[1] = (add_len
& 0xff);
4112 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4113 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4114 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4115 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4117 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4118 * set the TMV: Task Mask Valid bit.
4122 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4124 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4126 * PTPL_A: Persistence across Target Power Loss Active bit
4128 if (pr_tmpl
->pr_aptpl_active
)
4131 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4133 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4134 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4135 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4136 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4137 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4138 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4140 transport_kunmap_data_sg(cmd
);
4146 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4148 * See spc4r17 section 6.13.5 Table 168 and 169
4150 static int core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
4152 struct se_device
*se_dev
= cmd
->se_dev
;
4153 struct se_node_acl
*se_nacl
;
4154 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
4155 struct se_portal_group
*se_tpg
;
4156 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
4157 struct t10_reservation
*pr_tmpl
= &se_dev
->se_sub_dev
->t10_pr
;
4159 u32 add_desc_len
= 0, add_len
= 0, desc_len
, exp_desc_len
;
4160 u32 off
= 8; /* off into first Full Status descriptor */
4161 int format_code
= 0;
4163 if (cmd
->data_length
< 8) {
4164 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4165 " too small\n", cmd
->data_length
);
4166 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4170 buf
= transport_kmap_data_sg(cmd
);
4172 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
4173 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
4174 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
4175 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
4177 spin_lock(&pr_tmpl
->registration_lock
);
4178 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
4179 &pr_tmpl
->registration_list
, pr_reg_list
) {
4181 se_nacl
= pr_reg
->pr_reg_nacl
;
4182 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
4185 atomic_inc(&pr_reg
->pr_res_holders
);
4186 smp_mb__after_atomic_inc();
4187 spin_unlock(&pr_tmpl
->registration_lock
);
4189 * Determine expected length of $FABRIC_MOD specific
4190 * TransportID full status descriptor..
4192 exp_desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id_len(
4193 se_tpg
, se_nacl
, pr_reg
, &format_code
);
4195 if ((exp_desc_len
+ add_len
) > cmd
->data_length
) {
4196 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4197 " out of buffer: %d\n", cmd
->data_length
);
4198 spin_lock(&pr_tmpl
->registration_lock
);
4199 atomic_dec(&pr_reg
->pr_res_holders
);
4200 smp_mb__after_atomic_dec();
4204 * Set RESERVATION KEY
4206 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
4207 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
4208 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
4209 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4210 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4211 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4212 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4213 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4214 off
+= 4; /* Skip Over Reserved area */
4217 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4219 if (pr_reg
->pr_reg_all_tg_pt
)
4222 * The struct se_lun pointer will be present for the
4223 * reservation holder for PR_HOLDER bit.
4225 * Also, if this registration is the reservation
4226 * holder, fill in SCOPE and TYPE in the next byte.
4228 if (pr_reg
->pr_res_holder
) {
4230 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4231 (pr_reg
->pr_res_type
& 0x0f);
4235 off
+= 4; /* Skip over reserved area */
4237 * From spc4r17 6.3.15:
4239 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4240 * IDENTIFIER field contains the relative port identifier (see
4241 * 3.1.120) of the target port that is part of the I_T nexus
4242 * described by this full status descriptor. If the ALL_TG_PT
4243 * bit is set to one, the contents of the RELATIVE TARGET PORT
4244 * IDENTIFIER field are not defined by this standard.
4246 if (!pr_reg
->pr_reg_all_tg_pt
) {
4247 struct se_port
*port
= pr_reg
->pr_reg_tg_pt_lun
->lun_sep
;
4249 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
4250 buf
[off
++] = (port
->sep_rtpi
& 0xff);
4252 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4255 * Now, have the $FABRIC_MOD fill in the protocol identifier
4257 desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id(se_tpg
,
4258 se_nacl
, pr_reg
, &format_code
, &buf
[off
+4]);
4260 spin_lock(&pr_tmpl
->registration_lock
);
4261 atomic_dec(&pr_reg
->pr_res_holders
);
4262 smp_mb__after_atomic_dec();
4264 * Set the ADDITIONAL DESCRIPTOR LENGTH
4266 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4267 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4268 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4269 buf
[off
++] = (desc_len
& 0xff);
4271 * Size of full desctipor header minus TransportID
4272 * containing $FABRIC_MOD specific) initiator device/port
4275 * See spc4r17 Section 6.13.5 Table 169
4277 add_desc_len
= (24 + desc_len
);
4280 add_len
+= add_desc_len
;
4282 spin_unlock(&pr_tmpl
->registration_lock
);
4284 * Set ADDITIONAL_LENGTH
4286 buf
[4] = ((add_len
>> 24) & 0xff);
4287 buf
[5] = ((add_len
>> 16) & 0xff);
4288 buf
[6] = ((add_len
>> 8) & 0xff);
4289 buf
[7] = (add_len
& 0xff);
4291 transport_kunmap_data_sg(cmd
);
4296 int target_scsi3_emulate_pr_in(struct se_cmd
*cmd
)
4301 * Following spc2r20 5.5.1 Reservations overview:
4303 * If a logical unit has been reserved by any RESERVE command and is
4304 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4305 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4306 * initiator or service action and shall terminate with a RESERVATION
4309 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
) {
4310 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4311 " SPC-2 reservation is held, returning"
4312 " RESERVATION_CONFLICT\n");
4313 cmd
->scsi_sense_reason
= TCM_RESERVATION_CONFLICT
;
4317 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4319 ret
= core_scsi3_pri_read_keys(cmd
);
4321 case PRI_READ_RESERVATION
:
4322 ret
= core_scsi3_pri_read_reservation(cmd
);
4324 case PRI_REPORT_CAPABILITIES
:
4325 ret
= core_scsi3_pri_report_capabilities(cmd
);
4327 case PRI_READ_FULL_STATUS
:
4328 ret
= core_scsi3_pri_read_full_status(cmd
);
4331 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4332 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4333 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
4339 target_complete_cmd(cmd
, GOOD
);
4343 static int core_pt_reservation_check(struct se_cmd
*cmd
, u32
*pr_res_type
)
4348 static int core_pt_seq_non_holder(
4356 int core_setup_reservations(struct se_device
*dev
, int force_pt
)
4358 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
4359 struct t10_reservation
*rest
= &su_dev
->t10_pr
;
4361 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4362 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4363 * cause a problem because libata and some SATA RAID HBAs appear
4364 * under Linux/SCSI, but to emulate reservations themselves.
4366 if (((dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
4367 !(dev
->se_sub_dev
->se_dev_attrib
.emulate_reservations
)) || force_pt
) {
4368 rest
->res_type
= SPC_PASSTHROUGH
;
4369 rest
->pr_ops
.t10_reservation_check
= &core_pt_reservation_check
;
4370 rest
->pr_ops
.t10_seq_non_holder
= &core_pt_seq_non_holder
;
4371 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4372 " emulation\n", dev
->transport
->name
);
4376 * If SPC-3 or above is reported by real or emulated struct se_device,
4377 * use emulated Persistent Reservations.
4379 if (dev
->transport
->get_device_rev(dev
) >= SCSI_3
) {
4380 rest
->res_type
= SPC3_PERSISTENT_RESERVATIONS
;
4381 rest
->pr_ops
.t10_reservation_check
= &core_scsi3_pr_reservation_check
;
4382 rest
->pr_ops
.t10_seq_non_holder
= &core_scsi3_pr_seq_non_holder
;
4383 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4384 " emulation\n", dev
->transport
->name
);
4386 rest
->res_type
= SPC2_RESERVATIONS
;
4387 rest
->pr_ops
.t10_reservation_check
= &core_scsi2_reservation_check
;
4388 rest
->pr_ops
.t10_seq_non_holder
=
4389 &core_scsi2_reservation_seq_non_holder
;
4390 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4391 dev
->transport
->name
);