1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@kernel.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ******************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_device.h>
37 #include <target/target_core_tmr.h>
38 #include <target/target_core_tpg.h>
39 #include <target/target_core_transport.h>
40 #include <target/target_core_fabric_ops.h>
41 #include <target/target_core_configfs.h>
43 #include "target_core_hba.h"
44 #include "target_core_pr.h"
45 #include "target_core_ua.h"
48 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
50 struct pr_transport_id_holder
{
52 struct t10_pr_registration
*dest_pr_reg
;
53 struct se_portal_group
*dest_tpg
;
54 struct se_node_acl
*dest_node_acl
;
55 struct se_dev_entry
*dest_se_deve
;
56 struct list_head dest_list
;
59 int core_pr_dump_initiator_port(
60 struct t10_pr_registration
*pr_reg
,
64 if (!pr_reg
->isid_present_at_reg
)
67 snprintf(buf
, size
, ",i,0x%s", &pr_reg
->pr_reg_isid
[0]);
71 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
72 struct t10_pr_registration
*, int);
74 static int core_scsi2_reservation_seq_non_holder(
91 static int core_scsi2_reservation_check(struct se_cmd
*cmd
, u32
*pr_reg_type
)
93 struct se_device
*dev
= cmd
->se_dev
;
94 struct se_session
*sess
= cmd
->se_sess
;
100 spin_lock(&dev
->dev_reservation_lock
);
101 if (!dev
->dev_reserved_node_acl
|| !sess
) {
102 spin_unlock(&dev
->dev_reservation_lock
);
105 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
) {
106 spin_unlock(&dev
->dev_reservation_lock
);
109 if (!(dev
->dev_flags
& DF_SPC2_RESERVATIONS_WITH_ISID
)) {
110 spin_unlock(&dev
->dev_reservation_lock
);
113 ret
= (dev
->dev_res_bin_isid
== sess
->sess_bin_isid
) ? 0 : -EINVAL
;
114 spin_unlock(&dev
->dev_reservation_lock
);
119 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
120 struct se_node_acl
*, struct se_session
*);
121 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
123 static int target_check_scsi2_reservation_conflict(struct se_cmd
*cmd
, int *ret
)
125 struct se_session
*se_sess
= cmd
->se_sess
;
126 struct se_subsystem_dev
*su_dev
= cmd
->se_dev
->se_sub_dev
;
127 struct t10_pr_registration
*pr_reg
;
128 struct t10_reservation
*pr_tmpl
= &su_dev
->t10_pr
;
129 int crh
= (su_dev
->t10_pr
.res_type
== SPC3_PERSISTENT_RESERVATIONS
);
135 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
139 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
142 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
143 * status, but no reservation shall be established and the
144 * persistent reservation shall not be changed, if the command
145 * is received from a) and b) below.
147 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
148 * status, but the persistent reservation shall not be released,
149 * if the command is received from a) and b)
151 * a) An I_T nexus that is a persistent reservation holder; or
152 * b) An I_T nexus that is registered if a registrants only or
153 * all registrants type persistent reservation is present.
155 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
156 * RELEASE(6) command, or RELEASE(10) command shall be processed
157 * as defined in SPC-2.
159 if (pr_reg
->pr_res_holder
) {
160 core_scsi3_put_pr_reg(pr_reg
);
164 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
165 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
166 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
167 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
168 core_scsi3_put_pr_reg(pr_reg
);
172 core_scsi3_put_pr_reg(pr_reg
);
176 * Following spc2r20 5.5.1 Reservations overview:
178 * If a logical unit has executed a PERSISTENT RESERVE OUT
179 * command with the REGISTER or the REGISTER AND IGNORE
180 * EXISTING KEY service action and is still registered by any
181 * initiator, all RESERVE commands and all RELEASE commands
182 * regardless of initiator shall conflict and shall terminate
183 * with a RESERVATION CONFLICT status.
185 spin_lock(&pr_tmpl
->registration_lock
);
186 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
187 spin_unlock(&pr_tmpl
->registration_lock
);
191 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
192 " while active SPC-3 registrations exist,"
193 " returning RESERVATION_CONFLICT\n");
194 *ret
= PYX_TRANSPORT_RESERVATION_CONFLICT
;
201 int target_scsi2_reservation_release(struct se_task
*task
)
203 struct se_cmd
*cmd
= task
->task_se_cmd
;
204 struct se_device
*dev
= cmd
->se_dev
;
205 struct se_session
*sess
= cmd
->se_sess
;
206 struct se_portal_group
*tpg
= sess
->se_tpg
;
211 if (target_check_scsi2_reservation_conflict(cmd
, &ret
))
214 spin_lock(&dev
->dev_reservation_lock
);
215 if (!dev
->dev_reserved_node_acl
|| !sess
) {
216 spin_unlock(&dev
->dev_reservation_lock
);
220 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
) {
221 spin_unlock(&dev
->dev_reservation_lock
);
224 dev
->dev_reserved_node_acl
= NULL
;
225 dev
->dev_flags
&= ~DF_SPC2_RESERVATIONS
;
226 if (dev
->dev_flags
& DF_SPC2_RESERVATIONS_WITH_ISID
) {
227 dev
->dev_res_bin_isid
= 0;
228 dev
->dev_flags
&= ~DF_SPC2_RESERVATIONS_WITH_ISID
;
230 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
231 " MAPPED LUN: %u for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
232 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
233 sess
->se_node_acl
->initiatorname
);
234 spin_unlock(&dev
->dev_reservation_lock
);
239 int target_scsi2_reservation_reserve(struct se_task
*task
)
241 struct se_cmd
*cmd
= task
->task_se_cmd
;
242 struct se_device
*dev
= cmd
->se_dev
;
243 struct se_session
*sess
= cmd
->se_sess
;
244 struct se_portal_group
*tpg
= sess
->se_tpg
;
247 if ((cmd
->t_task_cdb
[1] & 0x01) &&
248 (cmd
->t_task_cdb
[1] & 0x02)) {
249 pr_err("LongIO and Obselete Bits set, returning"
250 " ILLEGAL_REQUEST\n");
251 return PYX_TRANSPORT_ILLEGAL_REQUEST
;
254 * This is currently the case for target_core_mod passthrough struct se_cmd
259 if (target_check_scsi2_reservation_conflict(cmd
, &ret
))
262 spin_lock(&dev
->dev_reservation_lock
);
263 if (dev
->dev_reserved_node_acl
&&
264 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
265 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
266 tpg
->se_tpg_tfo
->get_fabric_name());
267 pr_err("Original reserver LUN: %u %s\n",
268 cmd
->se_lun
->unpacked_lun
,
269 dev
->dev_reserved_node_acl
->initiatorname
);
270 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
271 " from %s \n", cmd
->se_lun
->unpacked_lun
,
272 cmd
->se_deve
->mapped_lun
,
273 sess
->se_node_acl
->initiatorname
);
274 spin_unlock(&dev
->dev_reservation_lock
);
275 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
278 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
279 dev
->dev_flags
|= DF_SPC2_RESERVATIONS
;
280 if (sess
->sess_bin_isid
!= 0) {
281 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
282 dev
->dev_flags
|= DF_SPC2_RESERVATIONS_WITH_ISID
;
284 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
285 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
286 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
287 sess
->se_node_acl
->initiatorname
);
288 spin_unlock(&dev
->dev_reservation_lock
);
295 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
297 * This function is called by those initiator ports who are *NOT*
298 * the active PR reservation holder when a reservation is present.
300 static int core_scsi3_pr_seq_non_holder(
305 struct se_dev_entry
*se_deve
;
306 struct se_session
*se_sess
= cmd
->se_sess
;
307 int other_cdb
= 0, ignore_reg
;
308 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
309 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
310 int we
= 0; /* Write Exclusive */
311 int legacy
= 0; /* Act like a legacy device and return
312 * RESERVATION CONFLICT on some CDBs */
314 * A legacy SPC-2 reservation is being held.
316 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
)
317 return core_scsi2_reservation_seq_non_holder(cmd
,
320 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
322 * Determine if the registration should be ignored due to
323 * non-matching ISIDs in core_scsi3_pr_reservation_check().
325 ignore_reg
= (pr_reg_type
& 0x80000000);
327 pr_reg_type
&= ~0x80000000;
329 switch (pr_reg_type
) {
330 case PR_TYPE_WRITE_EXCLUSIVE
:
332 case PR_TYPE_EXCLUSIVE_ACCESS
:
334 * Some commands are only allowed for the persistent reservation
337 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
338 registered_nexus
= 1;
340 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
342 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
344 * Some commands are only allowed for registered I_T Nexuses.
347 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
348 registered_nexus
= 1;
350 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
352 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
354 * Each registered I_T Nexus is a reservation holder.
357 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
358 registered_nexus
= 1;
364 * Referenced from spc4r17 table 45 for *NON* PR holder access
367 case SECURITY_PROTOCOL_IN
:
368 if (registered_nexus
)
376 case RECEIVE_DIAGNOSTIC
:
381 if (registered_nexus
) {
385 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
387 case PERSISTENT_RESERVE_OUT
:
389 * This follows PERSISTENT_RESERVE_OUT service actions that
390 * are allowed in the presence of various reservations.
391 * See spc4r17, table 46
393 switch (cdb
[1] & 0x1f) {
396 case PRO_PREEMPT_AND_ABORT
:
397 ret
= (registered_nexus
) ? 0 : 1;
400 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
403 case PRO_REGISTER_AND_MOVE
:
408 ret
= (registered_nexus
) ? 0 : 1;
411 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
412 " action: 0x%02x\n", cdb
[1] & 0x1f);
418 /* Handled by CRH=1 in target_scsi2_reservation_release() */
423 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
426 case TEST_UNIT_READY
:
427 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
430 switch (cdb
[1] & 0x1f) {
431 case MI_MANAGEMENT_PROTOCOL_IN
:
432 if (registered_nexus
) {
436 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
438 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
439 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
444 if (registered_nexus
) {
448 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
450 case MI_REPORT_ALIASES
:
451 case MI_REPORT_IDENTIFYING_INFORMATION
:
452 case MI_REPORT_PRIORITY
:
453 case MI_REPORT_TARGET_PGS
:
454 case MI_REPORT_TIMESTAMP
:
455 ret
= 0; /* Allowed */
458 pr_err("Unknown MI Service Action: 0x%02x\n",
463 case ACCESS_CONTROL_IN
:
464 case ACCESS_CONTROL_OUT
:
467 case READ_MEDIA_SERIAL_NUMBER
:
470 ret
= 0; /*/ Allowed CDBs */
477 * Case where the CDB is explicitly allowed in the above switch
480 if (!ret
&& !other_cdb
) {
482 pr_debug("Allowing explict CDB: 0x%02x for %s"
483 " reservation holder\n", cdb
[0],
484 core_scsi3_pr_dump_type(pr_reg_type
));
489 * Check if write exclusive initiator ports *NOT* holding the
490 * WRITE_EXCLUSIVE_* reservation.
492 if ((we
) && !(registered_nexus
)) {
493 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
495 * Conflict for write exclusive
497 pr_debug("%s Conflict for unregistered nexus"
498 " %s CDB: 0x%02x to %s reservation\n",
499 transport_dump_cmd_direction(cmd
),
500 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
501 core_scsi3_pr_dump_type(pr_reg_type
));
505 * Allow non WRITE CDBs for all Write Exclusive
506 * PR TYPEs to pass for registered and
507 * non-registered_nexuxes NOT holding the reservation.
509 * We only make noise for the unregisterd nexuses,
510 * as we expect registered non-reservation holding
511 * nexuses to issue CDBs.
514 if (!registered_nexus
) {
515 pr_debug("Allowing implict CDB: 0x%02x"
516 " for %s reservation on unregistered"
518 core_scsi3_pr_dump_type(pr_reg_type
));
523 } else if ((reg_only
) || (all_reg
)) {
524 if (registered_nexus
) {
526 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
527 * allow commands from registered nexuses.
530 pr_debug("Allowing implict CDB: 0x%02x for %s"
531 " reservation\n", cdb
[0],
532 core_scsi3_pr_dump_type(pr_reg_type
));
537 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
538 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
539 (registered_nexus
) ? "" : "un",
540 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
541 core_scsi3_pr_dump_type(pr_reg_type
));
543 return 1; /* Conflict by default */
546 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
548 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
551 * PRGeneration field shall contain the value of a 32-bit wrapping
552 * counter mainted by the device server.
554 * Note that this is done regardless of Active Persist across
555 * Target PowerLoss (APTPL)
557 * See spc4r17 section 6.3.12 READ_KEYS service action
559 spin_lock(&dev
->dev_reservation_lock
);
560 prg
= su_dev
->t10_pr
.pr_generation
++;
561 spin_unlock(&dev
->dev_reservation_lock
);
566 static int core_scsi3_pr_reservation_check(
570 struct se_device
*dev
= cmd
->se_dev
;
571 struct se_session
*sess
= cmd
->se_sess
;
577 * A legacy SPC-2 reservation is being held.
579 if (dev
->dev_flags
& DF_SPC2_RESERVATIONS
)
580 return core_scsi2_reservation_check(cmd
, pr_reg_type
);
582 spin_lock(&dev
->dev_reservation_lock
);
583 if (!dev
->dev_pr_res_holder
) {
584 spin_unlock(&dev
->dev_reservation_lock
);
587 *pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
588 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
589 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
) {
590 spin_unlock(&dev
->dev_reservation_lock
);
593 if (!dev
->dev_pr_res_holder
->isid_present_at_reg
) {
594 spin_unlock(&dev
->dev_reservation_lock
);
597 ret
= (dev
->dev_pr_res_holder
->pr_reg_bin_isid
==
598 sess
->sess_bin_isid
) ? 0 : -EINVAL
;
600 * Use bit in *pr_reg_type to notify ISID mismatch in
601 * core_scsi3_pr_seq_non_holder().
604 *pr_reg_type
|= 0x80000000;
605 spin_unlock(&dev
->dev_reservation_lock
);
610 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
611 struct se_device
*dev
,
612 struct se_node_acl
*nacl
,
613 struct se_dev_entry
*deve
,
619 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
620 struct t10_pr_registration
*pr_reg
;
622 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
624 pr_err("Unable to allocate struct t10_pr_registration\n");
628 pr_reg
->pr_aptpl_buf
= kzalloc(su_dev
->t10_pr
.pr_aptpl_buf_len
,
630 if (!pr_reg
->pr_aptpl_buf
) {
631 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
632 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
636 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
637 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
638 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
639 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
640 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
641 atomic_set(&pr_reg
->pr_res_holders
, 0);
642 pr_reg
->pr_reg_nacl
= nacl
;
643 pr_reg
->pr_reg_deve
= deve
;
644 pr_reg
->pr_res_mapped_lun
= deve
->mapped_lun
;
645 pr_reg
->pr_aptpl_target_lun
= deve
->se_lun
->unpacked_lun
;
646 pr_reg
->pr_res_key
= sa_res_key
;
647 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
648 pr_reg
->pr_reg_aptpl
= aptpl
;
649 pr_reg
->pr_reg_tg_pt_lun
= deve
->se_lun
;
651 * If an ISID value for this SCSI Initiator Port exists,
652 * save it to the registration now.
655 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
656 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
657 pr_reg
->isid_present_at_reg
= 1;
663 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
664 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
667 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
670 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
671 struct se_device
*dev
,
672 struct se_node_acl
*nacl
,
673 struct se_dev_entry
*deve
,
679 struct se_dev_entry
*deve_tmp
;
680 struct se_node_acl
*nacl_tmp
;
681 struct se_port
*port
, *port_tmp
;
682 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
683 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
686 * Create a registration for the I_T Nexus upon which the
687 * PROUT REGISTER was received.
689 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, deve
, isid
,
690 sa_res_key
, all_tg_pt
, aptpl
);
694 * Return pointer to pr_reg for ALL_TG_PT=0
699 * Create list of matching SCSI Initiator Port registrations
702 spin_lock(&dev
->se_port_lock
);
703 list_for_each_entry_safe(port
, port_tmp
, &dev
->dev_sep_list
, sep_list
) {
704 atomic_inc(&port
->sep_tg_pt_ref_cnt
);
705 smp_mb__after_atomic_inc();
706 spin_unlock(&dev
->se_port_lock
);
708 spin_lock_bh(&port
->sep_alua_lock
);
709 list_for_each_entry(deve_tmp
, &port
->sep_alua_list
,
712 * This pointer will be NULL for demo mode MappedLUNs
713 * that have not been make explict via a ConfigFS
714 * MappedLUN group for the SCSI Initiator Node ACL.
716 if (!deve_tmp
->se_lun_acl
)
719 nacl_tmp
= deve_tmp
->se_lun_acl
->se_lun_nacl
;
721 * Skip the matching struct se_node_acl that is allocated
724 if (nacl
== nacl_tmp
)
727 * Only perform PR registrations for target ports on
728 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
731 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
734 * Look for a matching Initiator Node ACL in ASCII format
736 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
739 atomic_inc(&deve_tmp
->pr_ref_count
);
740 smp_mb__after_atomic_inc();
741 spin_unlock_bh(&port
->sep_alua_lock
);
743 * Grab a configfs group dependency that is released
744 * for the exception path at label out: below, or upon
745 * completion of adding ALL_TG_PT=1 registrations in
746 * __core_scsi3_add_registration()
748 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
750 pr_err("core_scsi3_lunacl_depend"
752 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
753 smp_mb__after_atomic_dec();
754 atomic_dec(&deve_tmp
->pr_ref_count
);
755 smp_mb__after_atomic_dec();
759 * Located a matching SCSI Initiator Port on a different
760 * port, allocate the pr_reg_atp and attach it to the
761 * pr_reg->pr_reg_atp_list that will be processed once
762 * the original *pr_reg is processed in
763 * __core_scsi3_add_registration()
765 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
766 nacl_tmp
, deve_tmp
, NULL
,
767 sa_res_key
, all_tg_pt
, aptpl
);
769 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
770 smp_mb__after_atomic_dec();
771 atomic_dec(&deve_tmp
->pr_ref_count
);
772 smp_mb__after_atomic_dec();
773 core_scsi3_lunacl_undepend_item(deve_tmp
);
777 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
778 &pr_reg
->pr_reg_atp_list
);
779 spin_lock_bh(&port
->sep_alua_lock
);
781 spin_unlock_bh(&port
->sep_alua_lock
);
783 spin_lock(&dev
->se_port_lock
);
784 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
785 smp_mb__after_atomic_dec();
787 spin_unlock(&dev
->se_port_lock
);
791 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
792 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
793 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
794 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
795 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
797 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
801 int core_scsi3_alloc_aptpl_registration(
802 struct t10_reservation
*pr_tmpl
,
804 unsigned char *i_port
,
807 unsigned char *t_port
,
814 struct t10_pr_registration
*pr_reg
;
816 if (!i_port
|| !t_port
|| !sa_res_key
) {
817 pr_err("Illegal parameters for APTPL registration\n");
821 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
823 pr_err("Unable to allocate struct t10_pr_registration\n");
826 pr_reg
->pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
, GFP_KERNEL
);
828 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
829 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
830 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
831 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
832 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
833 atomic_set(&pr_reg
->pr_res_holders
, 0);
834 pr_reg
->pr_reg_nacl
= NULL
;
835 pr_reg
->pr_reg_deve
= NULL
;
836 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
837 pr_reg
->pr_aptpl_target_lun
= target_lun
;
838 pr_reg
->pr_res_key
= sa_res_key
;
839 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
840 pr_reg
->pr_reg_aptpl
= 1;
841 pr_reg
->pr_reg_tg_pt_lun
= NULL
;
842 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
843 pr_reg
->pr_res_type
= type
;
845 * If an ISID value had been saved in APTPL metadata for this
846 * SCSI Initiator Port, restore it now.
849 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
850 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
851 pr_reg
->isid_present_at_reg
= 1;
854 * Copy the i_port and t_port information from caller.
856 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
857 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
858 pr_reg
->pr_reg_tpgt
= tpgt
;
860 * Set pr_res_holder from caller, the pr_reg who is the reservation
861 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
862 * the Initiator Node LUN ACL from the fabric module is created for
865 pr_reg
->pr_res_holder
= res_holder
;
867 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
868 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
869 " metadata\n", (res_holder
) ? "+reservation" : "");
873 static void core_scsi3_aptpl_reserve(
874 struct se_device
*dev
,
875 struct se_portal_group
*tpg
,
876 struct se_node_acl
*node_acl
,
877 struct t10_pr_registration
*pr_reg
)
879 char i_buf
[PR_REG_ISID_ID_LEN
];
882 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
883 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
886 spin_lock(&dev
->dev_reservation_lock
);
887 dev
->dev_pr_res_holder
= pr_reg
;
888 spin_unlock(&dev
->dev_reservation_lock
);
890 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
891 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
892 tpg
->se_tpg_tfo
->get_fabric_name(),
893 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
894 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
895 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
896 tpg
->se_tpg_tfo
->get_fabric_name(), node_acl
->initiatorname
,
897 (prf_isid
) ? &i_buf
[0] : "");
900 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
901 struct t10_pr_registration
*, int, int);
903 static int __core_scsi3_check_aptpl_registration(
904 struct se_device
*dev
,
905 struct se_portal_group
*tpg
,
908 struct se_node_acl
*nacl
,
909 struct se_dev_entry
*deve
)
911 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
912 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
913 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
914 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
917 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
918 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
920 * Copy Initiator Port information from struct se_node_acl
922 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
923 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
924 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
925 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
927 * Look for the matching registrations+reservation from those
928 * created from APTPL metadata. Note that multiple registrations
929 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
932 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
933 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
935 if (!strcmp(pr_reg
->pr_iport
, i_port
) &&
936 (pr_reg
->pr_res_mapped_lun
== deve
->mapped_lun
) &&
937 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
938 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
939 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
941 pr_reg
->pr_reg_nacl
= nacl
;
942 pr_reg
->pr_reg_deve
= deve
;
943 pr_reg
->pr_reg_tg_pt_lun
= lun
;
945 list_del(&pr_reg
->pr_reg_aptpl_list
);
946 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
948 * At this point all of the pointers in *pr_reg will
949 * be setup, so go ahead and add the registration.
952 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
954 * If this registration is the reservation holder,
955 * make that happen now..
957 if (pr_reg
->pr_res_holder
)
958 core_scsi3_aptpl_reserve(dev
, tpg
,
961 * Reenable pr_aptpl_active to accept new metadata
962 * updates once the SCSI device is active again..
964 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
965 pr_tmpl
->pr_aptpl_active
= 1;
968 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
973 int core_scsi3_check_aptpl_registration(
974 struct se_device
*dev
,
975 struct se_portal_group
*tpg
,
977 struct se_lun_acl
*lun_acl
)
979 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
980 struct se_node_acl
*nacl
= lun_acl
->se_lun_nacl
;
981 struct se_dev_entry
*deve
= &nacl
->device_list
[lun_acl
->mapped_lun
];
983 if (su_dev
->t10_pr
.res_type
!= SPC3_PERSISTENT_RESERVATIONS
)
986 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
987 lun
->unpacked_lun
, nacl
, deve
);
990 static void __core_scsi3_dump_registration(
991 struct target_core_fabric_ops
*tfo
,
992 struct se_device
*dev
,
993 struct se_node_acl
*nacl
,
994 struct t10_pr_registration
*pr_reg
,
997 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
998 char i_buf
[PR_REG_ISID_ID_LEN
];
1001 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
1002 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1003 PR_REG_ISID_ID_LEN
);
1005 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1006 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== 2) ?
1007 "_AND_MOVE" : (register_type
== 1) ?
1008 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
1009 (prf_isid
) ? i_buf
: "");
1010 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1011 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
1012 tfo
->tpg_get_tag(se_tpg
));
1013 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1014 " Port(s)\n", tfo
->get_fabric_name(),
1015 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1016 dev
->transport
->name
);
1017 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1018 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
1019 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
1020 pr_reg
->pr_reg_aptpl
);
1024 * this function can be called with struct se_device->dev_reservation_lock
1025 * when register_move = 1
1027 static void __core_scsi3_add_registration(
1028 struct se_device
*dev
,
1029 struct se_node_acl
*nacl
,
1030 struct t10_pr_registration
*pr_reg
,
1034 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1035 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1036 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1037 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1040 * Increment PRgeneration counter for struct se_device upon a successful
1041 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1043 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1044 * action, the struct se_device->dev_reservation_lock will already be held,
1045 * so we do not call core_scsi3_pr_generation() which grabs the lock
1048 pr_reg
->pr_res_generation
= (register_move
) ?
1049 su_dev
->t10_pr
.pr_generation
++ :
1050 core_scsi3_pr_generation(dev
);
1052 spin_lock(&pr_tmpl
->registration_lock
);
1053 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1054 pr_reg
->pr_reg_deve
->def_pr_registered
= 1;
1056 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1057 spin_unlock(&pr_tmpl
->registration_lock
);
1059 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1061 if (!pr_reg
->pr_reg_all_tg_pt
|| register_move
)
1064 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1065 * allocated in __core_scsi3_alloc_registration()
1067 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1068 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1069 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1071 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1073 spin_lock(&pr_tmpl
->registration_lock
);
1074 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1075 &pr_tmpl
->registration_list
);
1076 pr_reg_tmp
->pr_reg_deve
->def_pr_registered
= 1;
1078 __core_scsi3_dump_registration(tfo
, dev
,
1079 pr_reg_tmp
->pr_reg_nacl
, pr_reg_tmp
,
1081 spin_unlock(&pr_tmpl
->registration_lock
);
1083 * Drop configfs group dependency reference from
1084 * __core_scsi3_alloc_registration()
1086 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1090 static int core_scsi3_alloc_registration(
1091 struct se_device
*dev
,
1092 struct se_node_acl
*nacl
,
1093 struct se_dev_entry
*deve
,
1094 unsigned char *isid
,
1101 struct t10_pr_registration
*pr_reg
;
1103 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, deve
, isid
,
1104 sa_res_key
, all_tg_pt
, aptpl
);
1108 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1109 register_type
, register_move
);
1113 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1114 struct se_device
*dev
,
1115 struct se_node_acl
*nacl
,
1116 unsigned char *isid
)
1118 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1119 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1120 struct se_portal_group
*tpg
;
1122 spin_lock(&pr_tmpl
->registration_lock
);
1123 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1124 &pr_tmpl
->registration_list
, pr_reg_list
) {
1126 * First look for a matching struct se_node_acl
1128 if (pr_reg
->pr_reg_nacl
!= nacl
)
1131 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1133 * If this registration does NOT contain a fabric provided
1134 * ISID, then we have found a match.
1136 if (!pr_reg
->isid_present_at_reg
) {
1138 * Determine if this SCSI device server requires that
1139 * SCSI Intiatior TransportID w/ ISIDs is enforced
1140 * for fabric modules (iSCSI) requiring them.
1142 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1143 if (dev
->se_sub_dev
->se_dev_attrib
.enforce_pr_isids
)
1146 atomic_inc(&pr_reg
->pr_res_holders
);
1147 smp_mb__after_atomic_inc();
1148 spin_unlock(&pr_tmpl
->registration_lock
);
1152 * If the *pr_reg contains a fabric defined ISID for multi-value
1153 * SCSI Initiator Port TransportIDs, then we expect a valid
1154 * matching ISID to be provided by the local SCSI Initiator Port.
1158 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1161 atomic_inc(&pr_reg
->pr_res_holders
);
1162 smp_mb__after_atomic_inc();
1163 spin_unlock(&pr_tmpl
->registration_lock
);
1166 spin_unlock(&pr_tmpl
->registration_lock
);
1171 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1172 struct se_device
*dev
,
1173 struct se_node_acl
*nacl
,
1174 struct se_session
*sess
)
1176 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1177 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1179 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1180 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1181 tpg
->se_tpg_tfo
->sess_get_initiator_sid(sess
, &buf
[0],
1186 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1189 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1191 atomic_dec(&pr_reg
->pr_res_holders
);
1192 smp_mb__after_atomic_dec();
1195 static int core_scsi3_check_implict_release(
1196 struct se_device
*dev
,
1197 struct t10_pr_registration
*pr_reg
)
1199 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1200 struct t10_pr_registration
*pr_res_holder
;
1203 spin_lock(&dev
->dev_reservation_lock
);
1204 pr_res_holder
= dev
->dev_pr_res_holder
;
1205 if (!pr_res_holder
) {
1206 spin_unlock(&dev
->dev_reservation_lock
);
1209 if (pr_res_holder
== pr_reg
) {
1211 * Perform an implict RELEASE if the registration that
1212 * is being released is holding the reservation.
1214 * From spc4r17, section 5.7.11.1:
1216 * e) If the I_T nexus is the persistent reservation holder
1217 * and the persistent reservation is not an all registrants
1218 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1219 * service action or REGISTER AND IGNORE EXISTING KEY
1220 * service action with the SERVICE ACTION RESERVATION KEY
1221 * field set to zero (see 5.7.11.3).
1223 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0);
1226 * For 'All Registrants' reservation types, all existing
1227 * registrations are still processed as reservation holders
1228 * in core_scsi3_pr_seq_non_holder() after the initial
1229 * reservation holder is implictly released here.
1231 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1232 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1233 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1234 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1235 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1236 " UNREGISTER while existing reservation with matching"
1237 " key 0x%016Lx is present from another SCSI Initiator"
1238 " Port\n", pr_reg
->pr_res_key
);
1241 spin_unlock(&dev
->dev_reservation_lock
);
1247 * Called with struct t10_reservation->registration_lock held.
1249 static void __core_scsi3_free_registration(
1250 struct se_device
*dev
,
1251 struct t10_pr_registration
*pr_reg
,
1252 struct list_head
*preempt_and_abort_list
,
1255 struct target_core_fabric_ops
*tfo
=
1256 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1257 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1258 char i_buf
[PR_REG_ISID_ID_LEN
];
1261 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1262 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1263 PR_REG_ISID_ID_LEN
);
1265 pr_reg
->pr_reg_deve
->def_pr_registered
= 0;
1266 pr_reg
->pr_reg_deve
->pr_res_key
= 0;
1267 list_del(&pr_reg
->pr_reg_list
);
1269 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1270 * so call core_scsi3_put_pr_reg() to decrement our reference.
1273 core_scsi3_put_pr_reg(pr_reg
);
1275 * Wait until all reference from any other I_T nexuses for this
1276 * *pr_reg have been released. Because list_del() is called above,
1277 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1278 * count back to zero, and we release *pr_reg.
1280 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1281 spin_unlock(&pr_tmpl
->registration_lock
);
1282 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1283 tfo
->get_fabric_name());
1285 spin_lock(&pr_tmpl
->registration_lock
);
1288 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1289 " Node: %s%s\n", tfo
->get_fabric_name(),
1290 pr_reg
->pr_reg_nacl
->initiatorname
,
1291 (prf_isid
) ? &i_buf
[0] : "");
1292 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1293 " Port(s)\n", tfo
->get_fabric_name(),
1294 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1295 dev
->transport
->name
);
1296 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1297 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1298 pr_reg
->pr_res_generation
);
1300 if (!preempt_and_abort_list
) {
1301 pr_reg
->pr_reg_deve
= NULL
;
1302 pr_reg
->pr_reg_nacl
= NULL
;
1303 kfree(pr_reg
->pr_aptpl_buf
);
1304 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1308 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1309 * are released once the ABORT_TASK_SET has completed..
1311 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1314 void core_scsi3_free_pr_reg_from_nacl(
1315 struct se_device
*dev
,
1316 struct se_node_acl
*nacl
)
1318 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1319 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1321 * If the passed se_node_acl matches the reservation holder,
1322 * release the reservation.
1324 spin_lock(&dev
->dev_reservation_lock
);
1325 pr_res_holder
= dev
->dev_pr_res_holder
;
1326 if ((pr_res_holder
!= NULL
) &&
1327 (pr_res_holder
->pr_reg_nacl
== nacl
))
1328 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0);
1329 spin_unlock(&dev
->dev_reservation_lock
);
1331 * Release any registration associated with the struct se_node_acl.
1333 spin_lock(&pr_tmpl
->registration_lock
);
1334 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1335 &pr_tmpl
->registration_list
, pr_reg_list
) {
1337 if (pr_reg
->pr_reg_nacl
!= nacl
)
1340 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1342 spin_unlock(&pr_tmpl
->registration_lock
);
1345 void core_scsi3_free_all_registrations(
1346 struct se_device
*dev
)
1348 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
1349 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1351 spin_lock(&dev
->dev_reservation_lock
);
1352 pr_res_holder
= dev
->dev_pr_res_holder
;
1353 if (pr_res_holder
!= NULL
) {
1354 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1355 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1358 spin_unlock(&dev
->dev_reservation_lock
);
1360 spin_lock(&pr_tmpl
->registration_lock
);
1361 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1362 &pr_tmpl
->registration_list
, pr_reg_list
) {
1364 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1366 spin_unlock(&pr_tmpl
->registration_lock
);
1368 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1369 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1370 pr_reg_aptpl_list
) {
1371 list_del(&pr_reg
->pr_reg_aptpl_list
);
1372 kfree(pr_reg
->pr_aptpl_buf
);
1373 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1375 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1378 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1380 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1381 &tpg
->tpg_group
.cg_item
);
1384 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1386 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1387 &tpg
->tpg_group
.cg_item
);
1389 atomic_dec(&tpg
->tpg_pr_ref_count
);
1390 smp_mb__after_atomic_dec();
1393 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1395 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1397 if (nacl
->dynamic_node_acl
)
1400 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1401 &nacl
->acl_group
.cg_item
);
1404 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1406 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1408 if (nacl
->dynamic_node_acl
) {
1409 atomic_dec(&nacl
->acl_pr_ref_count
);
1410 smp_mb__after_atomic_dec();
1414 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1415 &nacl
->acl_group
.cg_item
);
1417 atomic_dec(&nacl
->acl_pr_ref_count
);
1418 smp_mb__after_atomic_dec();
1421 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1423 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1424 struct se_node_acl
*nacl
;
1425 struct se_portal_group
*tpg
;
1427 * For nacl->dynamic_node_acl=1
1432 nacl
= lun_acl
->se_lun_nacl
;
1435 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1436 &lun_acl
->se_lun_group
.cg_item
);
1439 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1441 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1442 struct se_node_acl
*nacl
;
1443 struct se_portal_group
*tpg
;
1445 * For nacl->dynamic_node_acl=1
1448 atomic_dec(&se_deve
->pr_ref_count
);
1449 smp_mb__after_atomic_dec();
1452 nacl
= lun_acl
->se_lun_nacl
;
1455 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1456 &lun_acl
->se_lun_group
.cg_item
);
1458 atomic_dec(&se_deve
->pr_ref_count
);
1459 smp_mb__after_atomic_dec();
1462 static int core_scsi3_decode_spec_i_port(
1464 struct se_portal_group
*tpg
,
1465 unsigned char *l_isid
,
1470 struct se_device
*dev
= cmd
->se_dev
;
1471 struct se_port
*tmp_port
;
1472 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1473 struct se_session
*se_sess
= cmd
->se_sess
;
1474 struct se_node_acl
*dest_node_acl
= NULL
;
1475 struct se_dev_entry
*dest_se_deve
= NULL
, *local_se_deve
;
1476 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1477 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1478 struct list_head tid_dest_list
;
1479 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1480 struct target_core_fabric_ops
*tmp_tf_ops
;
1482 unsigned char *ptr
, *i_str
= NULL
, proto_ident
, tmp_proto_ident
;
1483 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
1484 u32 tpdl
, tid_len
= 0;
1485 int ret
, dest_local_nexus
, prf_isid
;
1488 memset(dest_iport
, 0, 64);
1489 INIT_LIST_HEAD(&tid_dest_list
);
1491 local_se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
1493 * Allocate a struct pr_transport_id_holder and setup the
1494 * local_node_acl and local_se_deve pointers and add to
1495 * struct list_head tid_dest_list for add registration
1496 * processing in the loop of tid_dest_list below.
1498 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1500 pr_err("Unable to allocate tidh_new\n");
1501 return PYX_TRANSPORT_LU_COMM_FAILURE
;
1503 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1504 tidh_new
->dest_tpg
= tpg
;
1505 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1506 tidh_new
->dest_se_deve
= local_se_deve
;
1508 local_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1509 se_sess
->se_node_acl
, local_se_deve
, l_isid
,
1510 sa_res_key
, all_tg_pt
, aptpl
);
1511 if (!local_pr_reg
) {
1513 return PYX_TRANSPORT_LU_COMM_FAILURE
;
1515 tidh_new
->dest_pr_reg
= local_pr_reg
;
1517 * The local I_T nexus does not hold any configfs dependances,
1518 * so we set tid_h->dest_local_nexus=1 to prevent the
1519 * configfs_undepend_item() calls in the tid_dest_list loops below.
1521 tidh_new
->dest_local_nexus
= 1;
1522 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1524 buf
= transport_kmap_first_data_page(cmd
);
1526 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1527 * first extract TransportID Parameter Data Length, and make sure
1528 * the value matches up to the SCSI expected data transfer length.
1530 tpdl
= (buf
[24] & 0xff) << 24;
1531 tpdl
|= (buf
[25] & 0xff) << 16;
1532 tpdl
|= (buf
[26] & 0xff) << 8;
1533 tpdl
|= buf
[27] & 0xff;
1535 if ((tpdl
+ 28) != cmd
->data_length
) {
1536 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1537 " does not equal CDB data_length: %u\n", tpdl
,
1539 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1543 * Start processing the received transport IDs using the
1544 * receiving I_T Nexus portal's fabric dependent methods to
1545 * obtain the SCSI Initiator Port/Device Identifiers.
1550 proto_ident
= (ptr
[0] & 0x0f);
1553 spin_lock(&dev
->se_port_lock
);
1554 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1555 tmp_tpg
= tmp_port
->sep_tpg
;
1558 tmp_tf_ops
= tmp_tpg
->se_tpg_tfo
;
1561 if (!tmp_tf_ops
->get_fabric_proto_ident
||
1562 !tmp_tf_ops
->tpg_parse_pr_out_transport_id
)
1565 * Look for the matching proto_ident provided by
1566 * the received TransportID
1568 tmp_proto_ident
= tmp_tf_ops
->get_fabric_proto_ident(tmp_tpg
);
1569 if (tmp_proto_ident
!= proto_ident
)
1571 dest_rtpi
= tmp_port
->sep_rtpi
;
1573 i_str
= tmp_tf_ops
->tpg_parse_pr_out_transport_id(
1574 tmp_tpg
, (const char *)ptr
, &tid_len
,
1579 atomic_inc(&tmp_tpg
->tpg_pr_ref_count
);
1580 smp_mb__after_atomic_inc();
1581 spin_unlock(&dev
->se_port_lock
);
1583 ret
= core_scsi3_tpg_depend_item(tmp_tpg
);
1585 pr_err(" core_scsi3_tpg_depend_item()"
1587 atomic_dec(&tmp_tpg
->tpg_pr_ref_count
);
1588 smp_mb__after_atomic_dec();
1589 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1593 * Locate the desination initiator ACL to be registered
1594 * from the decoded fabric module specific TransportID
1597 spin_lock_irq(&tmp_tpg
->acl_node_lock
);
1598 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1600 if (dest_node_acl
) {
1601 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
1602 smp_mb__after_atomic_inc();
1604 spin_unlock_irq(&tmp_tpg
->acl_node_lock
);
1606 if (!dest_node_acl
) {
1607 core_scsi3_tpg_undepend_item(tmp_tpg
);
1608 spin_lock(&dev
->se_port_lock
);
1612 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
1614 pr_err("configfs_depend_item() failed"
1615 " for dest_node_acl->acl_group\n");
1616 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
1617 smp_mb__after_atomic_dec();
1618 core_scsi3_tpg_undepend_item(tmp_tpg
);
1619 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1624 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1625 " %s Port RTPI: %hu\n",
1626 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1627 dest_node_acl
->initiatorname
, dest_rtpi
);
1629 spin_lock(&dev
->se_port_lock
);
1632 spin_unlock(&dev
->se_port_lock
);
1635 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1637 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1641 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1642 " tid_len: %d for %s + %s\n",
1643 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1644 tpdl
, tid_len
, i_str
, iport_ptr
);
1646 if (tid_len
> tpdl
) {
1647 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1648 " %u for Transport ID: %s\n", tid_len
, ptr
);
1649 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1650 core_scsi3_tpg_undepend_item(dest_tpg
);
1651 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1655 * Locate the desintation struct se_dev_entry pointer for matching
1656 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1659 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1661 if (!dest_se_deve
) {
1662 pr_err("Unable to locate %s dest_se_deve"
1663 " from destination RTPI: %hu\n",
1664 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1667 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1668 core_scsi3_tpg_undepend_item(dest_tpg
);
1669 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1673 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
1675 pr_err("core_scsi3_lunacl_depend_item()"
1677 atomic_dec(&dest_se_deve
->pr_ref_count
);
1678 smp_mb__after_atomic_dec();
1679 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1680 core_scsi3_tpg_undepend_item(dest_tpg
);
1681 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1685 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1686 " dest_se_deve mapped_lun: %u\n",
1687 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1688 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1691 * Skip any TransportIDs that already have a registration for
1694 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1697 core_scsi3_put_pr_reg(pr_reg_e
);
1698 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1699 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1700 core_scsi3_tpg_undepend_item(dest_tpg
);
1707 * Allocate a struct pr_transport_id_holder and setup
1708 * the dest_node_acl and dest_se_deve pointers for the
1711 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1714 pr_err("Unable to allocate tidh_new\n");
1715 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1716 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1717 core_scsi3_tpg_undepend_item(dest_tpg
);
1718 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1721 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1722 tidh_new
->dest_tpg
= dest_tpg
;
1723 tidh_new
->dest_node_acl
= dest_node_acl
;
1724 tidh_new
->dest_se_deve
= dest_se_deve
;
1727 * Allocate, but do NOT add the registration for the
1728 * TransportID referenced SCSI Initiator port. This
1729 * done because of the following from spc4r17 in section
1730 * 6.14.3 wrt SPEC_I_PT:
1732 * "If a registration fails for any initiator port (e.g., if th
1733 * logical unit does not have enough resources available to
1734 * hold the registration information), no registrations shall be
1735 * made, and the command shall be terminated with
1736 * CHECK CONDITION status."
1738 * That means we call __core_scsi3_alloc_registration() here,
1739 * and then call __core_scsi3_add_registration() in the
1740 * 2nd loop which will never fail.
1742 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1743 dest_node_acl
, dest_se_deve
, iport_ptr
,
1744 sa_res_key
, all_tg_pt
, aptpl
);
1746 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1747 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1748 core_scsi3_tpg_undepend_item(dest_tpg
);
1750 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1753 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1754 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1762 transport_kunmap_first_data_page(cmd
);
1765 * Go ahead and create a registrations from tid_dest_list for the
1766 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1769 * The SA Reservation Key from the PROUT is set for the
1770 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1771 * means that the TransportID Initiator port will be
1772 * registered on all of the target ports in the SCSI target device
1773 * ALL_TG_PT=0 means the registration will only be for the
1774 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1777 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1778 dest_tpg
= tidh
->dest_tpg
;
1779 dest_node_acl
= tidh
->dest_node_acl
;
1780 dest_se_deve
= tidh
->dest_se_deve
;
1781 dest_pr_reg
= tidh
->dest_pr_reg
;
1782 dest_local_nexus
= tidh
->dest_local_nexus
;
1784 list_del(&tidh
->dest_list
);
1787 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1788 prf_isid
= core_pr_dump_initiator_port(dest_pr_reg
, &i_buf
[0],
1789 PR_REG_ISID_ID_LEN
);
1791 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1794 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1795 " registered Transport ID for Node: %s%s Mapped LUN:"
1796 " %u\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1797 dest_node_acl
->initiatorname
, (prf_isid
) ?
1798 &i_buf
[0] : "", dest_se_deve
->mapped_lun
);
1800 if (dest_local_nexus
)
1803 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1804 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1805 core_scsi3_tpg_undepend_item(dest_tpg
);
1810 transport_kunmap_first_data_page(cmd
);
1812 * For the failure case, release everything from tid_dest_list
1813 * including *dest_pr_reg and the configfs dependances..
1815 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1816 dest_tpg
= tidh
->dest_tpg
;
1817 dest_node_acl
= tidh
->dest_node_acl
;
1818 dest_se_deve
= tidh
->dest_se_deve
;
1819 dest_pr_reg
= tidh
->dest_pr_reg
;
1820 dest_local_nexus
= tidh
->dest_local_nexus
;
1822 list_del(&tidh
->dest_list
);
1825 * Release any extra ALL_TG_PT=1 registrations for
1826 * the SPEC_I_PT=1 case.
1828 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1829 &dest_pr_reg
->pr_reg_atp_list
,
1830 pr_reg_atp_mem_list
) {
1831 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1832 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1833 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1836 kfree(dest_pr_reg
->pr_aptpl_buf
);
1837 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1839 if (dest_local_nexus
)
1842 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1843 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1844 core_scsi3_tpg_undepend_item(dest_tpg
);
1850 * Called with struct se_device->dev_reservation_lock held
1852 static int __core_scsi3_update_aptpl_buf(
1853 struct se_device
*dev
,
1855 u32 pr_aptpl_buf_len
,
1856 int clear_aptpl_metadata
)
1859 struct se_portal_group
*tpg
;
1860 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1861 struct t10_pr_registration
*pr_reg
;
1862 unsigned char tmp
[512], isid_buf
[32];
1866 memset(buf
, 0, pr_aptpl_buf_len
);
1868 * Called to clear metadata once APTPL has been deactivated.
1870 if (clear_aptpl_metadata
) {
1871 snprintf(buf
, pr_aptpl_buf_len
,
1872 "No Registrations or Reservations\n");
1876 * Walk the registration list..
1878 spin_lock(&su_dev
->t10_pr
.registration_lock
);
1879 list_for_each_entry(pr_reg
, &su_dev
->t10_pr
.registration_list
,
1884 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1885 lun
= pr_reg
->pr_reg_tg_pt_lun
;
1887 * Write out any ISID value to APTPL metadata that was included
1888 * in the original registration.
1890 if (pr_reg
->isid_present_at_reg
)
1891 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1892 pr_reg
->pr_reg_isid
);
1894 * Include special metadata if the pr_reg matches the
1895 * reservation holder.
1897 if (dev
->dev_pr_res_holder
== pr_reg
) {
1898 snprintf(tmp
, 512, "PR_REG_START: %d"
1899 "\ninitiator_fabric=%s\n"
1900 "initiator_node=%s\n%s"
1902 "res_holder=1\nres_type=%02x\n"
1903 "res_scope=%02x\nres_all_tg_pt=%d\n"
1904 "mapped_lun=%u\n", reg_count
,
1905 tpg
->se_tpg_tfo
->get_fabric_name(),
1906 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1907 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1908 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1909 pr_reg
->pr_res_mapped_lun
);
1911 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1912 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1913 "sa_res_key=%llu\nres_holder=0\n"
1914 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1915 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1916 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1917 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1918 pr_reg
->pr_res_mapped_lun
);
1921 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1922 pr_err("Unable to update renaming"
1923 " APTPL metadata\n");
1924 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1927 len
+= sprintf(buf
+len
, "%s", tmp
);
1930 * Include information about the associated SCSI target port.
1932 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1933 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1934 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1935 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1936 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1937 lun
->lun_sep
->sep_rtpi
, lun
->unpacked_lun
, reg_count
);
1939 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1940 pr_err("Unable to update renaming"
1941 " APTPL metadata\n");
1942 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1945 len
+= sprintf(buf
+len
, "%s", tmp
);
1948 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
1951 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1956 static int core_scsi3_update_aptpl_buf(
1957 struct se_device
*dev
,
1959 u32 pr_aptpl_buf_len
,
1960 int clear_aptpl_metadata
)
1964 spin_lock(&dev
->dev_reservation_lock
);
1965 ret
= __core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
1966 clear_aptpl_metadata
);
1967 spin_unlock(&dev
->dev_reservation_lock
);
1973 * Called with struct se_device->aptpl_file_mutex held
1975 static int __core_scsi3_write_aptpl_to_file(
1976 struct se_device
*dev
,
1978 u32 pr_aptpl_buf_len
)
1980 struct t10_wwn
*wwn
= &dev
->se_sub_dev
->t10_wwn
;
1982 struct iovec iov
[1];
1983 mm_segment_t old_fs
;
1984 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1988 memset(iov
, 0, sizeof(struct iovec
));
1989 memset(path
, 0, 512);
1991 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
1992 pr_err("WWN value for struct se_device does not fit"
1993 " into path buffer\n");
1997 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
1998 file
= filp_open(path
, flags
, 0600);
1999 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
2000 pr_err("filp_open(%s) for APTPL metadata"
2002 return (PTR_ERR(file
) < 0 ? PTR_ERR(file
) : -ENOENT
);
2005 iov
[0].iov_base
= &buf
[0];
2006 if (!pr_aptpl_buf_len
)
2007 iov
[0].iov_len
= (strlen(&buf
[0]) + 1); /* Add extra for NULL */
2009 iov
[0].iov_len
= pr_aptpl_buf_len
;
2013 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
2017 pr_debug("Error writing APTPL metadata file: %s\n", path
);
2018 filp_close(file
, NULL
);
2021 filp_close(file
, NULL
);
2026 static int core_scsi3_update_and_write_aptpl(
2027 struct se_device
*dev
,
2028 unsigned char *in_buf
,
2029 u32 in_pr_aptpl_buf_len
)
2031 unsigned char null_buf
[64], *buf
;
2032 u32 pr_aptpl_buf_len
;
2033 int ret
, clear_aptpl_metadata
= 0;
2035 * Can be called with a NULL pointer from PROUT service action CLEAR
2038 memset(null_buf
, 0, 64);
2041 * This will clear the APTPL metadata to:
2042 * "No Registrations or Reservations" status
2044 pr_aptpl_buf_len
= 64;
2045 clear_aptpl_metadata
= 1;
2048 pr_aptpl_buf_len
= in_pr_aptpl_buf_len
;
2051 ret
= core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
2052 clear_aptpl_metadata
);
2056 * __core_scsi3_write_aptpl_to_file() will call strlen()
2057 * on the passed buf to determine pr_aptpl_buf_len.
2059 ret
= __core_scsi3_write_aptpl_to_file(dev
, buf
, 0);
2066 static int core_scsi3_emulate_pro_register(
2075 struct se_session
*se_sess
= cmd
->se_sess
;
2076 struct se_device
*dev
= cmd
->se_dev
;
2077 struct se_dev_entry
*se_deve
;
2078 struct se_lun
*se_lun
= cmd
->se_lun
;
2079 struct se_portal_group
*se_tpg
;
2080 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
, *pr_reg_e
;
2081 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2082 /* Used for APTPL metadata w/ UNREGISTER */
2083 unsigned char *pr_aptpl_buf
= NULL
;
2084 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2085 int pr_holder
= 0, ret
= 0, type
;
2087 if (!se_sess
|| !se_lun
) {
2088 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2089 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2091 se_tpg
= se_sess
->se_tpg
;
2092 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2094 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2095 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2096 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2098 isid_ptr
= &isid_buf
[0];
2101 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2103 pr_reg_e
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2106 pr_warn("SPC-3 PR: Reservation Key non-zero"
2107 " for SA REGISTER, returning CONFLICT\n");
2108 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2111 * Do nothing but return GOOD status.
2114 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2118 * Perform the Service Action REGISTER on the Initiator
2119 * Port Endpoint that the PRO was received from on the
2120 * Logical Unit of the SCSI device server.
2122 ret
= core_scsi3_alloc_registration(cmd
->se_dev
,
2123 se_sess
->se_node_acl
, se_deve
, isid_ptr
,
2124 sa_res_key
, all_tg_pt
, aptpl
,
2127 pr_err("Unable to allocate"
2128 " struct t10_pr_registration\n");
2129 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2133 * Register both the Initiator port that received
2134 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2135 * TransportID from Parameter list and loop through
2136 * fabric dependent parameter list while calling
2137 * logic from of core_scsi3_alloc_registration() for
2138 * each TransportID provided SCSI Initiator Port/Device
2140 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2141 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2146 * Nothing left to do for the APTPL=0 case.
2149 pr_tmpl
->pr_aptpl_active
= 0;
2150 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2151 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2156 * Locate the newly allocated local I_T Nexus *pr_reg, and
2157 * update the APTPL metadata information using its
2158 * preallocated *pr_reg->pr_aptpl_buf.
2160 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2161 se_sess
->se_node_acl
, se_sess
);
2163 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2164 &pr_reg
->pr_aptpl_buf
[0],
2165 pr_tmpl
->pr_aptpl_buf_len
);
2167 pr_tmpl
->pr_aptpl_active
= 1;
2168 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2171 core_scsi3_put_pr_reg(pr_reg
);
2175 * Locate the existing *pr_reg via struct se_node_acl pointers
2178 type
= pr_reg
->pr_res_type
;
2181 if (res_key
!= pr_reg
->pr_res_key
) {
2182 pr_err("SPC-3 PR REGISTER: Received"
2183 " res_key: 0x%016Lx does not match"
2184 " existing SA REGISTER res_key:"
2185 " 0x%016Lx\n", res_key
,
2186 pr_reg
->pr_res_key
);
2187 core_scsi3_put_pr_reg(pr_reg
);
2188 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2192 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2193 " set while sa_res_key=0\n");
2194 core_scsi3_put_pr_reg(pr_reg
);
2195 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2198 * An existing ALL_TG_PT=1 registration being released
2199 * must also set ALL_TG_PT=1 in the incoming PROUT.
2201 if (pr_reg
->pr_reg_all_tg_pt
&& !(all_tg_pt
)) {
2202 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2203 " registration exists, but ALL_TG_PT=1 bit not"
2204 " present in received PROUT\n");
2205 core_scsi3_put_pr_reg(pr_reg
);
2206 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
2209 * Allocate APTPL metadata buffer used for UNREGISTER ops
2212 pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
,
2214 if (!pr_aptpl_buf
) {
2215 pr_err("Unable to allocate"
2217 core_scsi3_put_pr_reg(pr_reg
);
2218 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2222 * sa_res_key=0 Unregister Reservation Key for registered I_T
2223 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2227 pr_holder
= core_scsi3_check_implict_release(
2228 cmd
->se_dev
, pr_reg
);
2229 if (pr_holder
< 0) {
2230 kfree(pr_aptpl_buf
);
2231 core_scsi3_put_pr_reg(pr_reg
);
2232 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2235 spin_lock(&pr_tmpl
->registration_lock
);
2237 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2238 * and matching pr_res_key.
2240 if (pr_reg
->pr_reg_all_tg_pt
) {
2241 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2242 &pr_tmpl
->registration_list
,
2245 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2248 if (pr_reg_p
->pr_res_key
!= res_key
)
2251 if (pr_reg
== pr_reg_p
)
2254 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2255 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2258 __core_scsi3_free_registration(dev
,
2263 * Release the calling I_T Nexus registration now..
2265 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
,
2268 * From spc4r17, section 5.7.11.3 Unregistering
2270 * If the persistent reservation is a registrants only
2271 * type, the device server shall establish a unit
2272 * attention condition for the initiator port associated
2273 * with every registered I_T nexus except for the I_T
2274 * nexus on which the PERSISTENT RESERVE OUT command was
2275 * received, with the additional sense code set to
2276 * RESERVATIONS RELEASED.
2279 ((type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
2280 (type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
))) {
2281 list_for_each_entry(pr_reg_p
,
2282 &pr_tmpl
->registration_list
,
2285 core_scsi3_ua_allocate(
2286 pr_reg_p
->pr_reg_nacl
,
2287 pr_reg_p
->pr_res_mapped_lun
,
2289 ASCQ_2AH_RESERVATIONS_RELEASED
);
2292 spin_unlock(&pr_tmpl
->registration_lock
);
2295 pr_tmpl
->pr_aptpl_active
= 0;
2296 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2297 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2298 " for UNREGISTER\n");
2302 ret
= core_scsi3_update_and_write_aptpl(dev
,
2304 pr_tmpl
->pr_aptpl_buf_len
);
2306 pr_tmpl
->pr_aptpl_active
= 1;
2307 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2308 " for UNREGISTER\n");
2311 kfree(pr_aptpl_buf
);
2315 * Increment PRgeneration counter for struct se_device"
2316 * upon a successful REGISTER, see spc4r17 section 6.3.2
2317 * READ_KEYS service action.
2319 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(
2321 pr_reg
->pr_res_key
= sa_res_key
;
2322 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2323 " Key for %s to: 0x%016Lx PRgeneration:"
2324 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2325 (ignore_key
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2326 pr_reg
->pr_reg_nacl
->initiatorname
,
2327 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2330 pr_tmpl
->pr_aptpl_active
= 0;
2331 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2332 core_scsi3_put_pr_reg(pr_reg
);
2333 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2338 ret
= core_scsi3_update_and_write_aptpl(dev
,
2340 pr_tmpl
->pr_aptpl_buf_len
);
2342 pr_tmpl
->pr_aptpl_active
= 1;
2343 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2347 kfree(pr_aptpl_buf
);
2348 core_scsi3_put_pr_reg(pr_reg
);
2354 unsigned char *core_scsi3_pr_dump_type(int type
)
2357 case PR_TYPE_WRITE_EXCLUSIVE
:
2358 return "Write Exclusive Access";
2359 case PR_TYPE_EXCLUSIVE_ACCESS
:
2360 return "Exclusive Access";
2361 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2362 return "Write Exclusive Access, Registrants Only";
2363 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2364 return "Exclusive Access, Registrants Only";
2365 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2366 return "Write Exclusive Access, All Registrants";
2367 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2368 return "Exclusive Access, All Registrants";
2373 return "Unknown SPC-3 PR Type";
2376 static int core_scsi3_pro_reserve(
2378 struct se_device
*dev
,
2383 struct se_session
*se_sess
= cmd
->se_sess
;
2384 struct se_dev_entry
*se_deve
;
2385 struct se_lun
*se_lun
= cmd
->se_lun
;
2386 struct se_portal_group
*se_tpg
;
2387 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2388 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2389 char i_buf
[PR_REG_ISID_ID_LEN
];
2392 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2394 if (!se_sess
|| !se_lun
) {
2395 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2396 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2398 se_tpg
= se_sess
->se_tpg
;
2399 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2401 * Locate the existing *pr_reg via struct se_node_acl pointers
2403 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2406 pr_err("SPC-3 PR: Unable to locate"
2407 " PR_REGISTERED *pr_reg for RESERVE\n");
2408 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2411 * From spc4r17 Section 5.7.9: Reserving:
2413 * An application client creates a persistent reservation by issuing
2414 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2415 * a registered I_T nexus with the following parameters:
2416 * a) RESERVATION KEY set to the value of the reservation key that is
2417 * registered with the logical unit for the I_T nexus; and
2419 if (res_key
!= pr_reg
->pr_res_key
) {
2420 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2421 " does not match existing SA REGISTER res_key:"
2422 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2423 core_scsi3_put_pr_reg(pr_reg
);
2424 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2427 * From spc4r17 Section 5.7.9: Reserving:
2430 * b) TYPE field and SCOPE field set to the persistent reservation
2433 * Only one persistent reservation is allowed at a time per logical unit
2434 * and that persistent reservation has a scope of LU_SCOPE.
2436 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2437 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2438 core_scsi3_put_pr_reg(pr_reg
);
2439 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2442 * See if we have an existing PR reservation holder pointer at
2443 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2446 spin_lock(&dev
->dev_reservation_lock
);
2447 pr_res_holder
= dev
->dev_pr_res_holder
;
2448 if ((pr_res_holder
)) {
2450 * From spc4r17 Section 5.7.9: Reserving:
2452 * If the device server receives a PERSISTENT RESERVE OUT
2453 * command from an I_T nexus other than a persistent reservation
2454 * holder (see 5.7.10) that attempts to create a persistent
2455 * reservation when a persistent reservation already exists for
2456 * the logical unit, then the command shall be completed with
2457 * RESERVATION CONFLICT status.
2459 if (pr_res_holder
!= pr_reg
) {
2460 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2461 pr_err("SPC-3 PR: Attempted RESERVE from"
2462 " [%s]: %s while reservation already held by"
2463 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2464 cmd
->se_tfo
->get_fabric_name(),
2465 se_sess
->se_node_acl
->initiatorname
,
2466 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2467 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2469 spin_unlock(&dev
->dev_reservation_lock
);
2470 core_scsi3_put_pr_reg(pr_reg
);
2471 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2474 * From spc4r17 Section 5.7.9: Reserving:
2476 * If a persistent reservation holder attempts to modify the
2477 * type or scope of an existing persistent reservation, the
2478 * command shall be completed with RESERVATION CONFLICT status.
2480 if ((pr_res_holder
->pr_res_type
!= type
) ||
2481 (pr_res_holder
->pr_res_scope
!= scope
)) {
2482 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2483 pr_err("SPC-3 PR: Attempted RESERVE from"
2484 " [%s]: %s trying to change TYPE and/or SCOPE,"
2485 " while reservation already held by [%s]: %s,"
2486 " returning RESERVATION_CONFLICT\n",
2487 cmd
->se_tfo
->get_fabric_name(),
2488 se_sess
->se_node_acl
->initiatorname
,
2489 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2490 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2492 spin_unlock(&dev
->dev_reservation_lock
);
2493 core_scsi3_put_pr_reg(pr_reg
);
2494 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2497 * From spc4r17 Section 5.7.9: Reserving:
2499 * If the device server receives a PERSISTENT RESERVE OUT
2500 * command with RESERVE service action where the TYPE field and
2501 * the SCOPE field contain the same values as the existing type
2502 * and scope from a persistent reservation holder, it shall not
2503 * make any change to the existing persistent reservation and
2504 * shall completethe command with GOOD status.
2506 spin_unlock(&dev
->dev_reservation_lock
);
2507 core_scsi3_put_pr_reg(pr_reg
);
2508 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2511 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2512 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2514 pr_reg
->pr_res_scope
= scope
;
2515 pr_reg
->pr_res_type
= type
;
2516 pr_reg
->pr_res_holder
= 1;
2517 dev
->dev_pr_res_holder
= pr_reg
;
2518 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2519 PR_REG_ISID_ID_LEN
);
2521 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2522 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2523 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2524 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2525 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2526 cmd
->se_tfo
->get_fabric_name(),
2527 se_sess
->se_node_acl
->initiatorname
,
2528 (prf_isid
) ? &i_buf
[0] : "");
2529 spin_unlock(&dev
->dev_reservation_lock
);
2531 if (pr_tmpl
->pr_aptpl_active
) {
2532 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2533 &pr_reg
->pr_aptpl_buf
[0],
2534 pr_tmpl
->pr_aptpl_buf_len
);
2536 pr_debug("SPC-3 PR: Updated APTPL metadata"
2540 core_scsi3_put_pr_reg(pr_reg
);
2544 static int core_scsi3_emulate_pro_reserve(
2550 struct se_device
*dev
= cmd
->se_dev
;
2554 case PR_TYPE_WRITE_EXCLUSIVE
:
2555 case PR_TYPE_EXCLUSIVE_ACCESS
:
2556 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2557 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2558 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2559 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2560 ret
= core_scsi3_pro_reserve(cmd
, dev
, type
, scope
, res_key
);
2563 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2565 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
2572 * Called with struct se_device->dev_reservation_lock held.
2574 static void __core_scsi3_complete_pro_release(
2575 struct se_device
*dev
,
2576 struct se_node_acl
*se_nacl
,
2577 struct t10_pr_registration
*pr_reg
,
2580 struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2581 char i_buf
[PR_REG_ISID_ID_LEN
];
2584 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2585 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2586 PR_REG_ISID_ID_LEN
);
2588 * Go ahead and release the current PR reservation holder.
2590 dev
->dev_pr_res_holder
= NULL
;
2592 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2593 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2594 tfo
->get_fabric_name(), (explict
) ? "explict" : "implict",
2595 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
2596 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2597 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2598 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2599 (prf_isid
) ? &i_buf
[0] : "");
2601 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2603 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2606 static int core_scsi3_emulate_pro_release(
2612 struct se_device
*dev
= cmd
->se_dev
;
2613 struct se_session
*se_sess
= cmd
->se_sess
;
2614 struct se_lun
*se_lun
= cmd
->se_lun
;
2615 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2616 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2617 int ret
, all_reg
= 0;
2619 if (!se_sess
|| !se_lun
) {
2620 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2621 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2624 * Locate the existing *pr_reg via struct se_node_acl pointers
2626 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2628 pr_err("SPC-3 PR: Unable to locate"
2629 " PR_REGISTERED *pr_reg for RELEASE\n");
2630 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2633 * From spc4r17 Section 5.7.11.2 Releasing:
2635 * If there is no persistent reservation or in response to a persistent
2636 * reservation release request from a registered I_T nexus that is not a
2637 * persistent reservation holder (see 5.7.10), the device server shall
2640 * a) Not release the persistent reservation, if any;
2641 * b) Not remove any registrations; and
2642 * c) Complete the command with GOOD status.
2644 spin_lock(&dev
->dev_reservation_lock
);
2645 pr_res_holder
= dev
->dev_pr_res_holder
;
2646 if (!pr_res_holder
) {
2648 * No persistent reservation, return GOOD status.
2650 spin_unlock(&dev
->dev_reservation_lock
);
2651 core_scsi3_put_pr_reg(pr_reg
);
2652 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2654 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2655 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
2658 if ((all_reg
== 0) && (pr_res_holder
!= pr_reg
)) {
2660 * Non 'All Registrants' PR Type cases..
2661 * Release request from a registered I_T nexus that is not a
2662 * persistent reservation holder. return GOOD status.
2664 spin_unlock(&dev
->dev_reservation_lock
);
2665 core_scsi3_put_pr_reg(pr_reg
);
2666 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2669 * From spc4r17 Section 5.7.11.2 Releasing:
2671 * Only the persistent reservation holder (see 5.7.10) is allowed to
2672 * release a persistent reservation.
2674 * An application client releases the persistent reservation by issuing
2675 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2676 * an I_T nexus that is a persistent reservation holder with the
2677 * following parameters:
2679 * a) RESERVATION KEY field set to the value of the reservation key
2680 * that is registered with the logical unit for the I_T nexus;
2682 if (res_key
!= pr_reg
->pr_res_key
) {
2683 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2684 " does not match existing SA REGISTER res_key:"
2685 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2686 spin_unlock(&dev
->dev_reservation_lock
);
2687 core_scsi3_put_pr_reg(pr_reg
);
2688 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2691 * From spc4r17 Section 5.7.11.2 Releasing and above:
2693 * b) TYPE field and SCOPE field set to match the persistent
2694 * reservation being released.
2696 if ((pr_res_holder
->pr_res_type
!= type
) ||
2697 (pr_res_holder
->pr_res_scope
!= scope
)) {
2698 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2699 pr_err("SPC-3 PR RELEASE: Attempted to release"
2700 " reservation from [%s]: %s with different TYPE "
2701 "and/or SCOPE while reservation already held by"
2702 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2703 cmd
->se_tfo
->get_fabric_name(),
2704 se_sess
->se_node_acl
->initiatorname
,
2705 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2706 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2708 spin_unlock(&dev
->dev_reservation_lock
);
2709 core_scsi3_put_pr_reg(pr_reg
);
2710 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2713 * In response to a persistent reservation release request from the
2714 * persistent reservation holder the device server shall perform a
2715 * release by doing the following as an uninterrupted series of actions:
2716 * a) Release the persistent reservation;
2717 * b) Not remove any registration(s);
2718 * c) If the released persistent reservation is a registrants only type
2719 * or all registrants type persistent reservation,
2720 * the device server shall establish a unit attention condition for
2721 * the initiator port associated with every regis-
2722 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2723 * RESERVE OUT command with RELEASE service action was received,
2724 * with the additional sense code set to RESERVATIONS RELEASED; and
2725 * d) If the persistent reservation is of any other type, the device
2726 * server shall not establish a unit attention condition.
2728 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2731 spin_unlock(&dev
->dev_reservation_lock
);
2733 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2734 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2735 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2736 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2738 * If no UNIT ATTENTION conditions will be established for
2739 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2740 * go ahead and check for APTPL=1 update+write below
2745 spin_lock(&pr_tmpl
->registration_lock
);
2746 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2749 * Do not establish a UNIT ATTENTION condition
2750 * for the calling I_T Nexus
2752 if (pr_reg_p
== pr_reg
)
2755 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2756 pr_reg_p
->pr_res_mapped_lun
,
2757 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2759 spin_unlock(&pr_tmpl
->registration_lock
);
2762 if (pr_tmpl
->pr_aptpl_active
) {
2763 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2764 &pr_reg
->pr_aptpl_buf
[0],
2765 pr_tmpl
->pr_aptpl_buf_len
);
2767 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2770 core_scsi3_put_pr_reg(pr_reg
);
2774 static int core_scsi3_emulate_pro_clear(
2778 struct se_device
*dev
= cmd
->se_dev
;
2779 struct se_node_acl
*pr_reg_nacl
;
2780 struct se_session
*se_sess
= cmd
->se_sess
;
2781 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2782 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2783 u32 pr_res_mapped_lun
= 0;
2784 int calling_it_nexus
= 0;
2786 * Locate the existing *pr_reg via struct se_node_acl pointers
2788 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2789 se_sess
->se_node_acl
, se_sess
);
2791 pr_err("SPC-3 PR: Unable to locate"
2792 " PR_REGISTERED *pr_reg for CLEAR\n");
2793 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2796 * From spc4r17 section 5.7.11.6, Clearing:
2798 * Any application client may release the persistent reservation and
2799 * remove all registrations from a device server by issuing a
2800 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2801 * registered I_T nexus with the following parameter:
2803 * a) RESERVATION KEY field set to the value of the reservation key
2804 * that is registered with the logical unit for the I_T nexus.
2806 if (res_key
!= pr_reg_n
->pr_res_key
) {
2807 pr_err("SPC-3 PR REGISTER: Received"
2808 " res_key: 0x%016Lx does not match"
2809 " existing SA REGISTER res_key:"
2810 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2811 core_scsi3_put_pr_reg(pr_reg_n
);
2812 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2815 * a) Release the persistent reservation, if any;
2817 spin_lock(&dev
->dev_reservation_lock
);
2818 pr_res_holder
= dev
->dev_pr_res_holder
;
2819 if (pr_res_holder
) {
2820 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2821 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2824 spin_unlock(&dev
->dev_reservation_lock
);
2826 * b) Remove all registration(s) (see spc4r17 5.7.7);
2828 spin_lock(&pr_tmpl
->registration_lock
);
2829 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2830 &pr_tmpl
->registration_list
, pr_reg_list
) {
2832 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2833 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2834 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2835 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2838 * e) Establish a unit attention condition for the initiator
2839 * port associated with every registered I_T nexus other
2840 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2841 * command with CLEAR service action was received, with the
2842 * additional sense code set to RESERVATIONS PREEMPTED.
2844 if (!calling_it_nexus
)
2845 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2846 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2848 spin_unlock(&pr_tmpl
->registration_lock
);
2850 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2851 cmd
->se_tfo
->get_fabric_name());
2853 if (pr_tmpl
->pr_aptpl_active
) {
2854 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2855 pr_debug("SPC-3 PR: Updated APTPL metadata"
2859 core_scsi3_pr_generation(dev
);
2864 * Called with struct se_device->dev_reservation_lock held.
2866 static void __core_scsi3_complete_pro_preempt(
2867 struct se_device
*dev
,
2868 struct t10_pr_registration
*pr_reg
,
2869 struct list_head
*preempt_and_abort_list
,
2874 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2875 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2876 char i_buf
[PR_REG_ISID_ID_LEN
];
2879 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2880 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2881 PR_REG_ISID_ID_LEN
);
2883 * Do an implict RELEASE of the existing reservation.
2885 if (dev
->dev_pr_res_holder
)
2886 __core_scsi3_complete_pro_release(dev
, nacl
,
2887 dev
->dev_pr_res_holder
, 0);
2889 dev
->dev_pr_res_holder
= pr_reg
;
2890 pr_reg
->pr_res_holder
= 1;
2891 pr_reg
->pr_res_type
= type
;
2892 pr_reg
->pr_res_scope
= scope
;
2894 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2895 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2896 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2897 core_scsi3_pr_dump_type(type
),
2898 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2899 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2900 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2901 nacl
->initiatorname
, (prf_isid
) ? &i_buf
[0] : "");
2903 * For PREEMPT_AND_ABORT, add the preempting reservation's
2904 * struct t10_pr_registration to the list that will be compared
2905 * against received CDBs..
2907 if (preempt_and_abort_list
)
2908 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2909 preempt_and_abort_list
);
2912 static void core_scsi3_release_preempt_and_abort(
2913 struct list_head
*preempt_and_abort_list
,
2914 struct t10_pr_registration
*pr_reg_holder
)
2916 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2918 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2919 pr_reg_abort_list
) {
2921 list_del(&pr_reg
->pr_reg_abort_list
);
2922 if (pr_reg_holder
== pr_reg
)
2924 if (pr_reg
->pr_res_holder
) {
2925 pr_warn("pr_reg->pr_res_holder still set\n");
2929 pr_reg
->pr_reg_deve
= NULL
;
2930 pr_reg
->pr_reg_nacl
= NULL
;
2931 kfree(pr_reg
->pr_aptpl_buf
);
2932 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2936 int core_scsi3_check_cdb_abort_and_preempt(
2937 struct list_head
*preempt_and_abort_list
,
2940 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2942 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2943 pr_reg_abort_list
) {
2944 if (pr_reg
->pr_res_key
== cmd
->pr_res_key
)
2951 static int core_scsi3_pro_preempt(
2959 struct se_device
*dev
= cmd
->se_dev
;
2960 struct se_dev_entry
*se_deve
;
2961 struct se_node_acl
*pr_reg_nacl
;
2962 struct se_session
*se_sess
= cmd
->se_sess
;
2963 struct list_head preempt_and_abort_list
;
2964 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2965 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
2966 u32 pr_res_mapped_lun
= 0;
2967 int all_reg
= 0, calling_it_nexus
= 0, released_regs
= 0;
2968 int prh_type
= 0, prh_scope
= 0, ret
;
2971 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2973 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2974 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2977 pr_err("SPC-3 PR: Unable to locate"
2978 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2979 (abort
) ? "_AND_ABORT" : "");
2980 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2982 if (pr_reg_n
->pr_res_key
!= res_key
) {
2983 core_scsi3_put_pr_reg(pr_reg_n
);
2984 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2986 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2987 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2988 core_scsi3_put_pr_reg(pr_reg_n
);
2989 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2991 INIT_LIST_HEAD(&preempt_and_abort_list
);
2993 spin_lock(&dev
->dev_reservation_lock
);
2994 pr_res_holder
= dev
->dev_pr_res_holder
;
2995 if (pr_res_holder
&&
2996 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2997 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
3000 if (!all_reg
&& !sa_res_key
) {
3001 spin_unlock(&dev
->dev_reservation_lock
);
3002 core_scsi3_put_pr_reg(pr_reg_n
);
3003 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3006 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3008 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3009 * persistent reservation holder or there is no persistent reservation
3010 * holder (i.e., there is no persistent reservation), then the device
3011 * server shall perform a preempt by doing the following in an
3012 * uninterrupted series of actions. (See below..)
3014 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
3016 * No existing or SA Reservation Key matching reservations..
3018 * PROUT SA PREEMPT with All Registrant type reservations are
3019 * allowed to be processed without a matching SA Reservation Key
3021 spin_lock(&pr_tmpl
->registration_lock
);
3022 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3023 &pr_tmpl
->registration_list
, pr_reg_list
) {
3025 * Removing of registrations in non all registrants
3026 * type reservations without a matching SA reservation
3029 * a) Remove the registrations for all I_T nexuses
3030 * specified by the SERVICE ACTION RESERVATION KEY
3032 * b) Ignore the contents of the SCOPE and TYPE fields;
3033 * c) Process tasks as defined in 5.7.1; and
3034 * d) Establish a unit attention condition for the
3035 * initiator port associated with every I_T nexus
3036 * that lost its registration other than the I_T
3037 * nexus on which the PERSISTENT RESERVE OUT command
3038 * was received, with the additional sense code set
3039 * to REGISTRATIONS PREEMPTED.
3042 if (pr_reg
->pr_res_key
!= sa_res_key
)
3045 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3046 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3047 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3048 __core_scsi3_free_registration(dev
, pr_reg
,
3049 (abort
) ? &preempt_and_abort_list
:
3050 NULL
, calling_it_nexus
);
3054 * Case for any existing all registrants type
3055 * reservation, follow logic in spc4r17 section
3056 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3058 * For a ZERO SA Reservation key, release
3059 * all other registrations and do an implict
3060 * release of active persistent reservation.
3062 * For a non-ZERO SA Reservation key, only
3063 * release the matching reservation key from
3067 (pr_reg
->pr_res_key
!= sa_res_key
))
3070 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3071 if (calling_it_nexus
)
3074 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3075 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3076 __core_scsi3_free_registration(dev
, pr_reg
,
3077 (abort
) ? &preempt_and_abort_list
:
3081 if (!calling_it_nexus
)
3082 core_scsi3_ua_allocate(pr_reg_nacl
,
3083 pr_res_mapped_lun
, 0x2A,
3084 ASCQ_2AH_RESERVATIONS_PREEMPTED
);
3086 spin_unlock(&pr_tmpl
->registration_lock
);
3088 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3089 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3090 * RESERVATION KEY field to a value that does not match any
3091 * registered reservation key, then the device server shall
3092 * complete the command with RESERVATION CONFLICT status.
3094 if (!released_regs
) {
3095 spin_unlock(&dev
->dev_reservation_lock
);
3096 core_scsi3_put_pr_reg(pr_reg_n
);
3097 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
3100 * For an existing all registrants type reservation
3101 * with a zero SA rservation key, preempt the existing
3102 * reservation with the new PR type and scope.
3104 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
3105 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3106 (abort
) ? &preempt_and_abort_list
: NULL
,
3107 type
, scope
, abort
);
3110 core_scsi3_release_preempt_and_abort(
3111 &preempt_and_abort_list
, pr_reg_n
);
3113 spin_unlock(&dev
->dev_reservation_lock
);
3115 if (pr_tmpl
->pr_aptpl_active
) {
3116 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3117 &pr_reg_n
->pr_aptpl_buf
[0],
3118 pr_tmpl
->pr_aptpl_buf_len
);
3120 pr_debug("SPC-3 PR: Updated APTPL"
3121 " metadata for PREEMPT%s\n", (abort
) ?
3125 core_scsi3_put_pr_reg(pr_reg_n
);
3126 core_scsi3_pr_generation(cmd
->se_dev
);
3130 * The PREEMPTing SA reservation key matches that of the
3131 * existing persistent reservation, first, we check if
3132 * we are preempting our own reservation.
3133 * From spc4r17, section 5.7.11.4.3 Preempting
3134 * persistent reservations and registration handling
3136 * If an all registrants persistent reservation is not
3137 * present, it is not an error for the persistent
3138 * reservation holder to preempt itself (i.e., a
3139 * PERSISTENT RESERVE OUT with a PREEMPT service action
3140 * or a PREEMPT AND ABORT service action with the
3141 * SERVICE ACTION RESERVATION KEY value equal to the
3142 * persistent reservation holder's reservation key that
3143 * is received from the persistent reservation holder).
3144 * In that case, the device server shall establish the
3145 * new persistent reservation and maintain the
3148 prh_type
= pr_res_holder
->pr_res_type
;
3149 prh_scope
= pr_res_holder
->pr_res_scope
;
3151 * If the SERVICE ACTION RESERVATION KEY field identifies a
3152 * persistent reservation holder (see 5.7.10), the device
3153 * server shall perform a preempt by doing the following as
3154 * an uninterrupted series of actions:
3156 * a) Release the persistent reservation for the holder
3157 * identified by the SERVICE ACTION RESERVATION KEY field;
3159 if (pr_reg_n
!= pr_res_holder
)
3160 __core_scsi3_complete_pro_release(dev
,
3161 pr_res_holder
->pr_reg_nacl
,
3162 dev
->dev_pr_res_holder
, 0);
3164 * b) Remove the registrations for all I_T nexuses identified
3165 * by the SERVICE ACTION RESERVATION KEY field, except the
3166 * I_T nexus that is being used for the PERSISTENT RESERVE
3167 * OUT command. If an all registrants persistent reservation
3168 * is present and the SERVICE ACTION RESERVATION KEY field
3169 * is set to zero, then all registrations shall be removed
3170 * except for that of the I_T nexus that is being used for
3171 * the PERSISTENT RESERVE OUT command;
3173 spin_lock(&pr_tmpl
->registration_lock
);
3174 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3175 &pr_tmpl
->registration_list
, pr_reg_list
) {
3177 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3178 if (calling_it_nexus
)
3181 if (pr_reg
->pr_res_key
!= sa_res_key
)
3184 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3185 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3186 __core_scsi3_free_registration(dev
, pr_reg
,
3187 (abort
) ? &preempt_and_abort_list
: NULL
,
3190 * e) Establish a unit attention condition for the initiator
3191 * port associated with every I_T nexus that lost its
3192 * persistent reservation and/or registration, with the
3193 * additional sense code set to REGISTRATIONS PREEMPTED;
3195 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3196 ASCQ_2AH_RESERVATIONS_PREEMPTED
);
3198 spin_unlock(&pr_tmpl
->registration_lock
);
3200 * c) Establish a persistent reservation for the preempting
3201 * I_T nexus using the contents of the SCOPE and TYPE fields;
3203 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3204 (abort
) ? &preempt_and_abort_list
: NULL
,
3205 type
, scope
, abort
);
3207 * d) Process tasks as defined in 5.7.1;
3209 * f) If the type or scope has changed, then for every I_T nexus
3210 * whose reservation key was not removed, except for the I_T
3211 * nexus on which the PERSISTENT RESERVE OUT command was
3212 * received, the device server shall establish a unit
3213 * attention condition for the initiator port associated with
3214 * that I_T nexus, with the additional sense code set to
3215 * RESERVATIONS RELEASED. If the type or scope have not
3216 * changed, then no unit attention condition(s) shall be
3217 * established for this reason.
3219 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3220 spin_lock(&pr_tmpl
->registration_lock
);
3221 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3222 &pr_tmpl
->registration_list
, pr_reg_list
) {
3224 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3225 if (calling_it_nexus
)
3228 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3229 pr_reg
->pr_res_mapped_lun
, 0x2A,
3230 ASCQ_2AH_RESERVATIONS_RELEASED
);
3232 spin_unlock(&pr_tmpl
->registration_lock
);
3234 spin_unlock(&dev
->dev_reservation_lock
);
3236 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3237 * All received CDBs for the matching existing reservation and
3238 * registrations undergo ABORT_TASK logic.
3240 * From there, core_scsi3_release_preempt_and_abort() will
3241 * release every registration in the list (which have already
3242 * been removed from the primary pr_reg list), except the
3243 * new persistent reservation holder, the calling Initiator Port.
3246 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3247 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3251 if (pr_tmpl
->pr_aptpl_active
) {
3252 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3253 &pr_reg_n
->pr_aptpl_buf
[0],
3254 pr_tmpl
->pr_aptpl_buf_len
);
3256 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3257 "%s\n", (abort
) ? "_AND_ABORT" : "");
3260 core_scsi3_put_pr_reg(pr_reg_n
);
3261 core_scsi3_pr_generation(cmd
->se_dev
);
3265 static int core_scsi3_emulate_pro_preempt(
3276 case PR_TYPE_WRITE_EXCLUSIVE
:
3277 case PR_TYPE_EXCLUSIVE_ACCESS
:
3278 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3279 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3280 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3281 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3282 ret
= core_scsi3_pro_preempt(cmd
, type
, scope
,
3283 res_key
, sa_res_key
, abort
);
3286 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3287 " Type: 0x%02x\n", (abort
) ? "_AND_ABORT" : "", type
);
3288 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3295 static int core_scsi3_emulate_pro_register_and_move(
3302 struct se_session
*se_sess
= cmd
->se_sess
;
3303 struct se_device
*dev
= cmd
->se_dev
;
3304 struct se_dev_entry
*se_deve
, *dest_se_deve
= NULL
;
3305 struct se_lun
*se_lun
= cmd
->se_lun
;
3306 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3307 struct se_port
*se_port
;
3308 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3309 struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3310 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3311 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
3313 unsigned char *initiator_str
;
3314 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
3315 u32 tid_len
, tmp_tid_len
;
3316 int new_reg
= 0, type
, scope
, ret
, matching_iname
, prf_isid
;
3317 unsigned short rtpi
;
3318 unsigned char proto_ident
;
3320 if (!se_sess
|| !se_lun
) {
3321 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3322 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3324 memset(dest_iport
, 0, 64);
3325 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3326 se_tpg
= se_sess
->se_tpg
;
3327 tf_ops
= se_tpg
->se_tpg_tfo
;
3328 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
3330 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3331 * Register behaviors for a REGISTER AND MOVE service action
3333 * Locate the existing *pr_reg via struct se_node_acl pointers
3335 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3338 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3339 " *pr_reg for REGISTER_AND_MOVE\n");
3340 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3343 * The provided reservation key much match the existing reservation key
3344 * provided during this initiator's I_T nexus registration.
3346 if (res_key
!= pr_reg
->pr_res_key
) {
3347 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3348 " res_key: 0x%016Lx does not match existing SA REGISTER"
3349 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3350 core_scsi3_put_pr_reg(pr_reg
);
3351 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
3354 * The service active reservation key needs to be non zero
3357 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3359 core_scsi3_put_pr_reg(pr_reg
);
3360 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3364 * Determine the Relative Target Port Identifier where the reservation
3365 * will be moved to for the TransportID containing SCSI initiator WWN
3368 buf
= transport_kmap_first_data_page(cmd
);
3369 rtpi
= (buf
[18] & 0xff) << 8;
3370 rtpi
|= buf
[19] & 0xff;
3371 tid_len
= (buf
[20] & 0xff) << 24;
3372 tid_len
|= (buf
[21] & 0xff) << 16;
3373 tid_len
|= (buf
[22] & 0xff) << 8;
3374 tid_len
|= buf
[23] & 0xff;
3375 transport_kunmap_first_data_page(cmd
);
3378 if ((tid_len
+ 24) != cmd
->data_length
) {
3379 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3380 " does not equal CDB data_length: %u\n", tid_len
,
3382 core_scsi3_put_pr_reg(pr_reg
);
3383 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3386 spin_lock(&dev
->se_port_lock
);
3387 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3388 if (se_port
->sep_rtpi
!= rtpi
)
3390 dest_se_tpg
= se_port
->sep_tpg
;
3393 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3397 atomic_inc(&dest_se_tpg
->tpg_pr_ref_count
);
3398 smp_mb__after_atomic_inc();
3399 spin_unlock(&dev
->se_port_lock
);
3401 ret
= core_scsi3_tpg_depend_item(dest_se_tpg
);
3403 pr_err("core_scsi3_tpg_depend_item() failed"
3404 " for dest_se_tpg\n");
3405 atomic_dec(&dest_se_tpg
->tpg_pr_ref_count
);
3406 smp_mb__after_atomic_dec();
3407 core_scsi3_put_pr_reg(pr_reg
);
3408 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3411 spin_lock(&dev
->se_port_lock
);
3414 spin_unlock(&dev
->se_port_lock
);
3416 if (!dest_se_tpg
|| !dest_tf_ops
) {
3417 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3418 " fabric ops from Relative Target Port Identifier:"
3420 core_scsi3_put_pr_reg(pr_reg
);
3421 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3424 buf
= transport_kmap_first_data_page(cmd
);
3425 proto_ident
= (buf
[24] & 0x0f);
3427 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3428 " 0x%02x\n", proto_ident
);
3430 if (proto_ident
!= dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
)) {
3431 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3432 " proto_ident: 0x%02x does not match ident: 0x%02x"
3433 " from fabric: %s\n", proto_ident
,
3434 dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
),
3435 dest_tf_ops
->get_fabric_name());
3436 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3439 if (dest_tf_ops
->tpg_parse_pr_out_transport_id
== NULL
) {
3440 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3441 " containg a valid tpg_parse_pr_out_transport_id"
3442 " function pointer\n");
3443 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
3446 initiator_str
= dest_tf_ops
->tpg_parse_pr_out_transport_id(dest_se_tpg
,
3447 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3448 if (!initiator_str
) {
3449 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3450 " initiator_str from Transport ID\n");
3451 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3455 transport_kunmap_first_data_page(cmd
);
3458 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3459 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3460 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3463 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3464 * action specifies a TransportID that is the same as the initiator port
3465 * of the I_T nexus for the command received, then the command shall
3466 * be terminated with CHECK CONDITION status, with the sense key set to
3467 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3468 * IN PARAMETER LIST.
3470 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3471 matching_iname
= (!strcmp(initiator_str
,
3472 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3473 if (!matching_iname
)
3474 goto after_iport_check
;
3476 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3477 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3478 " matches: %s on received I_T Nexus\n", initiator_str
,
3479 pr_reg_nacl
->initiatorname
);
3480 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3483 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3484 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3485 " matches: %s %s on received I_T Nexus\n",
3486 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3487 pr_reg
->pr_reg_isid
);
3488 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3493 * Locate the destination struct se_node_acl from the received Transport ID
3495 spin_lock_irq(&dest_se_tpg
->acl_node_lock
);
3496 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3498 if (dest_node_acl
) {
3499 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
3500 smp_mb__after_atomic_inc();
3502 spin_unlock_irq(&dest_se_tpg
->acl_node_lock
);
3504 if (!dest_node_acl
) {
3505 pr_err("Unable to locate %s dest_node_acl for"
3506 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3508 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3511 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
3513 pr_err("core_scsi3_nodeacl_depend_item() for"
3514 " dest_node_acl\n");
3515 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
3516 smp_mb__after_atomic_dec();
3517 dest_node_acl
= NULL
;
3518 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
3522 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3523 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3524 dest_node_acl
->initiatorname
);
3527 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3530 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3531 if (!dest_se_deve
) {
3532 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3533 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3534 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3538 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
3540 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3541 atomic_dec(&dest_se_deve
->pr_ref_count
);
3542 smp_mb__after_atomic_dec();
3543 dest_se_deve
= NULL
;
3544 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
3548 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3549 " ACL for dest_se_deve->mapped_lun: %u\n",
3550 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3551 dest_se_deve
->mapped_lun
);
3554 * A persistent reservation needs to already existing in order to
3555 * successfully complete the REGISTER_AND_MOVE service action..
3557 spin_lock(&dev
->dev_reservation_lock
);
3558 pr_res_holder
= dev
->dev_pr_res_holder
;
3559 if (!pr_res_holder
) {
3560 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3561 " currently held\n");
3562 spin_unlock(&dev
->dev_reservation_lock
);
3563 ret
= PYX_TRANSPORT_INVALID_CDB_FIELD
;
3567 * The received on I_T Nexus must be the reservation holder.
3569 * From spc4r17 section 5.7.8 Table 50 --
3570 * Register behaviors for a REGISTER AND MOVE service action
3572 if (pr_res_holder
!= pr_reg
) {
3573 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3574 " Nexus is not reservation holder\n");
3575 spin_unlock(&dev
->dev_reservation_lock
);
3576 ret
= PYX_TRANSPORT_RESERVATION_CONFLICT
;
3580 * From spc4r17 section 5.7.8: registering and moving reservation
3582 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3583 * action is received and the established persistent reservation is a
3584 * Write Exclusive - All Registrants type or Exclusive Access -
3585 * All Registrants type reservation, then the command shall be completed
3586 * with RESERVATION CONFLICT status.
3588 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3589 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3590 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3591 " reservation for type: %s\n",
3592 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3593 spin_unlock(&dev
->dev_reservation_lock
);
3594 ret
= PYX_TRANSPORT_RESERVATION_CONFLICT
;
3597 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3599 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3601 type
= pr_res_holder
->pr_res_type
;
3602 scope
= pr_res_holder
->pr_res_type
;
3604 * c) Associate the reservation key specified in the SERVICE ACTION
3605 * RESERVATION KEY field with the I_T nexus specified as the
3606 * destination of the register and move, where:
3607 * A) The I_T nexus is specified by the TransportID and the
3608 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3609 * B) Regardless of the TransportID format used, the association for
3610 * the initiator port is based on either the initiator port name
3611 * (see 3.1.71) on SCSI transport protocols where port names are
3612 * required or the initiator port identifier (see 3.1.70) on SCSI
3613 * transport protocols where port names are not required;
3614 * d) Register the reservation key specified in the SERVICE ACTION
3615 * RESERVATION KEY field;
3616 * e) Retain the reservation key specified in the SERVICE ACTION
3617 * RESERVATION KEY field and associated information;
3619 * Also, It is not an error for a REGISTER AND MOVE service action to
3620 * register an I_T nexus that is already registered with the same
3621 * reservation key or a different reservation key.
3623 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3626 ret
= core_scsi3_alloc_registration(cmd
->se_dev
,
3627 dest_node_acl
, dest_se_deve
, iport_ptr
,
3628 sa_res_key
, 0, aptpl
, 2, 1);
3630 spin_unlock(&dev
->dev_reservation_lock
);
3631 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3634 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3639 * f) Release the persistent reservation for the persistent reservation
3640 * holder (i.e., the I_T nexus on which the
3642 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3643 dev
->dev_pr_res_holder
, 0);
3645 * g) Move the persistent reservation to the specified I_T nexus using
3646 * the same scope and type as the persistent reservation released in
3649 dev
->dev_pr_res_holder
= dest_pr_reg
;
3650 dest_pr_reg
->pr_res_holder
= 1;
3651 dest_pr_reg
->pr_res_type
= type
;
3652 pr_reg
->pr_res_scope
= scope
;
3653 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
3654 PR_REG_ISID_ID_LEN
);
3656 * Increment PRGeneration for existing registrations..
3659 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3660 spin_unlock(&dev
->dev_reservation_lock
);
3662 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3663 " created new reservation holder TYPE: %s on object RTPI:"
3664 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3665 core_scsi3_pr_dump_type(type
), rtpi
,
3666 dest_pr_reg
->pr_res_generation
);
3667 pr_debug("SPC-3 PR Successfully moved reservation from"
3668 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3669 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3670 (prf_isid
) ? &i_buf
[0] : "", dest_tf_ops
->get_fabric_name(),
3671 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3674 * It is now safe to release configfs group dependencies for destination
3675 * of Transport ID Initiator Device/Port Identifier
3677 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3678 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3679 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3681 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3682 * nexus on which PERSISTENT RESERVE OUT command was received.
3685 spin_lock(&pr_tmpl
->registration_lock
);
3686 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3687 spin_unlock(&pr_tmpl
->registration_lock
);
3689 core_scsi3_put_pr_reg(pr_reg
);
3692 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3693 * write out the updated metadata to struct file for this SCSI device.
3696 pr_tmpl
->pr_aptpl_active
= 0;
3697 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
3698 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3699 " REGISTER_AND_MOVE\n");
3701 pr_tmpl
->pr_aptpl_active
= 1;
3702 ret
= core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3703 &dest_pr_reg
->pr_aptpl_buf
[0],
3704 pr_tmpl
->pr_aptpl_buf_len
);
3706 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3707 " REGISTER_AND_MOVE\n");
3710 transport_kunmap_first_data_page(cmd
);
3712 core_scsi3_put_pr_reg(dest_pr_reg
);
3716 transport_kunmap_first_data_page(cmd
);
3718 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3720 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3721 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3722 core_scsi3_put_pr_reg(pr_reg
);
3726 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3728 unsigned int __v1
, __v2
;
3730 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3731 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3733 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3737 * See spc4r17 section 6.14 Table 170
3739 int target_scsi3_emulate_pr_out(struct se_task
*task
)
3741 struct se_cmd
*cmd
= task
->task_se_cmd
;
3742 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3744 u64 res_key
, sa_res_key
;
3745 int sa
, scope
, type
, aptpl
;
3746 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3749 * Following spc2r20 5.5.1 Reservations overview:
3751 * If a logical unit has been reserved by any RESERVE command and is
3752 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3753 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3754 * initiator or service action and shall terminate with a RESERVATION
3757 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
) {
3758 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3759 " SPC-2 reservation is held, returning"
3760 " RESERVATION_CONFLICT\n");
3761 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
3765 * FIXME: A NULL struct se_session pointer means an this is not coming from
3766 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3769 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3771 if (cmd
->data_length
< 24) {
3772 pr_warn("SPC-PR: Received PR OUT parameter list"
3773 " length too small: %u\n", cmd
->data_length
);
3774 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3777 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3779 sa
= (cdb
[1] & 0x1f);
3780 scope
= (cdb
[2] & 0xf0);
3781 type
= (cdb
[2] & 0x0f);
3783 buf
= transport_kmap_first_data_page(cmd
);
3785 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3787 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3788 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3790 * REGISTER_AND_MOVE uses a different SA parameter list containing
3791 * SCSI TransportIDs.
3793 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3794 spec_i_pt
= (buf
[20] & 0x08);
3795 all_tg_pt
= (buf
[20] & 0x04);
3796 aptpl
= (buf
[20] & 0x01);
3798 aptpl
= (buf
[17] & 0x01);
3799 unreg
= (buf
[17] & 0x02);
3801 transport_kunmap_first_data_page(cmd
);
3805 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3807 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
))
3808 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3810 * From spc4r17 section 6.14:
3812 * If the SPEC_I_PT bit is set to zero, the service action is not
3813 * REGISTER AND MOVE, and the parameter list length is not 24, then
3814 * the command shall be terminated with CHECK CONDITION status, with
3815 * the sense key set to ILLEGAL REQUEST, and the additional sense
3816 * code set to PARAMETER LIST LENGTH ERROR.
3818 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3819 (cmd
->data_length
!= 24)) {
3820 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3821 " list length: %u\n", cmd
->data_length
);
3822 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3825 * (core_scsi3_emulate_pro_* function parameters
3826 * are defined by spc4r17 Table 174:
3827 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3831 return core_scsi3_emulate_pro_register(cmd
,
3832 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 0);
3834 return core_scsi3_emulate_pro_reserve(cmd
,
3835 type
, scope
, res_key
);
3837 return core_scsi3_emulate_pro_release(cmd
,
3838 type
, scope
, res_key
);
3840 return core_scsi3_emulate_pro_clear(cmd
, res_key
);
3842 return core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3843 res_key
, sa_res_key
, 0);
3844 case PRO_PREEMPT_AND_ABORT
:
3845 return core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3846 res_key
, sa_res_key
, 1);
3847 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3848 return core_scsi3_emulate_pro_register(cmd
,
3849 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 1);
3850 case PRO_REGISTER_AND_MOVE
:
3851 return core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3852 sa_res_key
, aptpl
, unreg
);
3854 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3855 " action: 0x%02x\n", cdb
[1] & 0x1f);
3856 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3859 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3863 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3865 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3867 static int core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3869 struct se_device
*se_dev
= cmd
->se_dev
;
3870 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
3871 struct t10_pr_registration
*pr_reg
;
3873 u32 add_len
= 0, off
= 8;
3875 if (cmd
->data_length
< 8) {
3876 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3877 " too small\n", cmd
->data_length
);
3878 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3881 buf
= transport_kmap_first_data_page(cmd
);
3882 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3883 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3884 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3885 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
3887 spin_lock(&su_dev
->t10_pr
.registration_lock
);
3888 list_for_each_entry(pr_reg
, &su_dev
->t10_pr
.registration_list
,
3891 * Check for overflow of 8byte PRI READ_KEYS payload and
3892 * next reservation key list descriptor.
3894 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3897 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3898 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3899 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3900 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3901 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3902 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3903 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3904 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3908 spin_unlock(&su_dev
->t10_pr
.registration_lock
);
3910 buf
[4] = ((add_len
>> 24) & 0xff);
3911 buf
[5] = ((add_len
>> 16) & 0xff);
3912 buf
[6] = ((add_len
>> 8) & 0xff);
3913 buf
[7] = (add_len
& 0xff);
3915 transport_kunmap_first_data_page(cmd
);
3921 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3923 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3925 static int core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
3927 struct se_device
*se_dev
= cmd
->se_dev
;
3928 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
3929 struct t10_pr_registration
*pr_reg
;
3932 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
3934 if (cmd
->data_length
< 8) {
3935 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3936 " too small\n", cmd
->data_length
);
3937 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3940 buf
= transport_kmap_first_data_page(cmd
);
3941 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3942 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3943 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3944 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
3946 spin_lock(&se_dev
->dev_reservation_lock
);
3947 pr_reg
= se_dev
->dev_pr_res_holder
;
3950 * Set the hardcoded Additional Length
3952 buf
[4] = ((add_len
>> 24) & 0xff);
3953 buf
[5] = ((add_len
>> 16) & 0xff);
3954 buf
[6] = ((add_len
>> 8) & 0xff);
3955 buf
[7] = (add_len
& 0xff);
3957 if (cmd
->data_length
< 22)
3961 * Set the Reservation key.
3963 * From spc4r17, section 5.7.10:
3964 * A persistent reservation holder has its reservation key
3965 * returned in the parameter data from a PERSISTENT
3966 * RESERVE IN command with READ RESERVATION service action as
3968 * a) For a persistent reservation of the type Write Exclusive
3969 * - All Registrants or Exclusive Access  All Regitrants,
3970 * the reservation key shall be set to zero; or
3971 * b) For all other persistent reservation types, the
3972 * reservation key shall be set to the registered
3973 * reservation key for the I_T nexus that holds the
3974 * persistent reservation.
3976 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3977 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
3980 pr_res_key
= pr_reg
->pr_res_key
;
3982 buf
[8] = ((pr_res_key
>> 56) & 0xff);
3983 buf
[9] = ((pr_res_key
>> 48) & 0xff);
3984 buf
[10] = ((pr_res_key
>> 40) & 0xff);
3985 buf
[11] = ((pr_res_key
>> 32) & 0xff);
3986 buf
[12] = ((pr_res_key
>> 24) & 0xff);
3987 buf
[13] = ((pr_res_key
>> 16) & 0xff);
3988 buf
[14] = ((pr_res_key
>> 8) & 0xff);
3989 buf
[15] = (pr_res_key
& 0xff);
3991 * Set the SCOPE and TYPE
3993 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
3994 (pr_reg
->pr_res_type
& 0x0f);
3998 spin_unlock(&se_dev
->dev_reservation_lock
);
3999 transport_kunmap_first_data_page(cmd
);
4005 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4007 * See spc4r17 section 6.13.4 Table 165
4009 static int core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
4011 struct se_device
*dev
= cmd
->se_dev
;
4012 struct t10_reservation
*pr_tmpl
= &dev
->se_sub_dev
->t10_pr
;
4014 u16 add_len
= 8; /* Hardcoded to 8. */
4016 if (cmd
->data_length
< 6) {
4017 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4018 " %u too small\n", cmd
->data_length
);
4019 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
4022 buf
= transport_kmap_first_data_page(cmd
);
4024 buf
[0] = ((add_len
<< 8) & 0xff);
4025 buf
[1] = (add_len
& 0xff);
4026 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4027 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4028 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4029 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4031 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4032 * set the TMV: Task Mask Valid bit.
4036 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4038 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4040 * PTPL_A: Persistence across Target Power Loss Active bit
4042 if (pr_tmpl
->pr_aptpl_active
)
4045 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4047 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4048 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4049 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4050 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4051 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4052 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4054 transport_kunmap_first_data_page(cmd
);
4060 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4062 * See spc4r17 section 6.13.5 Table 168 and 169
4064 static int core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
4066 struct se_device
*se_dev
= cmd
->se_dev
;
4067 struct se_node_acl
*se_nacl
;
4068 struct se_subsystem_dev
*su_dev
= se_dev
->se_sub_dev
;
4069 struct se_portal_group
*se_tpg
;
4070 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
4071 struct t10_reservation
*pr_tmpl
= &se_dev
->se_sub_dev
->t10_pr
;
4073 u32 add_desc_len
= 0, add_len
= 0, desc_len
, exp_desc_len
;
4074 u32 off
= 8; /* off into first Full Status descriptor */
4075 int format_code
= 0;
4077 if (cmd
->data_length
< 8) {
4078 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4079 " too small\n", cmd
->data_length
);
4080 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
4083 buf
= transport_kmap_first_data_page(cmd
);
4085 buf
[0] = ((su_dev
->t10_pr
.pr_generation
>> 24) & 0xff);
4086 buf
[1] = ((su_dev
->t10_pr
.pr_generation
>> 16) & 0xff);
4087 buf
[2] = ((su_dev
->t10_pr
.pr_generation
>> 8) & 0xff);
4088 buf
[3] = (su_dev
->t10_pr
.pr_generation
& 0xff);
4090 spin_lock(&pr_tmpl
->registration_lock
);
4091 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
4092 &pr_tmpl
->registration_list
, pr_reg_list
) {
4094 se_nacl
= pr_reg
->pr_reg_nacl
;
4095 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
4098 atomic_inc(&pr_reg
->pr_res_holders
);
4099 smp_mb__after_atomic_inc();
4100 spin_unlock(&pr_tmpl
->registration_lock
);
4102 * Determine expected length of $FABRIC_MOD specific
4103 * TransportID full status descriptor..
4105 exp_desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id_len(
4106 se_tpg
, se_nacl
, pr_reg
, &format_code
);
4108 if ((exp_desc_len
+ add_len
) > cmd
->data_length
) {
4109 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4110 " out of buffer: %d\n", cmd
->data_length
);
4111 spin_lock(&pr_tmpl
->registration_lock
);
4112 atomic_dec(&pr_reg
->pr_res_holders
);
4113 smp_mb__after_atomic_dec();
4117 * Set RESERVATION KEY
4119 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
4120 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
4121 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
4122 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4123 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4124 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4125 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4126 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4127 off
+= 4; /* Skip Over Reserved area */
4130 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4132 if (pr_reg
->pr_reg_all_tg_pt
)
4135 * The struct se_lun pointer will be present for the
4136 * reservation holder for PR_HOLDER bit.
4138 * Also, if this registration is the reservation
4139 * holder, fill in SCOPE and TYPE in the next byte.
4141 if (pr_reg
->pr_res_holder
) {
4143 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4144 (pr_reg
->pr_res_type
& 0x0f);
4148 off
+= 4; /* Skip over reserved area */
4150 * From spc4r17 6.3.15:
4152 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4153 * IDENTIFIER field contains the relative port identifier (see
4154 * 3.1.120) of the target port that is part of the I_T nexus
4155 * described by this full status descriptor. If the ALL_TG_PT
4156 * bit is set to one, the contents of the RELATIVE TARGET PORT
4157 * IDENTIFIER field are not defined by this standard.
4159 if (!pr_reg
->pr_reg_all_tg_pt
) {
4160 struct se_port
*port
= pr_reg
->pr_reg_tg_pt_lun
->lun_sep
;
4162 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
4163 buf
[off
++] = (port
->sep_rtpi
& 0xff);
4165 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4168 * Now, have the $FABRIC_MOD fill in the protocol identifier
4170 desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id(se_tpg
,
4171 se_nacl
, pr_reg
, &format_code
, &buf
[off
+4]);
4173 spin_lock(&pr_tmpl
->registration_lock
);
4174 atomic_dec(&pr_reg
->pr_res_holders
);
4175 smp_mb__after_atomic_dec();
4177 * Set the ADDITIONAL DESCRIPTOR LENGTH
4179 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4180 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4181 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4182 buf
[off
++] = (desc_len
& 0xff);
4184 * Size of full desctipor header minus TransportID
4185 * containing $FABRIC_MOD specific) initiator device/port
4188 * See spc4r17 Section 6.13.5 Table 169
4190 add_desc_len
= (24 + desc_len
);
4193 add_len
+= add_desc_len
;
4195 spin_unlock(&pr_tmpl
->registration_lock
);
4197 * Set ADDITIONAL_LENGTH
4199 buf
[4] = ((add_len
>> 24) & 0xff);
4200 buf
[5] = ((add_len
>> 16) & 0xff);
4201 buf
[6] = ((add_len
>> 8) & 0xff);
4202 buf
[7] = (add_len
& 0xff);
4204 transport_kunmap_first_data_page(cmd
);
4209 int target_scsi3_emulate_pr_in(struct se_task
*task
)
4211 struct se_cmd
*cmd
= task
->task_se_cmd
;
4214 * Following spc2r20 5.5.1 Reservations overview:
4216 * If a logical unit has been reserved by any RESERVE command and is
4217 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4218 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4219 * initiator or service action and shall terminate with a RESERVATION
4222 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
) {
4223 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4224 " SPC-2 reservation is held, returning"
4225 " RESERVATION_CONFLICT\n");
4226 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
4229 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4231 return core_scsi3_pri_read_keys(cmd
);
4232 case PRI_READ_RESERVATION
:
4233 return core_scsi3_pri_read_reservation(cmd
);
4234 case PRI_REPORT_CAPABILITIES
:
4235 return core_scsi3_pri_report_capabilities(cmd
);
4236 case PRI_READ_FULL_STATUS
:
4237 return core_scsi3_pri_read_full_status(cmd
);
4239 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4240 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4241 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
4245 static int core_pt_reservation_check(struct se_cmd
*cmd
, u32
*pr_res_type
)
4250 static int core_pt_seq_non_holder(
4258 int core_setup_reservations(struct se_device
*dev
, int force_pt
)
4260 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
4261 struct t10_reservation
*rest
= &su_dev
->t10_pr
;
4263 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4264 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4265 * cause a problem because libata and some SATA RAID HBAs appear
4266 * under Linux/SCSI, but to emulate reservations themselves.
4268 if (((dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
4269 !(dev
->se_sub_dev
->se_dev_attrib
.emulate_reservations
)) || force_pt
) {
4270 rest
->res_type
= SPC_PASSTHROUGH
;
4271 rest
->pr_ops
.t10_reservation_check
= &core_pt_reservation_check
;
4272 rest
->pr_ops
.t10_seq_non_holder
= &core_pt_seq_non_holder
;
4273 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4274 " emulation\n", dev
->transport
->name
);
4278 * If SPC-3 or above is reported by real or emulated struct se_device,
4279 * use emulated Persistent Reservations.
4281 if (dev
->transport
->get_device_rev(dev
) >= SCSI_3
) {
4282 rest
->res_type
= SPC3_PERSISTENT_RESERVATIONS
;
4283 rest
->pr_ops
.t10_reservation_check
= &core_scsi3_pr_reservation_check
;
4284 rest
->pr_ops
.t10_seq_non_holder
= &core_scsi3_pr_seq_non_holder
;
4285 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4286 " emulation\n", dev
->transport
->name
);
4288 rest
->res_type
= SPC2_RESERVATIONS
;
4289 rest
->pr_ops
.t10_reservation_check
= &core_scsi2_reservation_check
;
4290 rest
->pr_ops
.t10_seq_non_holder
=
4291 &core_scsi2_reservation_seq_non_holder
;
4292 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4293 dev
->transport
->name
);