1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * (c) Copyright 2009-2012 RisingTide Systems LLC.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/file.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
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 void 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
);
69 REGISTER_AND_IGNORE_EXISTING_KEY
,
78 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
79 struct t10_pr_registration
*, int);
82 target_scsi2_reservation_check(struct se_cmd
*cmd
)
84 struct se_device
*dev
= cmd
->se_dev
;
85 struct se_session
*sess
= cmd
->se_sess
;
87 switch (cmd
->t_task_cdb
[0]) {
96 if (!dev
->dev_reserved_node_acl
|| !sess
)
99 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
100 return TCM_RESERVATION_CONFLICT
;
102 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
103 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
104 return TCM_RESERVATION_CONFLICT
;
110 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
111 struct se_node_acl
*, struct se_session
*);
112 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
114 static int target_check_scsi2_reservation_conflict(struct se_cmd
*cmd
)
116 struct se_session
*se_sess
= cmd
->se_sess
;
117 struct se_device
*dev
= cmd
->se_dev
;
118 struct t10_pr_registration
*pr_reg
;
119 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
122 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
126 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
129 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
130 * status, but no reservation shall be established and the
131 * persistent reservation shall not be changed, if the command
132 * is received from a) and b) below.
134 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
135 * status, but the persistent reservation shall not be released,
136 * if the command is received from a) and b)
138 * a) An I_T nexus that is a persistent reservation holder; or
139 * b) An I_T nexus that is registered if a registrants only or
140 * all registrants type persistent reservation is present.
142 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
143 * RELEASE(6) command, or RELEASE(10) command shall be processed
144 * as defined in SPC-2.
146 if (pr_reg
->pr_res_holder
) {
147 core_scsi3_put_pr_reg(pr_reg
);
150 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
151 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
152 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
153 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
154 core_scsi3_put_pr_reg(pr_reg
);
157 core_scsi3_put_pr_reg(pr_reg
);
161 * Following spc2r20 5.5.1 Reservations overview:
163 * If a logical unit has executed a PERSISTENT RESERVE OUT
164 * command with the REGISTER or the REGISTER AND IGNORE
165 * EXISTING KEY service action and is still registered by any
166 * initiator, all RESERVE commands and all RELEASE commands
167 * regardless of initiator shall conflict and shall terminate
168 * with a RESERVATION CONFLICT status.
170 spin_lock(&pr_tmpl
->registration_lock
);
171 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
172 spin_unlock(&pr_tmpl
->registration_lock
);
176 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
177 " while active SPC-3 registrations exist,"
178 " returning RESERVATION_CONFLICT\n");
186 target_scsi2_reservation_release(struct se_cmd
*cmd
)
188 struct se_device
*dev
= cmd
->se_dev
;
189 struct se_session
*sess
= cmd
->se_sess
;
190 struct se_portal_group
*tpg
;
193 if (!sess
|| !sess
->se_tpg
)
195 rc
= target_check_scsi2_reservation_conflict(cmd
);
199 return TCM_RESERVATION_CONFLICT
;
201 spin_lock(&dev
->dev_reservation_lock
);
202 if (!dev
->dev_reserved_node_acl
|| !sess
)
205 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
208 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
211 dev
->dev_reserved_node_acl
= NULL
;
212 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS
;
213 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
214 dev
->dev_res_bin_isid
= 0;
215 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS_WITH_ISID
;
218 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
219 " MAPPED LUN: %u for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
220 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
221 sess
->se_node_acl
->initiatorname
);
224 spin_unlock(&dev
->dev_reservation_lock
);
226 target_complete_cmd(cmd
, GOOD
);
231 target_scsi2_reservation_reserve(struct se_cmd
*cmd
)
233 struct se_device
*dev
= cmd
->se_dev
;
234 struct se_session
*sess
= cmd
->se_sess
;
235 struct se_portal_group
*tpg
;
236 sense_reason_t ret
= 0;
239 if ((cmd
->t_task_cdb
[1] & 0x01) &&
240 (cmd
->t_task_cdb
[1] & 0x02)) {
241 pr_err("LongIO and Obselete Bits set, returning"
242 " ILLEGAL_REQUEST\n");
243 return TCM_UNSUPPORTED_SCSI_OPCODE
;
246 * This is currently the case for target_core_mod passthrough struct se_cmd
249 if (!sess
|| !sess
->se_tpg
)
251 rc
= target_check_scsi2_reservation_conflict(cmd
);
256 return TCM_RESERVATION_CONFLICT
;
259 spin_lock(&dev
->dev_reservation_lock
);
260 if (dev
->dev_reserved_node_acl
&&
261 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
262 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
263 tpg
->se_tpg_tfo
->get_fabric_name());
264 pr_err("Original reserver LUN: %u %s\n",
265 cmd
->se_lun
->unpacked_lun
,
266 dev
->dev_reserved_node_acl
->initiatorname
);
267 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
268 " from %s \n", cmd
->se_lun
->unpacked_lun
,
269 cmd
->se_deve
->mapped_lun
,
270 sess
->se_node_acl
->initiatorname
);
271 ret
= TCM_RESERVATION_CONFLICT
;
275 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
276 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS
;
277 if (sess
->sess_bin_isid
!= 0) {
278 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
279 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS_WITH_ISID
;
281 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
282 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
283 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
284 sess
->se_node_acl
->initiatorname
);
287 spin_unlock(&dev
->dev_reservation_lock
);
290 target_complete_cmd(cmd
, GOOD
);
296 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
298 * This function is called by those initiator ports who are *NOT*
299 * the active PR reservation holder when a reservation is present.
301 static int core_scsi3_pr_seq_non_holder(
305 unsigned char *cdb
= cmd
->t_task_cdb
;
306 struct se_dev_entry
*se_deve
;
307 struct se_session
*se_sess
= cmd
->se_sess
;
308 int other_cdb
= 0, ignore_reg
;
309 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
310 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
311 int we
= 0; /* Write Exclusive */
312 int legacy
= 0; /* Act like a legacy device and return
313 * RESERVATION CONFLICT on some CDBs */
315 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
317 * Determine if the registration should be ignored due to
318 * non-matching ISIDs in target_scsi3_pr_reservation_check().
320 ignore_reg
= (pr_reg_type
& 0x80000000);
322 pr_reg_type
&= ~0x80000000;
324 switch (pr_reg_type
) {
325 case PR_TYPE_WRITE_EXCLUSIVE
:
327 case PR_TYPE_EXCLUSIVE_ACCESS
:
329 * Some commands are only allowed for the persistent reservation
332 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
333 registered_nexus
= 1;
335 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
337 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
339 * Some commands are only allowed for registered I_T Nexuses.
342 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
343 registered_nexus
= 1;
345 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
347 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
349 * Each registered I_T Nexus is a reservation holder.
352 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
353 registered_nexus
= 1;
359 * Referenced from spc4r17 table 45 for *NON* PR holder access
362 case SECURITY_PROTOCOL_IN
:
363 if (registered_nexus
)
371 case RECEIVE_DIAGNOSTIC
:
376 if (registered_nexus
) {
380 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
382 case PERSISTENT_RESERVE_OUT
:
384 * This follows PERSISTENT_RESERVE_OUT service actions that
385 * are allowed in the presence of various reservations.
386 * See spc4r17, table 46
388 switch (cdb
[1] & 0x1f) {
391 case PRO_PREEMPT_AND_ABORT
:
392 ret
= (registered_nexus
) ? 0 : 1;
395 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
398 case PRO_REGISTER_AND_MOVE
:
403 ret
= (registered_nexus
) ? 0 : 1;
406 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
407 " action: 0x%02x\n", cdb
[1] & 0x1f);
413 /* Handled by CRH=1 in target_scsi2_reservation_release() */
418 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
421 case TEST_UNIT_READY
:
422 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
425 switch (cdb
[1] & 0x1f) {
426 case MI_MANAGEMENT_PROTOCOL_IN
:
427 if (registered_nexus
) {
431 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
433 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
434 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
439 if (registered_nexus
) {
443 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
445 case MI_REPORT_ALIASES
:
446 case MI_REPORT_IDENTIFYING_INFORMATION
:
447 case MI_REPORT_PRIORITY
:
448 case MI_REPORT_TARGET_PGS
:
449 case MI_REPORT_TIMESTAMP
:
450 ret
= 0; /* Allowed */
453 pr_err("Unknown MI Service Action: 0x%02x\n",
458 case ACCESS_CONTROL_IN
:
459 case ACCESS_CONTROL_OUT
:
462 case READ_MEDIA_SERIAL_NUMBER
:
465 case PERSISTENT_RESERVE_IN
:
466 ret
= 0; /*/ Allowed CDBs */
473 * Case where the CDB is explicitly allowed in the above switch
476 if (!ret
&& !other_cdb
) {
477 pr_debug("Allowing explict CDB: 0x%02x for %s"
478 " reservation holder\n", cdb
[0],
479 core_scsi3_pr_dump_type(pr_reg_type
));
484 * Check if write exclusive initiator ports *NOT* holding the
485 * WRITE_EXCLUSIVE_* reservation.
487 if (we
&& !registered_nexus
) {
488 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
490 * Conflict for write exclusive
492 pr_debug("%s Conflict for unregistered nexus"
493 " %s CDB: 0x%02x to %s reservation\n",
494 transport_dump_cmd_direction(cmd
),
495 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
496 core_scsi3_pr_dump_type(pr_reg_type
));
500 * Allow non WRITE CDBs for all Write Exclusive
501 * PR TYPEs to pass for registered and
502 * non-registered_nexuxes NOT holding the reservation.
504 * We only make noise for the unregisterd nexuses,
505 * as we expect registered non-reservation holding
506 * nexuses to issue CDBs.
509 if (!registered_nexus
) {
510 pr_debug("Allowing implict CDB: 0x%02x"
511 " for %s reservation on unregistered"
513 core_scsi3_pr_dump_type(pr_reg_type
));
518 } else if ((reg_only
) || (all_reg
)) {
519 if (registered_nexus
) {
521 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
522 * allow commands from registered nexuses.
525 pr_debug("Allowing implict CDB: 0x%02x for %s"
526 " reservation\n", cdb
[0],
527 core_scsi3_pr_dump_type(pr_reg_type
));
532 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
533 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
534 (registered_nexus
) ? "" : "un",
535 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
536 core_scsi3_pr_dump_type(pr_reg_type
));
538 return 1; /* Conflict by default */
541 static sense_reason_t
542 target_scsi3_pr_reservation_check(struct se_cmd
*cmd
)
544 struct se_device
*dev
= cmd
->se_dev
;
545 struct se_session
*sess
= cmd
->se_sess
;
548 if (!dev
->dev_pr_res_holder
)
551 pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
552 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
553 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
)
554 goto check_nonholder
;
556 if (dev
->dev_pr_res_holder
->isid_present_at_reg
) {
557 if (dev
->dev_pr_res_holder
->pr_reg_bin_isid
!=
558 sess
->sess_bin_isid
) {
559 pr_reg_type
|= 0x80000000;
560 goto check_nonholder
;
567 if (core_scsi3_pr_seq_non_holder(cmd
, pr_reg_type
))
568 return TCM_RESERVATION_CONFLICT
;
572 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
577 * PRGeneration field shall contain the value of a 32-bit wrapping
578 * counter mainted by the device server.
580 * Note that this is done regardless of Active Persist across
581 * Target PowerLoss (APTPL)
583 * See spc4r17 section 6.3.12 READ_KEYS service action
585 spin_lock(&dev
->dev_reservation_lock
);
586 prg
= dev
->t10_pr
.pr_generation
++;
587 spin_unlock(&dev
->dev_reservation_lock
);
592 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
593 struct se_device
*dev
,
594 struct se_node_acl
*nacl
,
595 struct se_dev_entry
*deve
,
601 struct t10_pr_registration
*pr_reg
;
603 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
605 pr_err("Unable to allocate struct t10_pr_registration\n");
609 pr_reg
->pr_aptpl_buf
= kzalloc(PR_APTPL_BUF_LEN
, GFP_ATOMIC
);
610 if (!pr_reg
->pr_aptpl_buf
) {
611 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
612 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
616 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
617 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
618 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
619 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
620 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
621 atomic_set(&pr_reg
->pr_res_holders
, 0);
622 pr_reg
->pr_reg_nacl
= nacl
;
623 pr_reg
->pr_reg_deve
= deve
;
624 pr_reg
->pr_res_mapped_lun
= deve
->mapped_lun
;
625 pr_reg
->pr_aptpl_target_lun
= deve
->se_lun
->unpacked_lun
;
626 pr_reg
->pr_res_key
= sa_res_key
;
627 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
628 pr_reg
->pr_reg_aptpl
= aptpl
;
629 pr_reg
->pr_reg_tg_pt_lun
= deve
->se_lun
;
631 * If an ISID value for this SCSI Initiator Port exists,
632 * save it to the registration now.
635 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
636 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
637 pr_reg
->isid_present_at_reg
= 1;
643 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
644 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
647 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
650 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
651 struct se_device
*dev
,
652 struct se_node_acl
*nacl
,
653 struct se_dev_entry
*deve
,
659 struct se_dev_entry
*deve_tmp
;
660 struct se_node_acl
*nacl_tmp
;
661 struct se_port
*port
, *port_tmp
;
662 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
663 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
666 * Create a registration for the I_T Nexus upon which the
667 * PROUT REGISTER was received.
669 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, deve
, isid
,
670 sa_res_key
, all_tg_pt
, aptpl
);
674 * Return pointer to pr_reg for ALL_TG_PT=0
679 * Create list of matching SCSI Initiator Port registrations
682 spin_lock(&dev
->se_port_lock
);
683 list_for_each_entry_safe(port
, port_tmp
, &dev
->dev_sep_list
, sep_list
) {
684 atomic_inc(&port
->sep_tg_pt_ref_cnt
);
685 smp_mb__after_atomic_inc();
686 spin_unlock(&dev
->se_port_lock
);
688 spin_lock_bh(&port
->sep_alua_lock
);
689 list_for_each_entry(deve_tmp
, &port
->sep_alua_list
,
692 * This pointer will be NULL for demo mode MappedLUNs
693 * that have not been make explict via a ConfigFS
694 * MappedLUN group for the SCSI Initiator Node ACL.
696 if (!deve_tmp
->se_lun_acl
)
699 nacl_tmp
= deve_tmp
->se_lun_acl
->se_lun_nacl
;
701 * Skip the matching struct se_node_acl that is allocated
704 if (nacl
== nacl_tmp
)
707 * Only perform PR registrations for target ports on
708 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
711 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
714 * Look for a matching Initiator Node ACL in ASCII format
716 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
719 atomic_inc(&deve_tmp
->pr_ref_count
);
720 smp_mb__after_atomic_inc();
721 spin_unlock_bh(&port
->sep_alua_lock
);
723 * Grab a configfs group dependency that is released
724 * for the exception path at label out: below, or upon
725 * completion of adding ALL_TG_PT=1 registrations in
726 * __core_scsi3_add_registration()
728 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
730 pr_err("core_scsi3_lunacl_depend"
732 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
733 smp_mb__after_atomic_dec();
734 atomic_dec(&deve_tmp
->pr_ref_count
);
735 smp_mb__after_atomic_dec();
739 * Located a matching SCSI Initiator Port on a different
740 * port, allocate the pr_reg_atp and attach it to the
741 * pr_reg->pr_reg_atp_list that will be processed once
742 * the original *pr_reg is processed in
743 * __core_scsi3_add_registration()
745 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
746 nacl_tmp
, deve_tmp
, NULL
,
747 sa_res_key
, all_tg_pt
, aptpl
);
749 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
750 smp_mb__after_atomic_dec();
751 atomic_dec(&deve_tmp
->pr_ref_count
);
752 smp_mb__after_atomic_dec();
753 core_scsi3_lunacl_undepend_item(deve_tmp
);
757 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
758 &pr_reg
->pr_reg_atp_list
);
759 spin_lock_bh(&port
->sep_alua_lock
);
761 spin_unlock_bh(&port
->sep_alua_lock
);
763 spin_lock(&dev
->se_port_lock
);
764 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
765 smp_mb__after_atomic_dec();
767 spin_unlock(&dev
->se_port_lock
);
771 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
772 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
773 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
774 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
775 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
777 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
781 int core_scsi3_alloc_aptpl_registration(
782 struct t10_reservation
*pr_tmpl
,
784 unsigned char *i_port
,
787 unsigned char *t_port
,
794 struct t10_pr_registration
*pr_reg
;
796 if (!i_port
|| !t_port
|| !sa_res_key
) {
797 pr_err("Illegal parameters for APTPL registration\n");
801 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
803 pr_err("Unable to allocate struct t10_pr_registration\n");
806 pr_reg
->pr_aptpl_buf
= kzalloc(PR_APTPL_BUF_LEN
, GFP_KERNEL
);
808 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
809 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
810 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
811 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
812 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
813 atomic_set(&pr_reg
->pr_res_holders
, 0);
814 pr_reg
->pr_reg_nacl
= NULL
;
815 pr_reg
->pr_reg_deve
= NULL
;
816 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
817 pr_reg
->pr_aptpl_target_lun
= target_lun
;
818 pr_reg
->pr_res_key
= sa_res_key
;
819 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
820 pr_reg
->pr_reg_aptpl
= 1;
821 pr_reg
->pr_reg_tg_pt_lun
= NULL
;
822 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
823 pr_reg
->pr_res_type
= type
;
825 * If an ISID value had been saved in APTPL metadata for this
826 * SCSI Initiator Port, restore it now.
829 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
830 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
831 pr_reg
->isid_present_at_reg
= 1;
834 * Copy the i_port and t_port information from caller.
836 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
837 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
838 pr_reg
->pr_reg_tpgt
= tpgt
;
840 * Set pr_res_holder from caller, the pr_reg who is the reservation
841 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
842 * the Initiator Node LUN ACL from the fabric module is created for
845 pr_reg
->pr_res_holder
= res_holder
;
847 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
848 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
849 " metadata\n", (res_holder
) ? "+reservation" : "");
853 static void core_scsi3_aptpl_reserve(
854 struct se_device
*dev
,
855 struct se_portal_group
*tpg
,
856 struct se_node_acl
*node_acl
,
857 struct t10_pr_registration
*pr_reg
)
859 char i_buf
[PR_REG_ISID_ID_LEN
];
861 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
862 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
864 spin_lock(&dev
->dev_reservation_lock
);
865 dev
->dev_pr_res_holder
= pr_reg
;
866 spin_unlock(&dev
->dev_reservation_lock
);
868 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
869 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
870 tpg
->se_tpg_tfo
->get_fabric_name(),
871 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
872 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
873 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
874 tpg
->se_tpg_tfo
->get_fabric_name(), node_acl
->initiatorname
,
878 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
879 struct t10_pr_registration
*, enum register_type
, int);
881 static int __core_scsi3_check_aptpl_registration(
882 struct se_device
*dev
,
883 struct se_portal_group
*tpg
,
886 struct se_node_acl
*nacl
,
887 struct se_dev_entry
*deve
)
889 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
890 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
891 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
892 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
895 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
896 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
898 * Copy Initiator Port information from struct se_node_acl
900 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
901 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
902 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
903 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
905 * Look for the matching registrations+reservation from those
906 * created from APTPL metadata. Note that multiple registrations
907 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
910 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
911 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
913 if (!strcmp(pr_reg
->pr_iport
, i_port
) &&
914 (pr_reg
->pr_res_mapped_lun
== deve
->mapped_lun
) &&
915 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
916 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
917 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
919 pr_reg
->pr_reg_nacl
= nacl
;
920 pr_reg
->pr_reg_deve
= deve
;
921 pr_reg
->pr_reg_tg_pt_lun
= lun
;
923 list_del(&pr_reg
->pr_reg_aptpl_list
);
924 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
926 * At this point all of the pointers in *pr_reg will
927 * be setup, so go ahead and add the registration.
930 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
932 * If this registration is the reservation holder,
933 * make that happen now..
935 if (pr_reg
->pr_res_holder
)
936 core_scsi3_aptpl_reserve(dev
, tpg
,
939 * Reenable pr_aptpl_active to accept new metadata
940 * updates once the SCSI device is active again..
942 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
943 pr_tmpl
->pr_aptpl_active
= 1;
946 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
951 int core_scsi3_check_aptpl_registration(
952 struct se_device
*dev
,
953 struct se_portal_group
*tpg
,
955 struct se_lun_acl
*lun_acl
)
957 struct se_node_acl
*nacl
= lun_acl
->se_lun_nacl
;
958 struct se_dev_entry
*deve
= nacl
->device_list
[lun_acl
->mapped_lun
];
960 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
963 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
964 lun
->unpacked_lun
, nacl
, deve
);
967 static void __core_scsi3_dump_registration(
968 struct target_core_fabric_ops
*tfo
,
969 struct se_device
*dev
,
970 struct se_node_acl
*nacl
,
971 struct t10_pr_registration
*pr_reg
,
972 enum register_type register_type
)
974 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
975 char i_buf
[PR_REG_ISID_ID_LEN
];
977 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
978 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
980 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
981 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== REGISTER_AND_MOVE
) ?
982 "_AND_MOVE" : (register_type
== REGISTER_AND_IGNORE_EXISTING_KEY
) ?
983 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
985 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
986 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
987 tfo
->tpg_get_tag(se_tpg
));
988 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
989 " Port(s)\n", tfo
->get_fabric_name(),
990 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
991 dev
->transport
->name
);
992 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
993 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
994 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
995 pr_reg
->pr_reg_aptpl
);
999 * this function can be called with struct se_device->dev_reservation_lock
1000 * when register_move = 1
1002 static void __core_scsi3_add_registration(
1003 struct se_device
*dev
,
1004 struct se_node_acl
*nacl
,
1005 struct t10_pr_registration
*pr_reg
,
1006 enum register_type register_type
,
1009 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1010 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1011 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1014 * Increment PRgeneration counter for struct se_device upon a successful
1015 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1017 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1018 * action, the struct se_device->dev_reservation_lock will already be held,
1019 * so we do not call core_scsi3_pr_generation() which grabs the lock
1022 pr_reg
->pr_res_generation
= (register_move
) ?
1023 dev
->t10_pr
.pr_generation
++ :
1024 core_scsi3_pr_generation(dev
);
1026 spin_lock(&pr_tmpl
->registration_lock
);
1027 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1028 pr_reg
->pr_reg_deve
->def_pr_registered
= 1;
1030 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1031 spin_unlock(&pr_tmpl
->registration_lock
);
1033 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1035 if (!pr_reg
->pr_reg_all_tg_pt
|| register_move
)
1038 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1039 * allocated in __core_scsi3_alloc_registration()
1041 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1042 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1043 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1045 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1047 spin_lock(&pr_tmpl
->registration_lock
);
1048 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1049 &pr_tmpl
->registration_list
);
1050 pr_reg_tmp
->pr_reg_deve
->def_pr_registered
= 1;
1052 __core_scsi3_dump_registration(tfo
, dev
,
1053 pr_reg_tmp
->pr_reg_nacl
, pr_reg_tmp
,
1055 spin_unlock(&pr_tmpl
->registration_lock
);
1057 * Drop configfs group dependency reference from
1058 * __core_scsi3_alloc_registration()
1060 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1064 static int core_scsi3_alloc_registration(
1065 struct se_device
*dev
,
1066 struct se_node_acl
*nacl
,
1067 struct se_dev_entry
*deve
,
1068 unsigned char *isid
,
1072 enum register_type register_type
,
1075 struct t10_pr_registration
*pr_reg
;
1077 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, deve
, isid
,
1078 sa_res_key
, all_tg_pt
, aptpl
);
1082 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1083 register_type
, register_move
);
1087 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1088 struct se_device
*dev
,
1089 struct se_node_acl
*nacl
,
1090 unsigned char *isid
)
1092 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1093 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1094 struct se_portal_group
*tpg
;
1096 spin_lock(&pr_tmpl
->registration_lock
);
1097 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1098 &pr_tmpl
->registration_list
, pr_reg_list
) {
1100 * First look for a matching struct se_node_acl
1102 if (pr_reg
->pr_reg_nacl
!= nacl
)
1105 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1107 * If this registration does NOT contain a fabric provided
1108 * ISID, then we have found a match.
1110 if (!pr_reg
->isid_present_at_reg
) {
1112 * Determine if this SCSI device server requires that
1113 * SCSI Intiatior TransportID w/ ISIDs is enforced
1114 * for fabric modules (iSCSI) requiring them.
1116 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1117 if (dev
->dev_attrib
.enforce_pr_isids
)
1120 atomic_inc(&pr_reg
->pr_res_holders
);
1121 smp_mb__after_atomic_inc();
1122 spin_unlock(&pr_tmpl
->registration_lock
);
1126 * If the *pr_reg contains a fabric defined ISID for multi-value
1127 * SCSI Initiator Port TransportIDs, then we expect a valid
1128 * matching ISID to be provided by the local SCSI Initiator Port.
1132 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1135 atomic_inc(&pr_reg
->pr_res_holders
);
1136 smp_mb__after_atomic_inc();
1137 spin_unlock(&pr_tmpl
->registration_lock
);
1140 spin_unlock(&pr_tmpl
->registration_lock
);
1145 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1146 struct se_device
*dev
,
1147 struct se_node_acl
*nacl
,
1148 struct se_session
*sess
)
1150 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1151 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1153 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1154 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1155 tpg
->se_tpg_tfo
->sess_get_initiator_sid(sess
, &buf
[0],
1160 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1163 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1165 atomic_dec(&pr_reg
->pr_res_holders
);
1166 smp_mb__after_atomic_dec();
1169 static int core_scsi3_check_implict_release(
1170 struct se_device
*dev
,
1171 struct t10_pr_registration
*pr_reg
)
1173 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1174 struct t10_pr_registration
*pr_res_holder
;
1177 spin_lock(&dev
->dev_reservation_lock
);
1178 pr_res_holder
= dev
->dev_pr_res_holder
;
1179 if (!pr_res_holder
) {
1180 spin_unlock(&dev
->dev_reservation_lock
);
1183 if (pr_res_holder
== pr_reg
) {
1185 * Perform an implict RELEASE if the registration that
1186 * is being released is holding the reservation.
1188 * From spc4r17, section 5.7.11.1:
1190 * e) If the I_T nexus is the persistent reservation holder
1191 * and the persistent reservation is not an all registrants
1192 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1193 * service action or REGISTER AND IGNORE EXISTING KEY
1194 * service action with the SERVICE ACTION RESERVATION KEY
1195 * field set to zero (see 5.7.11.3).
1197 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0);
1200 * For 'All Registrants' reservation types, all existing
1201 * registrations are still processed as reservation holders
1202 * in core_scsi3_pr_seq_non_holder() after the initial
1203 * reservation holder is implictly released here.
1205 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1206 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1207 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1208 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1209 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1210 " UNREGISTER while existing reservation with matching"
1211 " key 0x%016Lx is present from another SCSI Initiator"
1212 " Port\n", pr_reg
->pr_res_key
);
1215 spin_unlock(&dev
->dev_reservation_lock
);
1221 * Called with struct t10_reservation->registration_lock held.
1223 static void __core_scsi3_free_registration(
1224 struct se_device
*dev
,
1225 struct t10_pr_registration
*pr_reg
,
1226 struct list_head
*preempt_and_abort_list
,
1229 struct target_core_fabric_ops
*tfo
=
1230 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1231 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1232 char i_buf
[PR_REG_ISID_ID_LEN
];
1234 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1235 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1237 pr_reg
->pr_reg_deve
->def_pr_registered
= 0;
1238 pr_reg
->pr_reg_deve
->pr_res_key
= 0;
1239 list_del(&pr_reg
->pr_reg_list
);
1241 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1242 * so call core_scsi3_put_pr_reg() to decrement our reference.
1245 core_scsi3_put_pr_reg(pr_reg
);
1247 * Wait until all reference from any other I_T nexuses for this
1248 * *pr_reg have been released. Because list_del() is called above,
1249 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1250 * count back to zero, and we release *pr_reg.
1252 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1253 spin_unlock(&pr_tmpl
->registration_lock
);
1254 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1255 tfo
->get_fabric_name());
1257 spin_lock(&pr_tmpl
->registration_lock
);
1260 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1261 " Node: %s%s\n", tfo
->get_fabric_name(),
1262 pr_reg
->pr_reg_nacl
->initiatorname
,
1264 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1265 " Port(s)\n", tfo
->get_fabric_name(),
1266 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1267 dev
->transport
->name
);
1268 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1269 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1270 pr_reg
->pr_res_generation
);
1272 if (!preempt_and_abort_list
) {
1273 pr_reg
->pr_reg_deve
= NULL
;
1274 pr_reg
->pr_reg_nacl
= NULL
;
1275 kfree(pr_reg
->pr_aptpl_buf
);
1276 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1280 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1281 * are released once the ABORT_TASK_SET has completed..
1283 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1286 void core_scsi3_free_pr_reg_from_nacl(
1287 struct se_device
*dev
,
1288 struct se_node_acl
*nacl
)
1290 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1291 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1293 * If the passed se_node_acl matches the reservation holder,
1294 * release the reservation.
1296 spin_lock(&dev
->dev_reservation_lock
);
1297 pr_res_holder
= dev
->dev_pr_res_holder
;
1298 if ((pr_res_holder
!= NULL
) &&
1299 (pr_res_holder
->pr_reg_nacl
== nacl
))
1300 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0);
1301 spin_unlock(&dev
->dev_reservation_lock
);
1303 * Release any registration associated with the struct se_node_acl.
1305 spin_lock(&pr_tmpl
->registration_lock
);
1306 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1307 &pr_tmpl
->registration_list
, pr_reg_list
) {
1309 if (pr_reg
->pr_reg_nacl
!= nacl
)
1312 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1314 spin_unlock(&pr_tmpl
->registration_lock
);
1317 void core_scsi3_free_all_registrations(
1318 struct se_device
*dev
)
1320 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1321 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1323 spin_lock(&dev
->dev_reservation_lock
);
1324 pr_res_holder
= dev
->dev_pr_res_holder
;
1325 if (pr_res_holder
!= NULL
) {
1326 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1327 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1330 spin_unlock(&dev
->dev_reservation_lock
);
1332 spin_lock(&pr_tmpl
->registration_lock
);
1333 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1334 &pr_tmpl
->registration_list
, pr_reg_list
) {
1336 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1338 spin_unlock(&pr_tmpl
->registration_lock
);
1340 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1341 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1342 pr_reg_aptpl_list
) {
1343 list_del(&pr_reg
->pr_reg_aptpl_list
);
1344 kfree(pr_reg
->pr_aptpl_buf
);
1345 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1347 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1350 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1352 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1353 &tpg
->tpg_group
.cg_item
);
1356 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1358 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1359 &tpg
->tpg_group
.cg_item
);
1361 atomic_dec(&tpg
->tpg_pr_ref_count
);
1362 smp_mb__after_atomic_dec();
1365 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1367 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1369 if (nacl
->dynamic_node_acl
)
1372 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1373 &nacl
->acl_group
.cg_item
);
1376 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1378 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1380 if (nacl
->dynamic_node_acl
) {
1381 atomic_dec(&nacl
->acl_pr_ref_count
);
1382 smp_mb__after_atomic_dec();
1386 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1387 &nacl
->acl_group
.cg_item
);
1389 atomic_dec(&nacl
->acl_pr_ref_count
);
1390 smp_mb__after_atomic_dec();
1393 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1395 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1396 struct se_node_acl
*nacl
;
1397 struct se_portal_group
*tpg
;
1399 * For nacl->dynamic_node_acl=1
1404 nacl
= lun_acl
->se_lun_nacl
;
1407 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1408 &lun_acl
->se_lun_group
.cg_item
);
1411 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1413 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1414 struct se_node_acl
*nacl
;
1415 struct se_portal_group
*tpg
;
1417 * For nacl->dynamic_node_acl=1
1420 atomic_dec(&se_deve
->pr_ref_count
);
1421 smp_mb__after_atomic_dec();
1424 nacl
= lun_acl
->se_lun_nacl
;
1427 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1428 &lun_acl
->se_lun_group
.cg_item
);
1430 atomic_dec(&se_deve
->pr_ref_count
);
1431 smp_mb__after_atomic_dec();
1434 static sense_reason_t
1435 core_scsi3_decode_spec_i_port(
1437 struct se_portal_group
*tpg
,
1438 unsigned char *l_isid
,
1443 struct se_device
*dev
= cmd
->se_dev
;
1444 struct se_port
*tmp_port
;
1445 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1446 struct se_session
*se_sess
= cmd
->se_sess
;
1447 struct se_node_acl
*dest_node_acl
= NULL
;
1448 struct se_dev_entry
*dest_se_deve
= NULL
, *local_se_deve
;
1449 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1450 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1451 LIST_HEAD(tid_dest_list
);
1452 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1453 struct target_core_fabric_ops
*tmp_tf_ops
;
1455 unsigned char *ptr
, *i_str
= NULL
, proto_ident
, tmp_proto_ident
;
1456 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
1458 u32 tpdl
, tid_len
= 0;
1459 int dest_local_nexus
;
1462 memset(dest_iport
, 0, 64);
1464 local_se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
1466 * Allocate a struct pr_transport_id_holder and setup the
1467 * local_node_acl and local_se_deve pointers and add to
1468 * struct list_head tid_dest_list for add registration
1469 * processing in the loop of tid_dest_list below.
1471 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1473 pr_err("Unable to allocate tidh_new\n");
1474 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1476 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1477 tidh_new
->dest_tpg
= tpg
;
1478 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1479 tidh_new
->dest_se_deve
= local_se_deve
;
1481 local_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1482 se_sess
->se_node_acl
, local_se_deve
, l_isid
,
1483 sa_res_key
, all_tg_pt
, aptpl
);
1484 if (!local_pr_reg
) {
1486 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1488 tidh_new
->dest_pr_reg
= local_pr_reg
;
1490 * The local I_T nexus does not hold any configfs dependances,
1491 * so we set tid_h->dest_local_nexus=1 to prevent the
1492 * configfs_undepend_item() calls in the tid_dest_list loops below.
1494 tidh_new
->dest_local_nexus
= 1;
1495 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1497 if (cmd
->data_length
< 28) {
1498 pr_warn("SPC-PR: Received PR OUT parameter list"
1499 " length too small: %u\n", cmd
->data_length
);
1500 ret
= TCM_INVALID_PARAMETER_LIST
;
1504 buf
= transport_kmap_data_sg(cmd
);
1506 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1511 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1512 * first extract TransportID Parameter Data Length, and make sure
1513 * the value matches up to the SCSI expected data transfer length.
1515 tpdl
= (buf
[24] & 0xff) << 24;
1516 tpdl
|= (buf
[25] & 0xff) << 16;
1517 tpdl
|= (buf
[26] & 0xff) << 8;
1518 tpdl
|= buf
[27] & 0xff;
1520 if ((tpdl
+ 28) != cmd
->data_length
) {
1521 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1522 " does not equal CDB data_length: %u\n", tpdl
,
1524 ret
= TCM_INVALID_PARAMETER_LIST
;
1528 * Start processing the received transport IDs using the
1529 * receiving I_T Nexus portal's fabric dependent methods to
1530 * obtain the SCSI Initiator Port/Device Identifiers.
1535 proto_ident
= (ptr
[0] & 0x0f);
1538 spin_lock(&dev
->se_port_lock
);
1539 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1540 tmp_tpg
= tmp_port
->sep_tpg
;
1543 tmp_tf_ops
= tmp_tpg
->se_tpg_tfo
;
1546 if (!tmp_tf_ops
->get_fabric_proto_ident
||
1547 !tmp_tf_ops
->tpg_parse_pr_out_transport_id
)
1550 * Look for the matching proto_ident provided by
1551 * the received TransportID
1553 tmp_proto_ident
= tmp_tf_ops
->get_fabric_proto_ident(tmp_tpg
);
1554 if (tmp_proto_ident
!= proto_ident
)
1556 dest_rtpi
= tmp_port
->sep_rtpi
;
1558 i_str
= tmp_tf_ops
->tpg_parse_pr_out_transport_id(
1559 tmp_tpg
, (const char *)ptr
, &tid_len
,
1564 atomic_inc(&tmp_tpg
->tpg_pr_ref_count
);
1565 smp_mb__after_atomic_inc();
1566 spin_unlock(&dev
->se_port_lock
);
1568 if (core_scsi3_tpg_depend_item(tmp_tpg
)) {
1569 pr_err(" core_scsi3_tpg_depend_item()"
1571 atomic_dec(&tmp_tpg
->tpg_pr_ref_count
);
1572 smp_mb__after_atomic_dec();
1573 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1577 * Locate the destination initiator ACL to be registered
1578 * from the decoded fabric module specific TransportID
1581 spin_lock_irq(&tmp_tpg
->acl_node_lock
);
1582 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1584 if (dest_node_acl
) {
1585 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
1586 smp_mb__after_atomic_inc();
1588 spin_unlock_irq(&tmp_tpg
->acl_node_lock
);
1590 if (!dest_node_acl
) {
1591 core_scsi3_tpg_undepend_item(tmp_tpg
);
1592 spin_lock(&dev
->se_port_lock
);
1596 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
1597 pr_err("configfs_depend_item() failed"
1598 " for dest_node_acl->acl_group\n");
1599 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
1600 smp_mb__after_atomic_dec();
1601 core_scsi3_tpg_undepend_item(tmp_tpg
);
1602 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1607 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1608 " %s Port RTPI: %hu\n",
1609 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1610 dest_node_acl
->initiatorname
, dest_rtpi
);
1612 spin_lock(&dev
->se_port_lock
);
1615 spin_unlock(&dev
->se_port_lock
);
1618 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1620 ret
= TCM_INVALID_PARAMETER_LIST
;
1624 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1625 " tid_len: %d for %s + %s\n",
1626 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1627 tpdl
, tid_len
, i_str
, iport_ptr
);
1629 if (tid_len
> tpdl
) {
1630 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1631 " %u for Transport ID: %s\n", tid_len
, ptr
);
1632 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1633 core_scsi3_tpg_undepend_item(dest_tpg
);
1634 ret
= TCM_INVALID_PARAMETER_LIST
;
1638 * Locate the desintation struct se_dev_entry pointer for matching
1639 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1642 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1644 if (!dest_se_deve
) {
1645 pr_err("Unable to locate %s dest_se_deve"
1646 " from destination RTPI: %hu\n",
1647 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1650 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1651 core_scsi3_tpg_undepend_item(dest_tpg
);
1652 ret
= TCM_INVALID_PARAMETER_LIST
;
1656 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
1657 pr_err("core_scsi3_lunacl_depend_item()"
1659 atomic_dec(&dest_se_deve
->pr_ref_count
);
1660 smp_mb__after_atomic_dec();
1661 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1662 core_scsi3_tpg_undepend_item(dest_tpg
);
1663 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1667 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1668 " dest_se_deve mapped_lun: %u\n",
1669 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1670 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1673 * Skip any TransportIDs that already have a registration for
1676 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1679 core_scsi3_put_pr_reg(pr_reg_e
);
1680 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1681 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1682 core_scsi3_tpg_undepend_item(dest_tpg
);
1689 * Allocate a struct pr_transport_id_holder and setup
1690 * the dest_node_acl and dest_se_deve pointers for the
1693 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1696 pr_err("Unable to allocate tidh_new\n");
1697 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1698 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1699 core_scsi3_tpg_undepend_item(dest_tpg
);
1700 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1703 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1704 tidh_new
->dest_tpg
= dest_tpg
;
1705 tidh_new
->dest_node_acl
= dest_node_acl
;
1706 tidh_new
->dest_se_deve
= dest_se_deve
;
1709 * Allocate, but do NOT add the registration for the
1710 * TransportID referenced SCSI Initiator port. This
1711 * done because of the following from spc4r17 in section
1712 * 6.14.3 wrt SPEC_I_PT:
1714 * "If a registration fails for any initiator port (e.g., if th
1715 * logical unit does not have enough resources available to
1716 * hold the registration information), no registrations shall be
1717 * made, and the command shall be terminated with
1718 * CHECK CONDITION status."
1720 * That means we call __core_scsi3_alloc_registration() here,
1721 * and then call __core_scsi3_add_registration() in the
1722 * 2nd loop which will never fail.
1724 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1725 dest_node_acl
, dest_se_deve
, iport_ptr
,
1726 sa_res_key
, all_tg_pt
, aptpl
);
1728 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1729 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1730 core_scsi3_tpg_undepend_item(dest_tpg
);
1732 ret
= TCM_INVALID_PARAMETER_LIST
;
1735 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1736 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1744 transport_kunmap_data_sg(cmd
);
1747 * Go ahead and create a registrations from tid_dest_list for the
1748 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1751 * The SA Reservation Key from the PROUT is set for the
1752 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1753 * means that the TransportID Initiator port will be
1754 * registered on all of the target ports in the SCSI target device
1755 * ALL_TG_PT=0 means the registration will only be for the
1756 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1759 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1760 dest_tpg
= tidh
->dest_tpg
;
1761 dest_node_acl
= tidh
->dest_node_acl
;
1762 dest_se_deve
= tidh
->dest_se_deve
;
1763 dest_pr_reg
= tidh
->dest_pr_reg
;
1764 dest_local_nexus
= tidh
->dest_local_nexus
;
1766 list_del(&tidh
->dest_list
);
1769 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1770 core_pr_dump_initiator_port(dest_pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1772 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1775 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1776 " registered Transport ID for Node: %s%s Mapped LUN:"
1777 " %u\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1778 dest_node_acl
->initiatorname
, i_buf
, dest_se_deve
->mapped_lun
);
1780 if (dest_local_nexus
)
1783 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1784 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1785 core_scsi3_tpg_undepend_item(dest_tpg
);
1790 transport_kunmap_data_sg(cmd
);
1793 * For the failure case, release everything from tid_dest_list
1794 * including *dest_pr_reg and the configfs dependances..
1796 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1797 dest_tpg
= tidh
->dest_tpg
;
1798 dest_node_acl
= tidh
->dest_node_acl
;
1799 dest_se_deve
= tidh
->dest_se_deve
;
1800 dest_pr_reg
= tidh
->dest_pr_reg
;
1801 dest_local_nexus
= tidh
->dest_local_nexus
;
1803 list_del(&tidh
->dest_list
);
1806 * Release any extra ALL_TG_PT=1 registrations for
1807 * the SPEC_I_PT=1 case.
1809 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1810 &dest_pr_reg
->pr_reg_atp_list
,
1811 pr_reg_atp_mem_list
) {
1812 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1813 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1814 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1817 kfree(dest_pr_reg
->pr_aptpl_buf
);
1818 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1820 if (dest_local_nexus
)
1823 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1824 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1825 core_scsi3_tpg_undepend_item(dest_tpg
);
1830 static int core_scsi3_update_aptpl_buf(
1831 struct se_device
*dev
,
1833 u32 pr_aptpl_buf_len
)
1836 struct se_portal_group
*tpg
;
1837 struct t10_pr_registration
*pr_reg
;
1838 unsigned char tmp
[512], isid_buf
[32];
1843 memset(buf
, 0, pr_aptpl_buf_len
);
1845 spin_lock(&dev
->dev_reservation_lock
);
1846 spin_lock(&dev
->t10_pr
.registration_lock
);
1848 * Walk the registration list..
1850 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
1855 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1856 lun
= pr_reg
->pr_reg_tg_pt_lun
;
1858 * Write out any ISID value to APTPL metadata that was included
1859 * in the original registration.
1861 if (pr_reg
->isid_present_at_reg
)
1862 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1863 pr_reg
->pr_reg_isid
);
1865 * Include special metadata if the pr_reg matches the
1866 * reservation holder.
1868 if (dev
->dev_pr_res_holder
== pr_reg
) {
1869 snprintf(tmp
, 512, "PR_REG_START: %d"
1870 "\ninitiator_fabric=%s\n"
1871 "initiator_node=%s\n%s"
1873 "res_holder=1\nres_type=%02x\n"
1874 "res_scope=%02x\nres_all_tg_pt=%d\n"
1875 "mapped_lun=%u\n", reg_count
,
1876 tpg
->se_tpg_tfo
->get_fabric_name(),
1877 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1878 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1879 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1880 pr_reg
->pr_res_mapped_lun
);
1882 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1883 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1884 "sa_res_key=%llu\nres_holder=0\n"
1885 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1886 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1887 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1888 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1889 pr_reg
->pr_res_mapped_lun
);
1892 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1893 pr_err("Unable to update renaming"
1894 " APTPL metadata\n");
1898 len
+= sprintf(buf
+len
, "%s", tmp
);
1901 * Include information about the associated SCSI target port.
1903 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1904 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1905 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1906 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1907 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1908 lun
->lun_sep
->sep_rtpi
, lun
->unpacked_lun
, reg_count
);
1910 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1911 pr_err("Unable to update renaming"
1912 " APTPL metadata\n");
1916 len
+= sprintf(buf
+len
, "%s", tmp
);
1921 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1924 spin_unlock(&dev
->t10_pr
.registration_lock
);
1925 spin_unlock(&dev
->dev_reservation_lock
);
1931 * Called with struct se_device->aptpl_file_mutex held
1933 static int __core_scsi3_write_aptpl_to_file(
1934 struct se_device
*dev
,
1936 u32 pr_aptpl_buf_len
)
1938 struct t10_wwn
*wwn
= &dev
->t10_wwn
;
1940 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1944 memset(path
, 0, 512);
1946 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
1947 pr_err("WWN value for struct se_device does not fit"
1948 " into path buffer\n");
1952 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
1953 file
= filp_open(path
, flags
, 0600);
1955 pr_err("filp_open(%s) for APTPL metadata"
1957 return PTR_ERR(file
);
1960 if (!pr_aptpl_buf_len
)
1961 pr_aptpl_buf_len
= (strlen(&buf
[0]) + 1); /* Add extra for NULL */
1963 ret
= kernel_write(file
, buf
, pr_aptpl_buf_len
, 0);
1966 pr_debug("Error writing APTPL metadata file: %s\n", path
);
1969 return ret
? -EIO
: 0;
1973 core_scsi3_update_and_write_aptpl(struct se_device
*dev
, unsigned char *in_buf
,
1974 u32 in_pr_aptpl_buf_len
)
1976 unsigned char null_buf
[64], *buf
;
1980 * Can be called with a NULL pointer from PROUT service action CLEAR
1983 snprintf(null_buf
, 64, "No Registrations or Reservations\n");
1986 ret
= core_scsi3_update_aptpl_buf(dev
, in_buf
, in_pr_aptpl_buf_len
);
1993 * __core_scsi3_write_aptpl_to_file() will call strlen()
1994 * on the passed buf to determine pr_aptpl_buf_len.
1996 return __core_scsi3_write_aptpl_to_file(dev
, buf
, 0);
1999 static sense_reason_t
2000 core_scsi3_emulate_pro_register(struct se_cmd
*cmd
, u64 res_key
, u64 sa_res_key
,
2001 int aptpl
, int all_tg_pt
, int spec_i_pt
, enum register_type register_type
)
2003 struct se_session
*se_sess
= cmd
->se_sess
;
2004 struct se_device
*dev
= cmd
->se_dev
;
2005 struct se_dev_entry
*se_deve
;
2006 struct se_lun
*se_lun
= cmd
->se_lun
;
2007 struct se_portal_group
*se_tpg
;
2008 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
, *pr_reg_e
;
2009 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2010 /* Used for APTPL metadata w/ UNREGISTER */
2011 unsigned char *pr_aptpl_buf
= NULL
;
2012 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2013 sense_reason_t ret
= TCM_NO_SENSE
;
2014 int pr_holder
= 0, type
;
2016 if (!se_sess
|| !se_lun
) {
2017 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2018 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2020 se_tpg
= se_sess
->se_tpg
;
2021 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2023 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2024 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2025 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2027 isid_ptr
= &isid_buf
[0];
2030 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2032 pr_reg_e
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2035 pr_warn("SPC-3 PR: Reservation Key non-zero"
2036 " for SA REGISTER, returning CONFLICT\n");
2037 return TCM_RESERVATION_CONFLICT
;
2040 * Do nothing but return GOOD status.
2047 * Perform the Service Action REGISTER on the Initiator
2048 * Port Endpoint that the PRO was received from on the
2049 * Logical Unit of the SCSI device server.
2051 if (core_scsi3_alloc_registration(cmd
->se_dev
,
2052 se_sess
->se_node_acl
, se_deve
, isid_ptr
,
2053 sa_res_key
, all_tg_pt
, aptpl
,
2054 register_type
, 0)) {
2055 pr_err("Unable to allocate"
2056 " struct t10_pr_registration\n");
2057 return TCM_INVALID_PARAMETER_LIST
;
2061 * Register both the Initiator port that received
2062 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2063 * TransportID from Parameter list and loop through
2064 * fabric dependent parameter list while calling
2065 * logic from of core_scsi3_alloc_registration() for
2066 * each TransportID provided SCSI Initiator Port/Device
2068 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2069 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2074 * Nothing left to do for the APTPL=0 case.
2077 pr_tmpl
->pr_aptpl_active
= 0;
2078 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2079 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2084 * Locate the newly allocated local I_T Nexus *pr_reg, and
2085 * update the APTPL metadata information using its
2086 * preallocated *pr_reg->pr_aptpl_buf.
2088 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2089 se_sess
->se_node_acl
, se_sess
);
2091 if (core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2092 pr_reg
->pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
2093 pr_tmpl
->pr_aptpl_active
= 1;
2094 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2097 goto out_put_pr_reg
;
2101 * Locate the existing *pr_reg via struct se_node_acl pointers
2104 type
= pr_reg
->pr_res_type
;
2106 if (register_type
== REGISTER
) {
2107 if (res_key
!= pr_reg
->pr_res_key
) {
2108 pr_err("SPC-3 PR REGISTER: Received"
2109 " res_key: 0x%016Lx does not match"
2110 " existing SA REGISTER res_key:"
2111 " 0x%016Lx\n", res_key
,
2112 pr_reg
->pr_res_key
);
2113 ret
= TCM_RESERVATION_CONFLICT
;
2114 goto out_put_pr_reg
;
2119 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2120 " set on a registered nexus\n");
2121 ret
= TCM_INVALID_PARAMETER_LIST
;
2122 goto out_put_pr_reg
;
2126 * An existing ALL_TG_PT=1 registration being released
2127 * must also set ALL_TG_PT=1 in the incoming PROUT.
2129 if (pr_reg
->pr_reg_all_tg_pt
&& !all_tg_pt
) {
2130 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2131 " registration exists, but ALL_TG_PT=1 bit not"
2132 " present in received PROUT\n");
2133 ret
= TCM_INVALID_CDB_FIELD
;
2134 goto out_put_pr_reg
;
2138 * Allocate APTPL metadata buffer used for UNREGISTER ops
2141 pr_aptpl_buf
= kzalloc(PR_APTPL_BUF_LEN
, GFP_KERNEL
);
2142 if (!pr_aptpl_buf
) {
2143 pr_err("Unable to allocate"
2145 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2146 goto out_put_pr_reg
;
2151 * sa_res_key=0 Unregister Reservation Key for registered I_T
2152 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2156 pr_holder
= core_scsi3_check_implict_release(
2157 cmd
->se_dev
, pr_reg
);
2158 if (pr_holder
< 0) {
2159 kfree(pr_aptpl_buf
);
2160 ret
= TCM_RESERVATION_CONFLICT
;
2161 goto out_put_pr_reg
;
2164 spin_lock(&pr_tmpl
->registration_lock
);
2166 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2167 * and matching pr_res_key.
2169 if (pr_reg
->pr_reg_all_tg_pt
) {
2170 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2171 &pr_tmpl
->registration_list
,
2174 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2176 if (pr_reg_p
->pr_res_key
!= res_key
)
2178 if (pr_reg
== pr_reg_p
)
2180 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2181 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2184 __core_scsi3_free_registration(dev
,
2190 * Release the calling I_T Nexus registration now..
2192 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
, NULL
, 1);
2195 * From spc4r17, section 5.7.11.3 Unregistering
2197 * If the persistent reservation is a registrants only
2198 * type, the device server shall establish a unit
2199 * attention condition for the initiator port associated
2200 * with every registered I_T nexus except for the I_T
2201 * nexus on which the PERSISTENT RESERVE OUT command was
2202 * received, with the additional sense code set to
2203 * RESERVATIONS RELEASED.
2206 (type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
||
2207 type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
)) {
2208 list_for_each_entry(pr_reg_p
,
2209 &pr_tmpl
->registration_list
,
2212 core_scsi3_ua_allocate(
2213 pr_reg_p
->pr_reg_nacl
,
2214 pr_reg_p
->pr_res_mapped_lun
,
2216 ASCQ_2AH_RESERVATIONS_RELEASED
);
2219 spin_unlock(&pr_tmpl
->registration_lock
);
2222 pr_tmpl
->pr_aptpl_active
= 0;
2223 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2224 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2225 " for UNREGISTER\n");
2229 if (!core_scsi3_update_and_write_aptpl(dev
, pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
2230 pr_tmpl
->pr_aptpl_active
= 1;
2231 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2232 " for UNREGISTER\n");
2235 goto out_free_aptpl_buf
;
2239 * Increment PRgeneration counter for struct se_device"
2240 * upon a successful REGISTER, see spc4r17 section 6.3.2
2241 * READ_KEYS service action.
2243 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(cmd
->se_dev
);
2244 pr_reg
->pr_res_key
= sa_res_key
;
2245 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2246 " Key for %s to: 0x%016Lx PRgeneration:"
2247 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2248 (register_type
== REGISTER_AND_IGNORE_EXISTING_KEY
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2249 pr_reg
->pr_reg_nacl
->initiatorname
,
2250 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2253 pr_tmpl
->pr_aptpl_active
= 0;
2254 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2255 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2258 goto out_put_pr_reg
;
2261 if (!core_scsi3_update_and_write_aptpl(dev
, pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
2262 pr_tmpl
->pr_aptpl_active
= 1;
2263 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2268 kfree(pr_aptpl_buf
);
2271 core_scsi3_put_pr_reg(pr_reg
);
2275 unsigned char *core_scsi3_pr_dump_type(int type
)
2278 case PR_TYPE_WRITE_EXCLUSIVE
:
2279 return "Write Exclusive Access";
2280 case PR_TYPE_EXCLUSIVE_ACCESS
:
2281 return "Exclusive Access";
2282 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2283 return "Write Exclusive Access, Registrants Only";
2284 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2285 return "Exclusive Access, Registrants Only";
2286 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2287 return "Write Exclusive Access, All Registrants";
2288 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2289 return "Exclusive Access, All Registrants";
2294 return "Unknown SPC-3 PR Type";
2297 static sense_reason_t
2298 core_scsi3_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
)
2300 struct se_device
*dev
= cmd
->se_dev
;
2301 struct se_session
*se_sess
= cmd
->se_sess
;
2302 struct se_lun
*se_lun
= cmd
->se_lun
;
2303 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2304 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2305 char i_buf
[PR_REG_ISID_ID_LEN
];
2308 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2310 if (!se_sess
|| !se_lun
) {
2311 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2312 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2315 * Locate the existing *pr_reg via struct se_node_acl pointers
2317 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2320 pr_err("SPC-3 PR: Unable to locate"
2321 " PR_REGISTERED *pr_reg for RESERVE\n");
2322 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2325 * From spc4r17 Section 5.7.9: Reserving:
2327 * An application client creates a persistent reservation by issuing
2328 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2329 * a registered I_T nexus with the following parameters:
2330 * a) RESERVATION KEY set to the value of the reservation key that is
2331 * registered with the logical unit for the I_T nexus; and
2333 if (res_key
!= pr_reg
->pr_res_key
) {
2334 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2335 " does not match existing SA REGISTER res_key:"
2336 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2337 ret
= TCM_RESERVATION_CONFLICT
;
2338 goto out_put_pr_reg
;
2341 * From spc4r17 Section 5.7.9: Reserving:
2344 * b) TYPE field and SCOPE field set to the persistent reservation
2347 * Only one persistent reservation is allowed at a time per logical unit
2348 * and that persistent reservation has a scope of LU_SCOPE.
2350 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2351 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2352 ret
= TCM_INVALID_PARAMETER_LIST
;
2353 goto out_put_pr_reg
;
2356 * See if we have an existing PR reservation holder pointer at
2357 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2360 spin_lock(&dev
->dev_reservation_lock
);
2361 pr_res_holder
= dev
->dev_pr_res_holder
;
2362 if (pr_res_holder
) {
2364 * From spc4r17 Section 5.7.9: Reserving:
2366 * If the device server receives a PERSISTENT RESERVE OUT
2367 * command from an I_T nexus other than a persistent reservation
2368 * holder (see 5.7.10) that attempts to create a persistent
2369 * reservation when a persistent reservation already exists for
2370 * the logical unit, then the command shall be completed with
2371 * RESERVATION CONFLICT status.
2373 if (pr_res_holder
!= pr_reg
) {
2374 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2375 pr_err("SPC-3 PR: Attempted RESERVE from"
2376 " [%s]: %s while reservation already held by"
2377 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2378 cmd
->se_tfo
->get_fabric_name(),
2379 se_sess
->se_node_acl
->initiatorname
,
2380 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2381 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2383 spin_unlock(&dev
->dev_reservation_lock
);
2384 ret
= TCM_RESERVATION_CONFLICT
;
2385 goto out_put_pr_reg
;
2388 * From spc4r17 Section 5.7.9: Reserving:
2390 * If a persistent reservation holder attempts to modify the
2391 * type or scope of an existing persistent reservation, the
2392 * command shall be completed with RESERVATION CONFLICT status.
2394 if ((pr_res_holder
->pr_res_type
!= type
) ||
2395 (pr_res_holder
->pr_res_scope
!= scope
)) {
2396 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2397 pr_err("SPC-3 PR: Attempted RESERVE from"
2398 " [%s]: %s trying to change TYPE and/or SCOPE,"
2399 " while reservation already held by [%s]: %s,"
2400 " returning RESERVATION_CONFLICT\n",
2401 cmd
->se_tfo
->get_fabric_name(),
2402 se_sess
->se_node_acl
->initiatorname
,
2403 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2404 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2406 spin_unlock(&dev
->dev_reservation_lock
);
2407 ret
= TCM_RESERVATION_CONFLICT
;
2408 goto out_put_pr_reg
;
2411 * From spc4r17 Section 5.7.9: Reserving:
2413 * If the device server receives a PERSISTENT RESERVE OUT
2414 * command with RESERVE service action where the TYPE field and
2415 * the SCOPE field contain the same values as the existing type
2416 * and scope from a persistent reservation holder, it shall not
2417 * make any change to the existing persistent reservation and
2418 * shall completethe command with GOOD status.
2420 spin_unlock(&dev
->dev_reservation_lock
);
2422 goto out_put_pr_reg
;
2425 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2426 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2428 pr_reg
->pr_res_scope
= scope
;
2429 pr_reg
->pr_res_type
= type
;
2430 pr_reg
->pr_res_holder
= 1;
2431 dev
->dev_pr_res_holder
= pr_reg
;
2432 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2434 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2435 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2436 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2437 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2438 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2439 cmd
->se_tfo
->get_fabric_name(),
2440 se_sess
->se_node_acl
->initiatorname
,
2442 spin_unlock(&dev
->dev_reservation_lock
);
2444 if (pr_tmpl
->pr_aptpl_active
) {
2445 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2446 pr_reg
->pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
2447 pr_debug("SPC-3 PR: Updated APTPL metadata"
2454 core_scsi3_put_pr_reg(pr_reg
);
2458 static sense_reason_t
2459 core_scsi3_emulate_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
,
2463 case PR_TYPE_WRITE_EXCLUSIVE
:
2464 case PR_TYPE_EXCLUSIVE_ACCESS
:
2465 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2466 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2467 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2468 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2469 return core_scsi3_pro_reserve(cmd
, type
, scope
, res_key
);
2471 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2473 return TCM_INVALID_CDB_FIELD
;
2478 * Called with struct se_device->dev_reservation_lock held.
2480 static void __core_scsi3_complete_pro_release(
2481 struct se_device
*dev
,
2482 struct se_node_acl
*se_nacl
,
2483 struct t10_pr_registration
*pr_reg
,
2486 struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2487 char i_buf
[PR_REG_ISID_ID_LEN
];
2489 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2490 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2492 * Go ahead and release the current PR reservation holder.
2494 dev
->dev_pr_res_holder
= NULL
;
2496 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2497 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2498 tfo
->get_fabric_name(), (explict
) ? "explict" : "implict",
2499 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
2500 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2501 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2502 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2505 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2507 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2510 static sense_reason_t
2511 core_scsi3_emulate_pro_release(struct se_cmd
*cmd
, int type
, int scope
,
2514 struct se_device
*dev
= cmd
->se_dev
;
2515 struct se_session
*se_sess
= cmd
->se_sess
;
2516 struct se_lun
*se_lun
= cmd
->se_lun
;
2517 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2518 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2520 sense_reason_t ret
= 0;
2522 if (!se_sess
|| !se_lun
) {
2523 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2524 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2527 * Locate the existing *pr_reg via struct se_node_acl pointers
2529 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2531 pr_err("SPC-3 PR: Unable to locate"
2532 " PR_REGISTERED *pr_reg for RELEASE\n");
2533 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2536 * From spc4r17 Section 5.7.11.2 Releasing:
2538 * If there is no persistent reservation or in response to a persistent
2539 * reservation release request from a registered I_T nexus that is not a
2540 * persistent reservation holder (see 5.7.10), the device server shall
2543 * a) Not release the persistent reservation, if any;
2544 * b) Not remove any registrations; and
2545 * c) Complete the command with GOOD status.
2547 spin_lock(&dev
->dev_reservation_lock
);
2548 pr_res_holder
= dev
->dev_pr_res_holder
;
2549 if (!pr_res_holder
) {
2551 * No persistent reservation, return GOOD status.
2553 spin_unlock(&dev
->dev_reservation_lock
);
2554 goto out_put_pr_reg
;
2556 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2557 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
2560 if ((all_reg
== 0) && (pr_res_holder
!= pr_reg
)) {
2562 * Non 'All Registrants' PR Type cases..
2563 * Release request from a registered I_T nexus that is not a
2564 * persistent reservation holder. return GOOD status.
2566 spin_unlock(&dev
->dev_reservation_lock
);
2567 goto out_put_pr_reg
;
2571 * From spc4r17 Section 5.7.11.2 Releasing:
2573 * Only the persistent reservation holder (see 5.7.10) is allowed to
2574 * release a persistent reservation.
2576 * An application client releases the persistent reservation by issuing
2577 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2578 * an I_T nexus that is a persistent reservation holder with the
2579 * following parameters:
2581 * a) RESERVATION KEY field set to the value of the reservation key
2582 * that is registered with the logical unit for the I_T nexus;
2584 if (res_key
!= pr_reg
->pr_res_key
) {
2585 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2586 " does not match existing SA REGISTER res_key:"
2587 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2588 spin_unlock(&dev
->dev_reservation_lock
);
2589 ret
= TCM_RESERVATION_CONFLICT
;
2590 goto out_put_pr_reg
;
2593 * From spc4r17 Section 5.7.11.2 Releasing and above:
2595 * b) TYPE field and SCOPE field set to match the persistent
2596 * reservation being released.
2598 if ((pr_res_holder
->pr_res_type
!= type
) ||
2599 (pr_res_holder
->pr_res_scope
!= scope
)) {
2600 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2601 pr_err("SPC-3 PR RELEASE: Attempted to release"
2602 " reservation from [%s]: %s with different TYPE "
2603 "and/or SCOPE while reservation already held by"
2604 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2605 cmd
->se_tfo
->get_fabric_name(),
2606 se_sess
->se_node_acl
->initiatorname
,
2607 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2608 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2610 spin_unlock(&dev
->dev_reservation_lock
);
2611 ret
= TCM_RESERVATION_CONFLICT
;
2612 goto out_put_pr_reg
;
2615 * In response to a persistent reservation release request from the
2616 * persistent reservation holder the device server shall perform a
2617 * release by doing the following as an uninterrupted series of actions:
2618 * a) Release the persistent reservation;
2619 * b) Not remove any registration(s);
2620 * c) If the released persistent reservation is a registrants only type
2621 * or all registrants type persistent reservation,
2622 * the device server shall establish a unit attention condition for
2623 * the initiator port associated with every regis-
2624 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2625 * RESERVE OUT command with RELEASE service action was received,
2626 * with the additional sense code set to RESERVATIONS RELEASED; and
2627 * d) If the persistent reservation is of any other type, the device
2628 * server shall not establish a unit attention condition.
2630 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2633 spin_unlock(&dev
->dev_reservation_lock
);
2635 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2636 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2637 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2638 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2640 * If no UNIT ATTENTION conditions will be established for
2641 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2642 * go ahead and check for APTPL=1 update+write below
2647 spin_lock(&pr_tmpl
->registration_lock
);
2648 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2651 * Do not establish a UNIT ATTENTION condition
2652 * for the calling I_T Nexus
2654 if (pr_reg_p
== pr_reg
)
2657 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2658 pr_reg_p
->pr_res_mapped_lun
,
2659 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2661 spin_unlock(&pr_tmpl
->registration_lock
);
2664 if (pr_tmpl
->pr_aptpl_active
) {
2665 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2666 pr_reg
->pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
2667 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2671 core_scsi3_put_pr_reg(pr_reg
);
2675 static sense_reason_t
2676 core_scsi3_emulate_pro_clear(struct se_cmd
*cmd
, u64 res_key
)
2678 struct se_device
*dev
= cmd
->se_dev
;
2679 struct se_node_acl
*pr_reg_nacl
;
2680 struct se_session
*se_sess
= cmd
->se_sess
;
2681 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2682 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2683 u32 pr_res_mapped_lun
= 0;
2684 int calling_it_nexus
= 0;
2686 * Locate the existing *pr_reg via struct se_node_acl pointers
2688 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2689 se_sess
->se_node_acl
, se_sess
);
2691 pr_err("SPC-3 PR: Unable to locate"
2692 " PR_REGISTERED *pr_reg for CLEAR\n");
2693 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2696 * From spc4r17 section 5.7.11.6, Clearing:
2698 * Any application client may release the persistent reservation and
2699 * remove all registrations from a device server by issuing a
2700 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2701 * registered I_T nexus with the following parameter:
2703 * a) RESERVATION KEY field set to the value of the reservation key
2704 * that is registered with the logical unit for the I_T nexus.
2706 if (res_key
!= pr_reg_n
->pr_res_key
) {
2707 pr_err("SPC-3 PR REGISTER: Received"
2708 " res_key: 0x%016Lx does not match"
2709 " existing SA REGISTER res_key:"
2710 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2711 core_scsi3_put_pr_reg(pr_reg_n
);
2712 return TCM_RESERVATION_CONFLICT
;
2715 * a) Release the persistent reservation, if any;
2717 spin_lock(&dev
->dev_reservation_lock
);
2718 pr_res_holder
= dev
->dev_pr_res_holder
;
2719 if (pr_res_holder
) {
2720 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2721 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2724 spin_unlock(&dev
->dev_reservation_lock
);
2726 * b) Remove all registration(s) (see spc4r17 5.7.7);
2728 spin_lock(&pr_tmpl
->registration_lock
);
2729 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2730 &pr_tmpl
->registration_list
, pr_reg_list
) {
2732 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2733 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2734 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2735 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2738 * e) Establish a unit attention condition for the initiator
2739 * port associated with every registered I_T nexus other
2740 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2741 * command with CLEAR service action was received, with the
2742 * additional sense code set to RESERVATIONS PREEMPTED.
2744 if (!calling_it_nexus
)
2745 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2746 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2748 spin_unlock(&pr_tmpl
->registration_lock
);
2750 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2751 cmd
->se_tfo
->get_fabric_name());
2753 if (pr_tmpl
->pr_aptpl_active
) {
2754 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2755 pr_debug("SPC-3 PR: Updated APTPL metadata"
2759 core_scsi3_pr_generation(dev
);
2764 * Called with struct se_device->dev_reservation_lock held.
2766 static void __core_scsi3_complete_pro_preempt(
2767 struct se_device
*dev
,
2768 struct t10_pr_registration
*pr_reg
,
2769 struct list_head
*preempt_and_abort_list
,
2772 enum preempt_type preempt_type
)
2774 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2775 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2776 char i_buf
[PR_REG_ISID_ID_LEN
];
2778 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2779 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2781 * Do an implict RELEASE of the existing reservation.
2783 if (dev
->dev_pr_res_holder
)
2784 __core_scsi3_complete_pro_release(dev
, nacl
,
2785 dev
->dev_pr_res_holder
, 0);
2787 dev
->dev_pr_res_holder
= pr_reg
;
2788 pr_reg
->pr_res_holder
= 1;
2789 pr_reg
->pr_res_type
= type
;
2790 pr_reg
->pr_res_scope
= scope
;
2792 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2793 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2794 tfo
->get_fabric_name(), (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "",
2795 core_scsi3_pr_dump_type(type
),
2796 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2797 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2798 tfo
->get_fabric_name(), (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "",
2799 nacl
->initiatorname
, i_buf
);
2801 * For PREEMPT_AND_ABORT, add the preempting reservation's
2802 * struct t10_pr_registration to the list that will be compared
2803 * against received CDBs..
2805 if (preempt_and_abort_list
)
2806 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2807 preempt_and_abort_list
);
2810 static void core_scsi3_release_preempt_and_abort(
2811 struct list_head
*preempt_and_abort_list
,
2812 struct t10_pr_registration
*pr_reg_holder
)
2814 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2816 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2817 pr_reg_abort_list
) {
2819 list_del(&pr_reg
->pr_reg_abort_list
);
2820 if (pr_reg_holder
== pr_reg
)
2822 if (pr_reg
->pr_res_holder
) {
2823 pr_warn("pr_reg->pr_res_holder still set\n");
2827 pr_reg
->pr_reg_deve
= NULL
;
2828 pr_reg
->pr_reg_nacl
= NULL
;
2829 kfree(pr_reg
->pr_aptpl_buf
);
2830 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2834 static sense_reason_t
2835 core_scsi3_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
,
2836 u64 sa_res_key
, enum preempt_type preempt_type
)
2838 struct se_device
*dev
= cmd
->se_dev
;
2839 struct se_node_acl
*pr_reg_nacl
;
2840 struct se_session
*se_sess
= cmd
->se_sess
;
2841 LIST_HEAD(preempt_and_abort_list
);
2842 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2843 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2844 u32 pr_res_mapped_lun
= 0;
2845 int all_reg
= 0, calling_it_nexus
= 0, released_regs
= 0;
2846 int prh_type
= 0, prh_scope
= 0;
2849 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2851 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2854 pr_err("SPC-3 PR: Unable to locate"
2855 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2856 (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "");
2857 return TCM_RESERVATION_CONFLICT
;
2859 if (pr_reg_n
->pr_res_key
!= res_key
) {
2860 core_scsi3_put_pr_reg(pr_reg_n
);
2861 return TCM_RESERVATION_CONFLICT
;
2863 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2864 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2865 core_scsi3_put_pr_reg(pr_reg_n
);
2866 return TCM_INVALID_PARAMETER_LIST
;
2869 spin_lock(&dev
->dev_reservation_lock
);
2870 pr_res_holder
= dev
->dev_pr_res_holder
;
2871 if (pr_res_holder
&&
2872 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2873 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
2876 if (!all_reg
&& !sa_res_key
) {
2877 spin_unlock(&dev
->dev_reservation_lock
);
2878 core_scsi3_put_pr_reg(pr_reg_n
);
2879 return TCM_INVALID_PARAMETER_LIST
;
2882 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2884 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2885 * persistent reservation holder or there is no persistent reservation
2886 * holder (i.e., there is no persistent reservation), then the device
2887 * server shall perform a preempt by doing the following in an
2888 * uninterrupted series of actions. (See below..)
2890 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
2892 * No existing or SA Reservation Key matching reservations..
2894 * PROUT SA PREEMPT with All Registrant type reservations are
2895 * allowed to be processed without a matching SA Reservation Key
2897 spin_lock(&pr_tmpl
->registration_lock
);
2898 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2899 &pr_tmpl
->registration_list
, pr_reg_list
) {
2901 * Removing of registrations in non all registrants
2902 * type reservations without a matching SA reservation
2905 * a) Remove the registrations for all I_T nexuses
2906 * specified by the SERVICE ACTION RESERVATION KEY
2908 * b) Ignore the contents of the SCOPE and TYPE fields;
2909 * c) Process tasks as defined in 5.7.1; and
2910 * d) Establish a unit attention condition for the
2911 * initiator port associated with every I_T nexus
2912 * that lost its registration other than the I_T
2913 * nexus on which the PERSISTENT RESERVE OUT command
2914 * was received, with the additional sense code set
2915 * to REGISTRATIONS PREEMPTED.
2918 if (pr_reg
->pr_res_key
!= sa_res_key
)
2921 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2922 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2923 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2924 __core_scsi3_free_registration(dev
, pr_reg
,
2925 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
:
2926 NULL
, calling_it_nexus
);
2930 * Case for any existing all registrants type
2931 * reservation, follow logic in spc4r17 section
2932 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2934 * For a ZERO SA Reservation key, release
2935 * all other registrations and do an implict
2936 * release of active persistent reservation.
2938 * For a non-ZERO SA Reservation key, only
2939 * release the matching reservation key from
2943 (pr_reg
->pr_res_key
!= sa_res_key
))
2946 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2947 if (calling_it_nexus
)
2950 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2951 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2952 __core_scsi3_free_registration(dev
, pr_reg
,
2953 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
:
2957 if (!calling_it_nexus
)
2958 core_scsi3_ua_allocate(pr_reg_nacl
,
2959 pr_res_mapped_lun
, 0x2A,
2960 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
2962 spin_unlock(&pr_tmpl
->registration_lock
);
2964 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2965 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2966 * RESERVATION KEY field to a value that does not match any
2967 * registered reservation key, then the device server shall
2968 * complete the command with RESERVATION CONFLICT status.
2970 if (!released_regs
) {
2971 spin_unlock(&dev
->dev_reservation_lock
);
2972 core_scsi3_put_pr_reg(pr_reg_n
);
2973 return TCM_RESERVATION_CONFLICT
;
2976 * For an existing all registrants type reservation
2977 * with a zero SA rservation key, preempt the existing
2978 * reservation with the new PR type and scope.
2980 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
2981 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
2982 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
2983 type
, scope
, preempt_type
);
2985 if (preempt_type
== PREEMPT_AND_ABORT
)
2986 core_scsi3_release_preempt_and_abort(
2987 &preempt_and_abort_list
, pr_reg_n
);
2989 spin_unlock(&dev
->dev_reservation_lock
);
2991 if (pr_tmpl
->pr_aptpl_active
) {
2992 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2993 pr_reg_n
->pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
2994 pr_debug("SPC-3 PR: Updated APTPL"
2995 " metadata for PREEMPT%s\n", (preempt_type
== PREEMPT_AND_ABORT
) ?
3000 core_scsi3_put_pr_reg(pr_reg_n
);
3001 core_scsi3_pr_generation(cmd
->se_dev
);
3005 * The PREEMPTing SA reservation key matches that of the
3006 * existing persistent reservation, first, we check if
3007 * we are preempting our own reservation.
3008 * From spc4r17, section 5.7.11.4.3 Preempting
3009 * persistent reservations and registration handling
3011 * If an all registrants persistent reservation is not
3012 * present, it is not an error for the persistent
3013 * reservation holder to preempt itself (i.e., a
3014 * PERSISTENT RESERVE OUT with a PREEMPT service action
3015 * or a PREEMPT AND ABORT service action with the
3016 * SERVICE ACTION RESERVATION KEY value equal to the
3017 * persistent reservation holder's reservation key that
3018 * is received from the persistent reservation holder).
3019 * In that case, the device server shall establish the
3020 * new persistent reservation and maintain the
3023 prh_type
= pr_res_holder
->pr_res_type
;
3024 prh_scope
= pr_res_holder
->pr_res_scope
;
3026 * If the SERVICE ACTION RESERVATION KEY field identifies a
3027 * persistent reservation holder (see 5.7.10), the device
3028 * server shall perform a preempt by doing the following as
3029 * an uninterrupted series of actions:
3031 * a) Release the persistent reservation for the holder
3032 * identified by the SERVICE ACTION RESERVATION KEY field;
3034 if (pr_reg_n
!= pr_res_holder
)
3035 __core_scsi3_complete_pro_release(dev
,
3036 pr_res_holder
->pr_reg_nacl
,
3037 dev
->dev_pr_res_holder
, 0);
3039 * b) Remove the registrations for all I_T nexuses identified
3040 * by the SERVICE ACTION RESERVATION KEY field, except the
3041 * I_T nexus that is being used for the PERSISTENT RESERVE
3042 * OUT command. If an all registrants persistent reservation
3043 * is present and the SERVICE ACTION RESERVATION KEY field
3044 * is set to zero, then all registrations shall be removed
3045 * except for that of the I_T nexus that is being used for
3046 * the PERSISTENT RESERVE OUT command;
3048 spin_lock(&pr_tmpl
->registration_lock
);
3049 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3050 &pr_tmpl
->registration_list
, pr_reg_list
) {
3052 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3053 if (calling_it_nexus
)
3056 if (pr_reg
->pr_res_key
!= sa_res_key
)
3059 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3060 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3061 __core_scsi3_free_registration(dev
, pr_reg
,
3062 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
3065 * e) Establish a unit attention condition for the initiator
3066 * port associated with every I_T nexus that lost its
3067 * persistent reservation and/or registration, with the
3068 * additional sense code set to REGISTRATIONS PREEMPTED;
3070 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3071 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3073 spin_unlock(&pr_tmpl
->registration_lock
);
3075 * c) Establish a persistent reservation for the preempting
3076 * I_T nexus using the contents of the SCOPE and TYPE fields;
3078 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3079 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
3080 type
, scope
, preempt_type
);
3082 * d) Process tasks as defined in 5.7.1;
3084 * f) If the type or scope has changed, then for every I_T nexus
3085 * whose reservation key was not removed, except for the I_T
3086 * nexus on which the PERSISTENT RESERVE OUT command was
3087 * received, the device server shall establish a unit
3088 * attention condition for the initiator port associated with
3089 * that I_T nexus, with the additional sense code set to
3090 * RESERVATIONS RELEASED. If the type or scope have not
3091 * changed, then no unit attention condition(s) shall be
3092 * established for this reason.
3094 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3095 spin_lock(&pr_tmpl
->registration_lock
);
3096 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3097 &pr_tmpl
->registration_list
, pr_reg_list
) {
3099 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3100 if (calling_it_nexus
)
3103 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3104 pr_reg
->pr_res_mapped_lun
, 0x2A,
3105 ASCQ_2AH_RESERVATIONS_RELEASED
);
3107 spin_unlock(&pr_tmpl
->registration_lock
);
3109 spin_unlock(&dev
->dev_reservation_lock
);
3111 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3112 * All received CDBs for the matching existing reservation and
3113 * registrations undergo ABORT_TASK logic.
3115 * From there, core_scsi3_release_preempt_and_abort() will
3116 * release every registration in the list (which have already
3117 * been removed from the primary pr_reg list), except the
3118 * new persistent reservation holder, the calling Initiator Port.
3120 if (preempt_type
== PREEMPT_AND_ABORT
) {
3121 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3122 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3126 if (pr_tmpl
->pr_aptpl_active
) {
3127 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3128 pr_reg_n
->pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
3129 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3130 "%s\n", (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "");
3134 core_scsi3_put_pr_reg(pr_reg_n
);
3135 core_scsi3_pr_generation(cmd
->se_dev
);
3139 static sense_reason_t
3140 core_scsi3_emulate_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
,
3141 u64 res_key
, u64 sa_res_key
, enum preempt_type preempt_type
)
3144 case PR_TYPE_WRITE_EXCLUSIVE
:
3145 case PR_TYPE_EXCLUSIVE_ACCESS
:
3146 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3147 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3148 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3149 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3150 return core_scsi3_pro_preempt(cmd
, type
, scope
, res_key
,
3151 sa_res_key
, preempt_type
);
3153 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3154 " Type: 0x%02x\n", (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "", type
);
3155 return TCM_INVALID_CDB_FIELD
;
3160 static sense_reason_t
3161 core_scsi3_emulate_pro_register_and_move(struct se_cmd
*cmd
, u64 res_key
,
3162 u64 sa_res_key
, int aptpl
, int unreg
)
3164 struct se_session
*se_sess
= cmd
->se_sess
;
3165 struct se_device
*dev
= cmd
->se_dev
;
3166 struct se_dev_entry
*dest_se_deve
= NULL
;
3167 struct se_lun
*se_lun
= cmd
->se_lun
;
3168 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3169 struct se_port
*se_port
;
3170 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3171 struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3172 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3173 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3175 unsigned char *initiator_str
;
3176 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
3177 u32 tid_len
, tmp_tid_len
;
3178 int new_reg
= 0, type
, scope
, matching_iname
;
3180 unsigned short rtpi
;
3181 unsigned char proto_ident
;
3183 if (!se_sess
|| !se_lun
) {
3184 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3185 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3188 memset(dest_iport
, 0, 64);
3189 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3190 se_tpg
= se_sess
->se_tpg
;
3191 tf_ops
= se_tpg
->se_tpg_tfo
;
3193 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3194 * Register behaviors for a REGISTER AND MOVE service action
3196 * Locate the existing *pr_reg via struct se_node_acl pointers
3198 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3201 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3202 " *pr_reg for REGISTER_AND_MOVE\n");
3203 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3206 * The provided reservation key much match the existing reservation key
3207 * provided during this initiator's I_T nexus registration.
3209 if (res_key
!= pr_reg
->pr_res_key
) {
3210 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3211 " res_key: 0x%016Lx does not match existing SA REGISTER"
3212 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3213 ret
= TCM_RESERVATION_CONFLICT
;
3214 goto out_put_pr_reg
;
3217 * The service active reservation key needs to be non zero
3220 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3222 ret
= TCM_INVALID_PARAMETER_LIST
;
3223 goto out_put_pr_reg
;
3227 * Determine the Relative Target Port Identifier where the reservation
3228 * will be moved to for the TransportID containing SCSI initiator WWN
3231 buf
= transport_kmap_data_sg(cmd
);
3233 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3234 goto out_put_pr_reg
;
3237 rtpi
= (buf
[18] & 0xff) << 8;
3238 rtpi
|= buf
[19] & 0xff;
3239 tid_len
= (buf
[20] & 0xff) << 24;
3240 tid_len
|= (buf
[21] & 0xff) << 16;
3241 tid_len
|= (buf
[22] & 0xff) << 8;
3242 tid_len
|= buf
[23] & 0xff;
3243 transport_kunmap_data_sg(cmd
);
3246 if ((tid_len
+ 24) != cmd
->data_length
) {
3247 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3248 " does not equal CDB data_length: %u\n", tid_len
,
3250 ret
= TCM_INVALID_PARAMETER_LIST
;
3251 goto out_put_pr_reg
;
3254 spin_lock(&dev
->se_port_lock
);
3255 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3256 if (se_port
->sep_rtpi
!= rtpi
)
3258 dest_se_tpg
= se_port
->sep_tpg
;
3261 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3265 atomic_inc(&dest_se_tpg
->tpg_pr_ref_count
);
3266 smp_mb__after_atomic_inc();
3267 spin_unlock(&dev
->se_port_lock
);
3269 if (core_scsi3_tpg_depend_item(dest_se_tpg
)) {
3270 pr_err("core_scsi3_tpg_depend_item() failed"
3271 " for dest_se_tpg\n");
3272 atomic_dec(&dest_se_tpg
->tpg_pr_ref_count
);
3273 smp_mb__after_atomic_dec();
3274 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3275 goto out_put_pr_reg
;
3278 spin_lock(&dev
->se_port_lock
);
3281 spin_unlock(&dev
->se_port_lock
);
3283 if (!dest_se_tpg
|| !dest_tf_ops
) {
3284 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3285 " fabric ops from Relative Target Port Identifier:"
3287 ret
= TCM_INVALID_PARAMETER_LIST
;
3288 goto out_put_pr_reg
;
3291 buf
= transport_kmap_data_sg(cmd
);
3293 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3294 goto out_put_pr_reg
;
3296 proto_ident
= (buf
[24] & 0x0f);
3298 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3299 " 0x%02x\n", proto_ident
);
3301 if (proto_ident
!= dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
)) {
3302 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3303 " proto_ident: 0x%02x does not match ident: 0x%02x"
3304 " from fabric: %s\n", proto_ident
,
3305 dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
),
3306 dest_tf_ops
->get_fabric_name());
3307 ret
= TCM_INVALID_PARAMETER_LIST
;
3310 if (dest_tf_ops
->tpg_parse_pr_out_transport_id
== NULL
) {
3311 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3312 " containg a valid tpg_parse_pr_out_transport_id"
3313 " function pointer\n");
3314 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3317 initiator_str
= dest_tf_ops
->tpg_parse_pr_out_transport_id(dest_se_tpg
,
3318 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3319 if (!initiator_str
) {
3320 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3321 " initiator_str from Transport ID\n");
3322 ret
= TCM_INVALID_PARAMETER_LIST
;
3326 transport_kunmap_data_sg(cmd
);
3329 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3330 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3331 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3334 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3335 * action specifies a TransportID that is the same as the initiator port
3336 * of the I_T nexus for the command received, then the command shall
3337 * be terminated with CHECK CONDITION status, with the sense key set to
3338 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3339 * IN PARAMETER LIST.
3341 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3342 matching_iname
= (!strcmp(initiator_str
,
3343 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3344 if (!matching_iname
)
3345 goto after_iport_check
;
3347 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3348 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3349 " matches: %s on received I_T Nexus\n", initiator_str
,
3350 pr_reg_nacl
->initiatorname
);
3351 ret
= TCM_INVALID_PARAMETER_LIST
;
3354 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3355 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3356 " matches: %s %s on received I_T Nexus\n",
3357 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3358 pr_reg
->pr_reg_isid
);
3359 ret
= TCM_INVALID_PARAMETER_LIST
;
3364 * Locate the destination struct se_node_acl from the received Transport ID
3366 spin_lock_irq(&dest_se_tpg
->acl_node_lock
);
3367 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3369 if (dest_node_acl
) {
3370 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
3371 smp_mb__after_atomic_inc();
3373 spin_unlock_irq(&dest_se_tpg
->acl_node_lock
);
3375 if (!dest_node_acl
) {
3376 pr_err("Unable to locate %s dest_node_acl for"
3377 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3379 ret
= TCM_INVALID_PARAMETER_LIST
;
3383 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
3384 pr_err("core_scsi3_nodeacl_depend_item() for"
3385 " dest_node_acl\n");
3386 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
3387 smp_mb__after_atomic_dec();
3388 dest_node_acl
= NULL
;
3389 ret
= TCM_INVALID_PARAMETER_LIST
;
3393 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3394 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3395 dest_node_acl
->initiatorname
);
3398 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3401 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3402 if (!dest_se_deve
) {
3403 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3404 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3405 ret
= TCM_INVALID_PARAMETER_LIST
;
3409 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
3410 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3411 atomic_dec(&dest_se_deve
->pr_ref_count
);
3412 smp_mb__after_atomic_dec();
3413 dest_se_deve
= NULL
;
3414 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3418 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3419 " ACL for dest_se_deve->mapped_lun: %u\n",
3420 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3421 dest_se_deve
->mapped_lun
);
3424 * A persistent reservation needs to already existing in order to
3425 * successfully complete the REGISTER_AND_MOVE service action..
3427 spin_lock(&dev
->dev_reservation_lock
);
3428 pr_res_holder
= dev
->dev_pr_res_holder
;
3429 if (!pr_res_holder
) {
3430 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3431 " currently held\n");
3432 spin_unlock(&dev
->dev_reservation_lock
);
3433 ret
= TCM_INVALID_CDB_FIELD
;
3437 * The received on I_T Nexus must be the reservation holder.
3439 * From spc4r17 section 5.7.8 Table 50 --
3440 * Register behaviors for a REGISTER AND MOVE service action
3442 if (pr_res_holder
!= pr_reg
) {
3443 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3444 " Nexus is not reservation holder\n");
3445 spin_unlock(&dev
->dev_reservation_lock
);
3446 ret
= TCM_RESERVATION_CONFLICT
;
3450 * From spc4r17 section 5.7.8: registering and moving reservation
3452 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3453 * action is received and the established persistent reservation is a
3454 * Write Exclusive - All Registrants type or Exclusive Access -
3455 * All Registrants type reservation, then the command shall be completed
3456 * with RESERVATION CONFLICT status.
3458 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3459 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3460 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3461 " reservation for type: %s\n",
3462 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3463 spin_unlock(&dev
->dev_reservation_lock
);
3464 ret
= TCM_RESERVATION_CONFLICT
;
3467 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3469 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3471 type
= pr_res_holder
->pr_res_type
;
3472 scope
= pr_res_holder
->pr_res_type
;
3474 * c) Associate the reservation key specified in the SERVICE ACTION
3475 * RESERVATION KEY field with the I_T nexus specified as the
3476 * destination of the register and move, where:
3477 * A) The I_T nexus is specified by the TransportID and the
3478 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3479 * B) Regardless of the TransportID format used, the association for
3480 * the initiator port is based on either the initiator port name
3481 * (see 3.1.71) on SCSI transport protocols where port names are
3482 * required or the initiator port identifier (see 3.1.70) on SCSI
3483 * transport protocols where port names are not required;
3484 * d) Register the reservation key specified in the SERVICE ACTION
3485 * RESERVATION KEY field;
3486 * e) Retain the reservation key specified in the SERVICE ACTION
3487 * RESERVATION KEY field and associated information;
3489 * Also, It is not an error for a REGISTER AND MOVE service action to
3490 * register an I_T nexus that is already registered with the same
3491 * reservation key or a different reservation key.
3493 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3496 if (core_scsi3_alloc_registration(cmd
->se_dev
,
3497 dest_node_acl
, dest_se_deve
, iport_ptr
,
3498 sa_res_key
, 0, aptpl
, 2, 1)) {
3499 spin_unlock(&dev
->dev_reservation_lock
);
3500 ret
= TCM_INVALID_PARAMETER_LIST
;
3503 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3508 * f) Release the persistent reservation for the persistent reservation
3509 * holder (i.e., the I_T nexus on which the
3511 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3512 dev
->dev_pr_res_holder
, 0);
3514 * g) Move the persistent reservation to the specified I_T nexus using
3515 * the same scope and type as the persistent reservation released in
3518 dev
->dev_pr_res_holder
= dest_pr_reg
;
3519 dest_pr_reg
->pr_res_holder
= 1;
3520 dest_pr_reg
->pr_res_type
= type
;
3521 pr_reg
->pr_res_scope
= scope
;
3522 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
3524 * Increment PRGeneration for existing registrations..
3527 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3528 spin_unlock(&dev
->dev_reservation_lock
);
3530 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3531 " created new reservation holder TYPE: %s on object RTPI:"
3532 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3533 core_scsi3_pr_dump_type(type
), rtpi
,
3534 dest_pr_reg
->pr_res_generation
);
3535 pr_debug("SPC-3 PR Successfully moved reservation from"
3536 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3537 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3538 i_buf
, dest_tf_ops
->get_fabric_name(),
3539 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3542 * It is now safe to release configfs group dependencies for destination
3543 * of Transport ID Initiator Device/Port Identifier
3545 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3546 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3547 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3549 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3550 * nexus on which PERSISTENT RESERVE OUT command was received.
3553 spin_lock(&pr_tmpl
->registration_lock
);
3554 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3555 spin_unlock(&pr_tmpl
->registration_lock
);
3557 core_scsi3_put_pr_reg(pr_reg
);
3560 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3561 * write out the updated metadata to struct file for this SCSI device.
3564 pr_tmpl
->pr_aptpl_active
= 0;
3565 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
3566 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3567 " REGISTER_AND_MOVE\n");
3569 pr_tmpl
->pr_aptpl_active
= 1;
3570 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3571 dest_pr_reg
->pr_aptpl_buf
, PR_APTPL_BUF_LEN
)) {
3572 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3573 " REGISTER_AND_MOVE\n");
3577 transport_kunmap_data_sg(cmd
);
3579 core_scsi3_put_pr_reg(dest_pr_reg
);
3583 transport_kunmap_data_sg(cmd
);
3585 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3587 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3588 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3591 core_scsi3_put_pr_reg(pr_reg
);
3595 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3597 unsigned int __v1
, __v2
;
3599 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3600 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3602 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3606 * See spc4r17 section 6.14 Table 170
3609 target_scsi3_emulate_pr_out(struct se_cmd
*cmd
)
3611 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3613 u64 res_key
, sa_res_key
;
3614 int sa
, scope
, type
, aptpl
;
3615 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3619 * Following spc2r20 5.5.1 Reservations overview:
3621 * If a logical unit has been reserved by any RESERVE command and is
3622 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3623 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3624 * initiator or service action and shall terminate with a RESERVATION
3627 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
3628 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3629 " SPC-2 reservation is held, returning"
3630 " RESERVATION_CONFLICT\n");
3631 return TCM_RESERVATION_CONFLICT
;
3635 * FIXME: A NULL struct se_session pointer means an this is not coming from
3636 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3639 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3641 if (cmd
->data_length
< 24) {
3642 pr_warn("SPC-PR: Received PR OUT parameter list"
3643 " length too small: %u\n", cmd
->data_length
);
3644 return TCM_INVALID_PARAMETER_LIST
;
3648 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3650 sa
= (cdb
[1] & 0x1f);
3651 scope
= (cdb
[2] & 0xf0);
3652 type
= (cdb
[2] & 0x0f);
3654 buf
= transport_kmap_data_sg(cmd
);
3656 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3659 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3661 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3662 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3664 * REGISTER_AND_MOVE uses a different SA parameter list containing
3665 * SCSI TransportIDs.
3667 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3668 spec_i_pt
= (buf
[20] & 0x08);
3669 all_tg_pt
= (buf
[20] & 0x04);
3670 aptpl
= (buf
[20] & 0x01);
3672 aptpl
= (buf
[17] & 0x01);
3673 unreg
= (buf
[17] & 0x02);
3675 transport_kunmap_data_sg(cmd
);
3679 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3681 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
))
3682 return TCM_INVALID_PARAMETER_LIST
;
3685 * From spc4r17 section 6.14:
3687 * If the SPEC_I_PT bit is set to zero, the service action is not
3688 * REGISTER AND MOVE, and the parameter list length is not 24, then
3689 * the command shall be terminated with CHECK CONDITION status, with
3690 * the sense key set to ILLEGAL REQUEST, and the additional sense
3691 * code set to PARAMETER LIST LENGTH ERROR.
3693 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3694 (cmd
->data_length
!= 24)) {
3695 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3696 " list length: %u\n", cmd
->data_length
);
3697 return TCM_INVALID_PARAMETER_LIST
;
3701 * (core_scsi3_emulate_pro_* function parameters
3702 * are defined by spc4r17 Table 174:
3703 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3707 ret
= core_scsi3_emulate_pro_register(cmd
,
3708 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, REGISTER
);
3711 ret
= core_scsi3_emulate_pro_reserve(cmd
, type
, scope
, res_key
);
3714 ret
= core_scsi3_emulate_pro_release(cmd
, type
, scope
, res_key
);
3717 ret
= core_scsi3_emulate_pro_clear(cmd
, res_key
);
3720 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3721 res_key
, sa_res_key
, PREEMPT
);
3723 case PRO_PREEMPT_AND_ABORT
:
3724 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3725 res_key
, sa_res_key
, PREEMPT_AND_ABORT
);
3727 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3728 ret
= core_scsi3_emulate_pro_register(cmd
,
3729 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, REGISTER_AND_IGNORE_EXISTING_KEY
);
3731 case PRO_REGISTER_AND_MOVE
:
3732 ret
= core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3733 sa_res_key
, aptpl
, unreg
);
3736 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3737 " action: 0x%02x\n", cdb
[1] & 0x1f);
3738 return TCM_INVALID_CDB_FIELD
;
3742 target_complete_cmd(cmd
, GOOD
);
3747 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3749 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3751 static sense_reason_t
3752 core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3754 struct se_device
*dev
= cmd
->se_dev
;
3755 struct t10_pr_registration
*pr_reg
;
3757 u32 add_len
= 0, off
= 8;
3759 if (cmd
->data_length
< 8) {
3760 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3761 " too small\n", cmd
->data_length
);
3762 return TCM_INVALID_CDB_FIELD
;
3765 buf
= transport_kmap_data_sg(cmd
);
3767 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3769 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3770 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3771 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3772 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3774 spin_lock(&dev
->t10_pr
.registration_lock
);
3775 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
3778 * Check for overflow of 8byte PRI READ_KEYS payload and
3779 * next reservation key list descriptor.
3781 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3784 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3785 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3786 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3787 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3788 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3789 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3790 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3791 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3795 spin_unlock(&dev
->t10_pr
.registration_lock
);
3797 buf
[4] = ((add_len
>> 24) & 0xff);
3798 buf
[5] = ((add_len
>> 16) & 0xff);
3799 buf
[6] = ((add_len
>> 8) & 0xff);
3800 buf
[7] = (add_len
& 0xff);
3802 transport_kunmap_data_sg(cmd
);
3808 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3810 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3812 static sense_reason_t
3813 core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
3815 struct se_device
*dev
= cmd
->se_dev
;
3816 struct t10_pr_registration
*pr_reg
;
3819 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
3821 if (cmd
->data_length
< 8) {
3822 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3823 " too small\n", cmd
->data_length
);
3824 return TCM_INVALID_CDB_FIELD
;
3827 buf
= transport_kmap_data_sg(cmd
);
3829 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3831 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3832 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3833 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3834 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3836 spin_lock(&dev
->dev_reservation_lock
);
3837 pr_reg
= dev
->dev_pr_res_holder
;
3840 * Set the hardcoded Additional Length
3842 buf
[4] = ((add_len
>> 24) & 0xff);
3843 buf
[5] = ((add_len
>> 16) & 0xff);
3844 buf
[6] = ((add_len
>> 8) & 0xff);
3845 buf
[7] = (add_len
& 0xff);
3847 if (cmd
->data_length
< 22)
3851 * Set the Reservation key.
3853 * From spc4r17, section 5.7.10:
3854 * A persistent reservation holder has its reservation key
3855 * returned in the parameter data from a PERSISTENT
3856 * RESERVE IN command with READ RESERVATION service action as
3858 * a) For a persistent reservation of the type Write Exclusive
3859 * - All Registrants or Exclusive Access  All Regitrants,
3860 * the reservation key shall be set to zero; or
3861 * b) For all other persistent reservation types, the
3862 * reservation key shall be set to the registered
3863 * reservation key for the I_T nexus that holds the
3864 * persistent reservation.
3866 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3867 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
3870 pr_res_key
= pr_reg
->pr_res_key
;
3872 buf
[8] = ((pr_res_key
>> 56) & 0xff);
3873 buf
[9] = ((pr_res_key
>> 48) & 0xff);
3874 buf
[10] = ((pr_res_key
>> 40) & 0xff);
3875 buf
[11] = ((pr_res_key
>> 32) & 0xff);
3876 buf
[12] = ((pr_res_key
>> 24) & 0xff);
3877 buf
[13] = ((pr_res_key
>> 16) & 0xff);
3878 buf
[14] = ((pr_res_key
>> 8) & 0xff);
3879 buf
[15] = (pr_res_key
& 0xff);
3881 * Set the SCOPE and TYPE
3883 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
3884 (pr_reg
->pr_res_type
& 0x0f);
3888 spin_unlock(&dev
->dev_reservation_lock
);
3889 transport_kunmap_data_sg(cmd
);
3895 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3897 * See spc4r17 section 6.13.4 Table 165
3899 static sense_reason_t
3900 core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
3902 struct se_device
*dev
= cmd
->se_dev
;
3903 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3905 u16 add_len
= 8; /* Hardcoded to 8. */
3907 if (cmd
->data_length
< 6) {
3908 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3909 " %u too small\n", cmd
->data_length
);
3910 return TCM_INVALID_CDB_FIELD
;
3913 buf
= transport_kmap_data_sg(cmd
);
3915 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3917 buf
[0] = ((add_len
<< 8) & 0xff);
3918 buf
[1] = (add_len
& 0xff);
3919 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3920 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3921 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3922 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3924 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3925 * set the TMV: Task Mask Valid bit.
3929 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3931 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3933 * PTPL_A: Persistence across Target Power Loss Active bit
3935 if (pr_tmpl
->pr_aptpl_active
)
3938 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3940 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3941 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3942 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3943 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3944 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3945 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3947 transport_kunmap_data_sg(cmd
);
3953 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3955 * See spc4r17 section 6.13.5 Table 168 and 169
3957 static sense_reason_t
3958 core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
3960 struct se_device
*dev
= cmd
->se_dev
;
3961 struct se_node_acl
*se_nacl
;
3962 struct se_portal_group
*se_tpg
;
3963 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
3964 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3966 u32 add_desc_len
= 0, add_len
= 0, desc_len
, exp_desc_len
;
3967 u32 off
= 8; /* off into first Full Status descriptor */
3968 int format_code
= 0;
3970 if (cmd
->data_length
< 8) {
3971 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3972 " too small\n", cmd
->data_length
);
3973 return TCM_INVALID_CDB_FIELD
;
3976 buf
= transport_kmap_data_sg(cmd
);
3978 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3980 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3981 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3982 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3983 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3985 spin_lock(&pr_tmpl
->registration_lock
);
3986 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3987 &pr_tmpl
->registration_list
, pr_reg_list
) {
3989 se_nacl
= pr_reg
->pr_reg_nacl
;
3990 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
3993 atomic_inc(&pr_reg
->pr_res_holders
);
3994 smp_mb__after_atomic_inc();
3995 spin_unlock(&pr_tmpl
->registration_lock
);
3997 * Determine expected length of $FABRIC_MOD specific
3998 * TransportID full status descriptor..
4000 exp_desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id_len(
4001 se_tpg
, se_nacl
, pr_reg
, &format_code
);
4003 if ((exp_desc_len
+ add_len
) > cmd
->data_length
) {
4004 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4005 " out of buffer: %d\n", cmd
->data_length
);
4006 spin_lock(&pr_tmpl
->registration_lock
);
4007 atomic_dec(&pr_reg
->pr_res_holders
);
4008 smp_mb__after_atomic_dec();
4012 * Set RESERVATION KEY
4014 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
4015 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
4016 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
4017 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4018 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4019 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4020 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4021 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4022 off
+= 4; /* Skip Over Reserved area */
4025 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4027 if (pr_reg
->pr_reg_all_tg_pt
)
4030 * The struct se_lun pointer will be present for the
4031 * reservation holder for PR_HOLDER bit.
4033 * Also, if this registration is the reservation
4034 * holder, fill in SCOPE and TYPE in the next byte.
4036 if (pr_reg
->pr_res_holder
) {
4038 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4039 (pr_reg
->pr_res_type
& 0x0f);
4043 off
+= 4; /* Skip over reserved area */
4045 * From spc4r17 6.3.15:
4047 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4048 * IDENTIFIER field contains the relative port identifier (see
4049 * 3.1.120) of the target port that is part of the I_T nexus
4050 * described by this full status descriptor. If the ALL_TG_PT
4051 * bit is set to one, the contents of the RELATIVE TARGET PORT
4052 * IDENTIFIER field are not defined by this standard.
4054 if (!pr_reg
->pr_reg_all_tg_pt
) {
4055 struct se_port
*port
= pr_reg
->pr_reg_tg_pt_lun
->lun_sep
;
4057 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
4058 buf
[off
++] = (port
->sep_rtpi
& 0xff);
4060 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4063 * Now, have the $FABRIC_MOD fill in the protocol identifier
4065 desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id(se_tpg
,
4066 se_nacl
, pr_reg
, &format_code
, &buf
[off
+4]);
4068 spin_lock(&pr_tmpl
->registration_lock
);
4069 atomic_dec(&pr_reg
->pr_res_holders
);
4070 smp_mb__after_atomic_dec();
4072 * Set the ADDITIONAL DESCRIPTOR LENGTH
4074 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4075 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4076 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4077 buf
[off
++] = (desc_len
& 0xff);
4079 * Size of full desctipor header minus TransportID
4080 * containing $FABRIC_MOD specific) initiator device/port
4083 * See spc4r17 Section 6.13.5 Table 169
4085 add_desc_len
= (24 + desc_len
);
4088 add_len
+= add_desc_len
;
4090 spin_unlock(&pr_tmpl
->registration_lock
);
4092 * Set ADDITIONAL_LENGTH
4094 buf
[4] = ((add_len
>> 24) & 0xff);
4095 buf
[5] = ((add_len
>> 16) & 0xff);
4096 buf
[6] = ((add_len
>> 8) & 0xff);
4097 buf
[7] = (add_len
& 0xff);
4099 transport_kunmap_data_sg(cmd
);
4105 target_scsi3_emulate_pr_in(struct se_cmd
*cmd
)
4110 * Following spc2r20 5.5.1 Reservations overview:
4112 * If a logical unit has been reserved by any RESERVE command and is
4113 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4114 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4115 * initiator or service action and shall terminate with a RESERVATION
4118 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
4119 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4120 " SPC-2 reservation is held, returning"
4121 " RESERVATION_CONFLICT\n");
4122 return TCM_RESERVATION_CONFLICT
;
4125 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4127 ret
= core_scsi3_pri_read_keys(cmd
);
4129 case PRI_READ_RESERVATION
:
4130 ret
= core_scsi3_pri_read_reservation(cmd
);
4132 case PRI_REPORT_CAPABILITIES
:
4133 ret
= core_scsi3_pri_report_capabilities(cmd
);
4135 case PRI_READ_FULL_STATUS
:
4136 ret
= core_scsi3_pri_read_full_status(cmd
);
4139 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4140 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4141 return TCM_INVALID_CDB_FIELD
;
4145 target_complete_cmd(cmd
, GOOD
);
4150 target_check_reservation(struct se_cmd
*cmd
)
4152 struct se_device
*dev
= cmd
->se_dev
;
4157 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
4159 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
4162 spin_lock(&dev
->dev_reservation_lock
);
4163 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
4164 ret
= target_scsi2_reservation_check(cmd
);
4166 ret
= target_scsi3_pr_reservation_check(cmd
);
4167 spin_unlock(&dev
->dev_reservation_lock
);