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/version.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/list.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <asm/unaligned.h>
36 #include <target/target_core_base.h>
37 #include <target/target_core_device.h>
38 #include <target/target_core_tmr.h>
39 #include <target/target_core_tpg.h>
40 #include <target/target_core_transport.h>
41 #include <target/target_core_fabric_ops.h>
42 #include <target/target_core_configfs.h>
44 #include "target_core_hba.h"
45 #include "target_core_pr.h"
46 #include "target_core_ua.h"
49 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
51 struct pr_transport_id_holder
{
53 struct t10_pr_registration
*dest_pr_reg
;
54 struct se_portal_group
*dest_tpg
;
55 struct se_node_acl
*dest_node_acl
;
56 struct se_dev_entry
*dest_se_deve
;
57 struct list_head dest_list
;
60 int core_pr_dump_initiator_port(
61 struct t10_pr_registration
*pr_reg
,
65 if (!(pr_reg
->isid_present_at_reg
))
68 snprintf(buf
, size
, ",i,0x%s", &pr_reg
->pr_reg_isid
[0]);
72 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
73 struct t10_pr_registration
*, int);
75 static int core_scsi2_reservation_seq_non_holder(
92 static int core_scsi2_reservation_check(struct se_cmd
*cmd
, u32
*pr_reg_type
)
94 struct se_device
*dev
= cmd
->se_dev
;
95 struct se_session
*sess
= cmd
->se_sess
;
101 spin_lock(&dev
->dev_reservation_lock
);
102 if (!dev
->dev_reserved_node_acl
|| !sess
) {
103 spin_unlock(&dev
->dev_reservation_lock
);
106 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
) {
107 spin_unlock(&dev
->dev_reservation_lock
);
110 if (!(dev
->dev_flags
& DF_SPC2_RESERVATIONS_WITH_ISID
)) {
111 spin_unlock(&dev
->dev_reservation_lock
);
114 ret
= (dev
->dev_res_bin_isid
== sess
->sess_bin_isid
) ? 0 : -1;
115 spin_unlock(&dev
->dev_reservation_lock
);
120 static int core_scsi2_reservation_release(struct se_cmd
*cmd
)
122 struct se_device
*dev
= cmd
->se_dev
;
123 struct se_session
*sess
= cmd
->se_sess
;
124 struct se_portal_group
*tpg
= sess
->se_tpg
;
126 if (!(sess
) || !(tpg
))
129 spin_lock(&dev
->dev_reservation_lock
);
130 if (!dev
->dev_reserved_node_acl
|| !sess
) {
131 spin_unlock(&dev
->dev_reservation_lock
);
135 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
) {
136 spin_unlock(&dev
->dev_reservation_lock
);
139 dev
->dev_reserved_node_acl
= NULL
;
140 dev
->dev_flags
&= ~DF_SPC2_RESERVATIONS
;
141 if (dev
->dev_flags
& DF_SPC2_RESERVATIONS_WITH_ISID
) {
142 dev
->dev_res_bin_isid
= 0;
143 dev
->dev_flags
&= ~DF_SPC2_RESERVATIONS_WITH_ISID
;
145 printk(KERN_INFO
"SCSI-2 Released reservation for %s LUN: %u ->"
146 " MAPPED LUN: %u for %s\n", TPG_TFO(tpg
)->get_fabric_name(),
147 SE_LUN(cmd
)->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
148 sess
->se_node_acl
->initiatorname
);
149 spin_unlock(&dev
->dev_reservation_lock
);
154 static int core_scsi2_reservation_reserve(struct se_cmd
*cmd
)
156 struct se_device
*dev
= cmd
->se_dev
;
157 struct se_session
*sess
= cmd
->se_sess
;
158 struct se_portal_group
*tpg
= sess
->se_tpg
;
160 if ((T_TASK(cmd
)->t_task_cdb
[1] & 0x01) &&
161 (T_TASK(cmd
)->t_task_cdb
[1] & 0x02)) {
162 printk(KERN_ERR
"LongIO and Obselete Bits set, returning"
163 " ILLEGAL_REQUEST\n");
164 return PYX_TRANSPORT_ILLEGAL_REQUEST
;
167 * This is currently the case for target_core_mod passthrough struct se_cmd
170 if (!(sess
) || !(tpg
))
173 spin_lock(&dev
->dev_reservation_lock
);
174 if (dev
->dev_reserved_node_acl
&&
175 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
176 printk(KERN_ERR
"SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
177 TPG_TFO(tpg
)->get_fabric_name());
178 printk(KERN_ERR
"Original reserver LUN: %u %s\n",
179 SE_LUN(cmd
)->unpacked_lun
,
180 dev
->dev_reserved_node_acl
->initiatorname
);
181 printk(KERN_ERR
"Current attempt - LUN: %u -> MAPPED LUN: %u"
182 " from %s \n", SE_LUN(cmd
)->unpacked_lun
,
183 cmd
->se_deve
->mapped_lun
,
184 sess
->se_node_acl
->initiatorname
);
185 spin_unlock(&dev
->dev_reservation_lock
);
186 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
189 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
190 dev
->dev_flags
|= DF_SPC2_RESERVATIONS
;
191 if (sess
->sess_bin_isid
!= 0) {
192 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
193 dev
->dev_flags
|= DF_SPC2_RESERVATIONS_WITH_ISID
;
195 printk(KERN_INFO
"SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
196 " for %s\n", TPG_TFO(tpg
)->get_fabric_name(),
197 SE_LUN(cmd
)->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
198 sess
->se_node_acl
->initiatorname
);
199 spin_unlock(&dev
->dev_reservation_lock
);
204 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
205 struct se_node_acl
*, struct se_session
*);
206 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
209 * Setup in target_core_transport.c:transport_generic_cmd_sequencer()
210 * and called via struct se_cmd->transport_emulate_cdb() in TCM processing
213 int core_scsi2_emulate_crh(struct se_cmd
*cmd
)
215 struct se_session
*se_sess
= cmd
->se_sess
;
216 struct se_subsystem_dev
*su_dev
= cmd
->se_dev
->se_sub_dev
;
217 struct t10_pr_registration
*pr_reg
;
218 struct t10_reservation_template
*pr_tmpl
= &su_dev
->t10_reservation
;
219 unsigned char *cdb
= &T_TASK(cmd
)->t_task_cdb
[0];
220 int crh
= (T10_RES(su_dev
)->res_type
== SPC3_PERSISTENT_RESERVATIONS
);
229 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
233 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
236 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
237 * status, but no reservation shall be established and the
238 * persistent reservation shall not be changed, if the command
239 * is received from a) and b) below.
241 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
242 * status, but the persistent reservation shall not be released,
243 * if the command is received from a) and b)
245 * a) An I_T nexus that is a persistent reservation holder; or
246 * b) An I_T nexus that is registered if a registrants only or
247 * all registrants type persistent reservation is present.
249 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
250 * RELEASE(6) command, or RELEASE(10) command shall be processed
251 * as defined in SPC-2.
253 if (pr_reg
->pr_res_holder
) {
254 core_scsi3_put_pr_reg(pr_reg
);
257 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
258 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
259 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
260 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
261 core_scsi3_put_pr_reg(pr_reg
);
264 core_scsi3_put_pr_reg(pr_reg
);
268 * Following spc2r20 5.5.1 Reservations overview:
270 * If a logical unit has executed a PERSISTENT RESERVE OUT
271 * command with the REGISTER or the REGISTER AND IGNORE
272 * EXISTING KEY service action and is still registered by any
273 * initiator, all RESERVE commands and all RELEASE commands
274 * regardless of initiator shall conflict and shall terminate
275 * with a RESERVATION CONFLICT status.
277 spin_lock(&pr_tmpl
->registration_lock
);
278 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
279 spin_unlock(&pr_tmpl
->registration_lock
);
283 printk(KERN_ERR
"Received legacy SPC-2 RESERVE/RELEASE"
284 " while active SPC-3 registrations exist,"
285 " returning RESERVATION_CONFLICT\n");
286 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
290 if ((cdb
[0] == RESERVE
) || (cdb
[0] == RESERVE_10
))
291 return core_scsi2_reservation_reserve(cmd
);
292 else if ((cdb
[0] == RELEASE
) || (cdb
[0] == RELEASE_10
))
293 return core_scsi2_reservation_release(cmd
);
295 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
299 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
301 * This function is called by those initiator ports who are *NOT*
302 * the active PR reservation holder when a reservation is present.
304 static int core_scsi3_pr_seq_non_holder(
309 struct se_dev_entry
*se_deve
;
310 struct se_session
*se_sess
= SE_SESS(cmd
);
311 int other_cdb
= 0, ignore_reg
;
312 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
313 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
314 int we
= 0; /* Write Exclusive */
315 int legacy
= 0; /* Act like a legacy device and return
316 * RESERVATION CONFLICT on some CDBs */
318 * A legacy SPC-2 reservation is being held.
320 if (cmd
->se_dev
->dev_flags
& DF_SPC2_RESERVATIONS
)
321 return core_scsi2_reservation_seq_non_holder(cmd
,
324 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
326 * Determine if the registration should be ignored due to
327 * non-matching ISIDs in core_scsi3_pr_reservation_check().
329 ignore_reg
= (pr_reg_type
& 0x80000000);
331 pr_reg_type
&= ~0x80000000;
333 switch (pr_reg_type
) {
334 case PR_TYPE_WRITE_EXCLUSIVE
:
336 case PR_TYPE_EXCLUSIVE_ACCESS
:
338 * Some commands are only allowed for the persistent reservation
341 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
342 registered_nexus
= 1;
344 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
346 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
348 * Some commands are only allowed for registered I_T Nexuses.
351 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
352 registered_nexus
= 1;
354 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
356 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
358 * Each registered I_T Nexus is a reservation holder.
361 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
362 registered_nexus
= 1;
368 * Referenced from spc4r17 table 45 for *NON* PR holder access
371 case SECURITY_PROTOCOL_IN
:
372 if (registered_nexus
)
380 case RECEIVE_DIAGNOSTIC
:
385 if (registered_nexus
) {
389 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
391 case PERSISTENT_RESERVE_OUT
:
393 * This follows PERSISTENT_RESERVE_OUT service actions that
394 * are allowed in the presence of various reservations.
395 * See spc4r17, table 46
397 switch (cdb
[1] & 0x1f) {
400 case PRO_PREEMPT_AND_ABORT
:
401 ret
= (registered_nexus
) ? 0 : 1;
404 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
407 case PRO_REGISTER_AND_MOVE
:
412 ret
= (registered_nexus
) ? 0 : 1;
415 printk(KERN_ERR
"Unknown PERSISTENT_RESERVE_OUT service"
416 " action: 0x%02x\n", cdb
[1] & 0x1f);
422 /* Handled by CRH=1 in core_scsi2_emulate_crh() */
427 /* Handled by CRH=1 in core_scsi2_emulate_crh() */
430 case TEST_UNIT_READY
:
431 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
434 switch (cdb
[1] & 0x1f) {
435 case MI_MANAGEMENT_PROTOCOL_IN
:
436 if (registered_nexus
) {
440 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
442 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
443 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
448 if (registered_nexus
) {
452 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
454 case MI_REPORT_ALIASES
:
455 case MI_REPORT_IDENTIFYING_INFORMATION
:
456 case MI_REPORT_PRIORITY
:
457 case MI_REPORT_TARGET_PGS
:
458 case MI_REPORT_TIMESTAMP
:
459 ret
= 0; /* Allowed */
462 printk(KERN_ERR
"Unknown MI Service Action: 0x%02x\n",
467 case ACCESS_CONTROL_IN
:
468 case ACCESS_CONTROL_OUT
:
471 case READ_MEDIA_SERIAL_NUMBER
:
474 case PERSISTENT_RESERVE_IN
:
475 ret
= 0; /*/ Allowed CDBs */
482 * Case where the CDB is explicitly allowed in the above switch
485 if (!(ret
) && !(other_cdb
)) {
487 printk(KERN_INFO
"Allowing explict CDB: 0x%02x for %s"
488 " reservation holder\n", cdb
[0],
489 core_scsi3_pr_dump_type(pr_reg_type
));
494 * Check if write exclusive initiator ports *NOT* holding the
495 * WRITE_EXCLUSIVE_* reservation.
497 if ((we
) && !(registered_nexus
)) {
498 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
500 * Conflict for write exclusive
502 printk(KERN_INFO
"%s Conflict for unregistered nexus"
503 " %s CDB: 0x%02x to %s reservation\n",
504 transport_dump_cmd_direction(cmd
),
505 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
506 core_scsi3_pr_dump_type(pr_reg_type
));
510 * Allow non WRITE CDBs for all Write Exclusive
511 * PR TYPEs to pass for registered and
512 * non-registered_nexuxes NOT holding the reservation.
514 * We only make noise for the unregisterd nexuses,
515 * as we expect registered non-reservation holding
516 * nexuses to issue CDBs.
519 if (!(registered_nexus
)) {
520 printk(KERN_INFO
"Allowing implict CDB: 0x%02x"
521 " for %s reservation on unregistered"
523 core_scsi3_pr_dump_type(pr_reg_type
));
528 } else if ((reg_only
) || (all_reg
)) {
529 if (registered_nexus
) {
531 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
532 * allow commands from registered nexuses.
535 printk(KERN_INFO
"Allowing implict CDB: 0x%02x for %s"
536 " reservation\n", cdb
[0],
537 core_scsi3_pr_dump_type(pr_reg_type
));
542 printk(KERN_INFO
"%s Conflict for %sregistered nexus %s CDB: 0x%2x"
543 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
544 (registered_nexus
) ? "" : "un",
545 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
546 core_scsi3_pr_dump_type(pr_reg_type
));
548 return 1; /* Conflict by default */
551 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
553 struct se_subsystem_dev
*su_dev
= SU_DEV(dev
);
556 * PRGeneration field shall contain the value of a 32-bit wrapping
557 * counter mainted by the device server.
559 * Note that this is done regardless of Active Persist across
560 * Target PowerLoss (APTPL)
562 * See spc4r17 section 6.3.12 READ_KEYS service action
564 spin_lock(&dev
->dev_reservation_lock
);
565 prg
= T10_RES(su_dev
)->pr_generation
++;
566 spin_unlock(&dev
->dev_reservation_lock
);
571 static int core_scsi3_pr_reservation_check(
575 struct se_device
*dev
= cmd
->se_dev
;
576 struct se_session
*sess
= cmd
->se_sess
;
582 * A legacy SPC-2 reservation is being held.
584 if (dev
->dev_flags
& DF_SPC2_RESERVATIONS
)
585 return core_scsi2_reservation_check(cmd
, pr_reg_type
);
587 spin_lock(&dev
->dev_reservation_lock
);
588 if (!(dev
->dev_pr_res_holder
)) {
589 spin_unlock(&dev
->dev_reservation_lock
);
592 *pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
593 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
594 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
) {
595 spin_unlock(&dev
->dev_reservation_lock
);
598 if (!(dev
->dev_pr_res_holder
->isid_present_at_reg
)) {
599 spin_unlock(&dev
->dev_reservation_lock
);
602 ret
= (dev
->dev_pr_res_holder
->pr_reg_bin_isid
==
603 sess
->sess_bin_isid
) ? 0 : -1;
605 * Use bit in *pr_reg_type to notify ISID mismatch in
606 * core_scsi3_pr_seq_non_holder().
609 *pr_reg_type
|= 0x80000000;
610 spin_unlock(&dev
->dev_reservation_lock
);
615 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
616 struct se_device
*dev
,
617 struct se_node_acl
*nacl
,
618 struct se_dev_entry
*deve
,
624 struct se_subsystem_dev
*su_dev
= SU_DEV(dev
);
625 struct t10_pr_registration
*pr_reg
;
627 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
629 printk(KERN_ERR
"Unable to allocate struct t10_pr_registration\n");
633 pr_reg
->pr_aptpl_buf
= kzalloc(T10_RES(su_dev
)->pr_aptpl_buf_len
,
635 if (!(pr_reg
->pr_aptpl_buf
)) {
636 printk(KERN_ERR
"Unable to allocate pr_reg->pr_aptpl_buf\n");
637 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
641 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
642 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
643 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
644 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
645 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
646 atomic_set(&pr_reg
->pr_res_holders
, 0);
647 pr_reg
->pr_reg_nacl
= nacl
;
648 pr_reg
->pr_reg_deve
= deve
;
649 pr_reg
->pr_res_mapped_lun
= deve
->mapped_lun
;
650 pr_reg
->pr_aptpl_target_lun
= deve
->se_lun
->unpacked_lun
;
651 pr_reg
->pr_res_key
= sa_res_key
;
652 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
653 pr_reg
->pr_reg_aptpl
= aptpl
;
654 pr_reg
->pr_reg_tg_pt_lun
= deve
->se_lun
;
656 * If an ISID value for this SCSI Initiator Port exists,
657 * save it to the registration now.
660 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
661 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
662 pr_reg
->isid_present_at_reg
= 1;
668 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
669 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
672 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
675 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
676 struct se_device
*dev
,
677 struct se_node_acl
*nacl
,
678 struct se_dev_entry
*deve
,
684 struct se_dev_entry
*deve_tmp
;
685 struct se_node_acl
*nacl_tmp
;
686 struct se_port
*port
, *port_tmp
;
687 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
688 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
691 * Create a registration for the I_T Nexus upon which the
692 * PROUT REGISTER was received.
694 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, deve
, isid
,
695 sa_res_key
, all_tg_pt
, aptpl
);
699 * Return pointer to pr_reg for ALL_TG_PT=0
704 * Create list of matching SCSI Initiator Port registrations
707 spin_lock(&dev
->se_port_lock
);
708 list_for_each_entry_safe(port
, port_tmp
, &dev
->dev_sep_list
, sep_list
) {
709 atomic_inc(&port
->sep_tg_pt_ref_cnt
);
710 smp_mb__after_atomic_inc();
711 spin_unlock(&dev
->se_port_lock
);
713 spin_lock_bh(&port
->sep_alua_lock
);
714 list_for_each_entry(deve_tmp
, &port
->sep_alua_list
,
717 * This pointer will be NULL for demo mode MappedLUNs
718 * that have not been make explict via a ConfigFS
719 * MappedLUN group for the SCSI Initiator Node ACL.
721 if (!(deve_tmp
->se_lun_acl
))
724 nacl_tmp
= deve_tmp
->se_lun_acl
->se_lun_nacl
;
726 * Skip the matching struct se_node_acl that is allocated
729 if (nacl
== nacl_tmp
)
732 * Only perform PR registrations for target ports on
733 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
736 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
739 * Look for a matching Initiator Node ACL in ASCII format
741 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
744 atomic_inc(&deve_tmp
->pr_ref_count
);
745 smp_mb__after_atomic_inc();
746 spin_unlock_bh(&port
->sep_alua_lock
);
748 * Grab a configfs group dependency that is released
749 * for the exception path at label out: below, or upon
750 * completion of adding ALL_TG_PT=1 registrations in
751 * __core_scsi3_add_registration()
753 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
755 printk(KERN_ERR
"core_scsi3_lunacl_depend"
757 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
758 smp_mb__after_atomic_dec();
759 atomic_dec(&deve_tmp
->pr_ref_count
);
760 smp_mb__after_atomic_dec();
764 * Located a matching SCSI Initiator Port on a different
765 * port, allocate the pr_reg_atp and attach it to the
766 * pr_reg->pr_reg_atp_list that will be processed once
767 * the original *pr_reg is processed in
768 * __core_scsi3_add_registration()
770 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
771 nacl_tmp
, deve_tmp
, NULL
,
772 sa_res_key
, all_tg_pt
, aptpl
);
774 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
775 smp_mb__after_atomic_dec();
776 atomic_dec(&deve_tmp
->pr_ref_count
);
777 smp_mb__after_atomic_dec();
778 core_scsi3_lunacl_undepend_item(deve_tmp
);
782 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
783 &pr_reg
->pr_reg_atp_list
);
784 spin_lock_bh(&port
->sep_alua_lock
);
786 spin_unlock_bh(&port
->sep_alua_lock
);
788 spin_lock(&dev
->se_port_lock
);
789 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
790 smp_mb__after_atomic_dec();
792 spin_unlock(&dev
->se_port_lock
);
796 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
797 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
798 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
799 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
800 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
802 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
806 int core_scsi3_alloc_aptpl_registration(
807 struct t10_reservation_template
*pr_tmpl
,
809 unsigned char *i_port
,
812 unsigned char *t_port
,
819 struct t10_pr_registration
*pr_reg
;
821 if (!(i_port
) || !(t_port
) || !(sa_res_key
)) {
822 printk(KERN_ERR
"Illegal parameters for APTPL registration\n");
826 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
828 printk(KERN_ERR
"Unable to allocate struct t10_pr_registration\n");
831 pr_reg
->pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
, GFP_KERNEL
);
833 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
834 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
835 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
836 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
837 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
838 atomic_set(&pr_reg
->pr_res_holders
, 0);
839 pr_reg
->pr_reg_nacl
= NULL
;
840 pr_reg
->pr_reg_deve
= NULL
;
841 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
842 pr_reg
->pr_aptpl_target_lun
= target_lun
;
843 pr_reg
->pr_res_key
= sa_res_key
;
844 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
845 pr_reg
->pr_reg_aptpl
= 1;
846 pr_reg
->pr_reg_tg_pt_lun
= NULL
;
847 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
848 pr_reg
->pr_res_type
= type
;
850 * If an ISID value had been saved in APTPL metadata for this
851 * SCSI Initiator Port, restore it now.
854 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
855 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
856 pr_reg
->isid_present_at_reg
= 1;
859 * Copy the i_port and t_port information from caller.
861 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
862 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
863 pr_reg
->pr_reg_tpgt
= tpgt
;
865 * Set pr_res_holder from caller, the pr_reg who is the reservation
866 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
867 * the Initiator Node LUN ACL from the fabric module is created for
870 pr_reg
->pr_res_holder
= res_holder
;
872 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
873 printk(KERN_INFO
"SPC-3 PR APTPL Successfully added registration%s from"
874 " metadata\n", (res_holder
) ? "+reservation" : "");
878 static void core_scsi3_aptpl_reserve(
879 struct se_device
*dev
,
880 struct se_portal_group
*tpg
,
881 struct se_node_acl
*node_acl
,
882 struct t10_pr_registration
*pr_reg
)
884 char i_buf
[PR_REG_ISID_ID_LEN
];
887 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
888 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
891 spin_lock(&dev
->dev_reservation_lock
);
892 dev
->dev_pr_res_holder
= pr_reg
;
893 spin_unlock(&dev
->dev_reservation_lock
);
895 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: APTPL RESERVE created"
896 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
897 TPG_TFO(tpg
)->get_fabric_name(),
898 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
899 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
900 printk(KERN_INFO
"SPC-3 PR [%s] RESERVE Node: %s%s\n",
901 TPG_TFO(tpg
)->get_fabric_name(), node_acl
->initiatorname
,
902 (prf_isid
) ? &i_buf
[0] : "");
905 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
906 struct t10_pr_registration
*, int, int);
908 static int __core_scsi3_check_aptpl_registration(
909 struct se_device
*dev
,
910 struct se_portal_group
*tpg
,
913 struct se_node_acl
*nacl
,
914 struct se_dev_entry
*deve
)
916 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
917 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
918 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
919 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
922 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
923 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
925 * Copy Initiator Port information from struct se_node_acl
927 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
928 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
929 TPG_TFO(tpg
)->tpg_get_wwn(tpg
));
930 tpgt
= TPG_TFO(tpg
)->tpg_get_tag(tpg
);
932 * Look for the matching registrations+reservation from those
933 * created from APTPL metadata. Note that multiple registrations
934 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
937 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
938 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
940 if (!(strcmp(pr_reg
->pr_iport
, i_port
)) &&
941 (pr_reg
->pr_res_mapped_lun
== deve
->mapped_lun
) &&
942 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
943 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
944 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
946 pr_reg
->pr_reg_nacl
= nacl
;
947 pr_reg
->pr_reg_deve
= deve
;
948 pr_reg
->pr_reg_tg_pt_lun
= lun
;
950 list_del(&pr_reg
->pr_reg_aptpl_list
);
951 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
953 * At this point all of the pointers in *pr_reg will
954 * be setup, so go ahead and add the registration.
957 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
959 * If this registration is the reservation holder,
960 * make that happen now..
962 if (pr_reg
->pr_res_holder
)
963 core_scsi3_aptpl_reserve(dev
, tpg
,
966 * Reenable pr_aptpl_active to accept new metadata
967 * updates once the SCSI device is active again..
969 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
970 pr_tmpl
->pr_aptpl_active
= 1;
973 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
978 int core_scsi3_check_aptpl_registration(
979 struct se_device
*dev
,
980 struct se_portal_group
*tpg
,
982 struct se_lun_acl
*lun_acl
)
984 struct se_subsystem_dev
*su_dev
= SU_DEV(dev
);
985 struct se_node_acl
*nacl
= lun_acl
->se_lun_nacl
;
986 struct se_dev_entry
*deve
= &nacl
->device_list
[lun_acl
->mapped_lun
];
988 if (T10_RES(su_dev
)->res_type
!= SPC3_PERSISTENT_RESERVATIONS
)
991 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
992 lun
->unpacked_lun
, nacl
, deve
);
995 static void __core_scsi3_dump_registration(
996 struct target_core_fabric_ops
*tfo
,
997 struct se_device
*dev
,
998 struct se_node_acl
*nacl
,
999 struct t10_pr_registration
*pr_reg
,
1002 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
1003 char i_buf
[PR_REG_ISID_ID_LEN
];
1006 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
1007 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1008 PR_REG_ISID_ID_LEN
);
1010 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1011 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== 2) ?
1012 "_AND_MOVE" : (register_type
== 1) ?
1013 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
1014 (prf_isid
) ? i_buf
: "");
1015 printk(KERN_INFO
"SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1016 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
1017 tfo
->tpg_get_tag(se_tpg
));
1018 printk(KERN_INFO
"SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1019 " Port(s)\n", tfo
->get_fabric_name(),
1020 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1021 TRANSPORT(dev
)->name
);
1022 printk(KERN_INFO
"SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1023 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
1024 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
1025 pr_reg
->pr_reg_aptpl
);
1029 * this function can be called with struct se_device->dev_reservation_lock
1030 * when register_move = 1
1032 static void __core_scsi3_add_registration(
1033 struct se_device
*dev
,
1034 struct se_node_acl
*nacl
,
1035 struct t10_pr_registration
*pr_reg
,
1039 struct se_subsystem_dev
*su_dev
= SU_DEV(dev
);
1040 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1041 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1042 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
1045 * Increment PRgeneration counter for struct se_device upon a successful
1046 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1048 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1049 * action, the struct se_device->dev_reservation_lock will already be held,
1050 * so we do not call core_scsi3_pr_generation() which grabs the lock
1053 pr_reg
->pr_res_generation
= (register_move
) ?
1054 T10_RES(su_dev
)->pr_generation
++ :
1055 core_scsi3_pr_generation(dev
);
1057 spin_lock(&pr_tmpl
->registration_lock
);
1058 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1059 pr_reg
->pr_reg_deve
->def_pr_registered
= 1;
1061 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1062 spin_unlock(&pr_tmpl
->registration_lock
);
1064 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1066 if (!(pr_reg
->pr_reg_all_tg_pt
) || (register_move
))
1069 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1070 * allocated in __core_scsi3_alloc_registration()
1072 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1073 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1074 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1076 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1078 spin_lock(&pr_tmpl
->registration_lock
);
1079 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1080 &pr_tmpl
->registration_list
);
1081 pr_reg_tmp
->pr_reg_deve
->def_pr_registered
= 1;
1083 __core_scsi3_dump_registration(tfo
, dev
,
1084 pr_reg_tmp
->pr_reg_nacl
, pr_reg_tmp
,
1086 spin_unlock(&pr_tmpl
->registration_lock
);
1088 * Drop configfs group dependency reference from
1089 * __core_scsi3_alloc_registration()
1091 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1095 static int core_scsi3_alloc_registration(
1096 struct se_device
*dev
,
1097 struct se_node_acl
*nacl
,
1098 struct se_dev_entry
*deve
,
1099 unsigned char *isid
,
1106 struct t10_pr_registration
*pr_reg
;
1108 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, deve
, isid
,
1109 sa_res_key
, all_tg_pt
, aptpl
);
1113 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1114 register_type
, register_move
);
1118 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1119 struct se_device
*dev
,
1120 struct se_node_acl
*nacl
,
1121 unsigned char *isid
)
1123 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
1124 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1125 struct se_portal_group
*tpg
;
1127 spin_lock(&pr_tmpl
->registration_lock
);
1128 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1129 &pr_tmpl
->registration_list
, pr_reg_list
) {
1131 * First look for a matching struct se_node_acl
1133 if (pr_reg
->pr_reg_nacl
!= nacl
)
1136 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1138 * If this registration does NOT contain a fabric provided
1139 * ISID, then we have found a match.
1141 if (!(pr_reg
->isid_present_at_reg
)) {
1143 * Determine if this SCSI device server requires that
1144 * SCSI Intiatior TransportID w/ ISIDs is enforced
1145 * for fabric modules (iSCSI) requiring them.
1147 if (TPG_TFO(tpg
)->sess_get_initiator_sid
!= NULL
) {
1148 if (DEV_ATTRIB(dev
)->enforce_pr_isids
)
1151 atomic_inc(&pr_reg
->pr_res_holders
);
1152 smp_mb__after_atomic_inc();
1153 spin_unlock(&pr_tmpl
->registration_lock
);
1157 * If the *pr_reg contains a fabric defined ISID for multi-value
1158 * SCSI Initiator Port TransportIDs, then we expect a valid
1159 * matching ISID to be provided by the local SCSI Initiator Port.
1163 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1166 atomic_inc(&pr_reg
->pr_res_holders
);
1167 smp_mb__after_atomic_inc();
1168 spin_unlock(&pr_tmpl
->registration_lock
);
1171 spin_unlock(&pr_tmpl
->registration_lock
);
1176 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1177 struct se_device
*dev
,
1178 struct se_node_acl
*nacl
,
1179 struct se_session
*sess
)
1181 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1182 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1184 if (TPG_TFO(tpg
)->sess_get_initiator_sid
!= NULL
) {
1185 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1186 TPG_TFO(tpg
)->sess_get_initiator_sid(sess
, &buf
[0],
1191 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1194 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1196 atomic_dec(&pr_reg
->pr_res_holders
);
1197 smp_mb__after_atomic_dec();
1200 static int core_scsi3_check_implict_release(
1201 struct se_device
*dev
,
1202 struct t10_pr_registration
*pr_reg
)
1204 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1205 struct t10_pr_registration
*pr_res_holder
;
1208 spin_lock(&dev
->dev_reservation_lock
);
1209 pr_res_holder
= dev
->dev_pr_res_holder
;
1210 if (!(pr_res_holder
)) {
1211 spin_unlock(&dev
->dev_reservation_lock
);
1214 if (pr_res_holder
== pr_reg
) {
1216 * Perform an implict RELEASE if the registration that
1217 * is being released is holding the reservation.
1219 * From spc4r17, section 5.7.11.1:
1221 * e) If the I_T nexus is the persistent reservation holder
1222 * and the persistent reservation is not an all registrants
1223 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1224 * service action or REGISTER AND IGNORE EXISTING KEY
1225 * service action with the SERVICE ACTION RESERVATION KEY
1226 * field set to zero (see 5.7.11.3).
1228 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0);
1231 * For 'All Registrants' reservation types, all existing
1232 * registrations are still processed as reservation holders
1233 * in core_scsi3_pr_seq_non_holder() after the initial
1234 * reservation holder is implictly released here.
1236 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1237 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1238 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1239 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1240 printk(KERN_ERR
"SPC-3 PR: Unable to perform ALL_TG_PT=1"
1241 " UNREGISTER while existing reservation with matching"
1242 " key 0x%016Lx is present from another SCSI Initiator"
1243 " Port\n", pr_reg
->pr_res_key
);
1246 spin_unlock(&dev
->dev_reservation_lock
);
1252 * Called with struct t10_reservation_template->registration_lock held.
1254 static void __core_scsi3_free_registration(
1255 struct se_device
*dev
,
1256 struct t10_pr_registration
*pr_reg
,
1257 struct list_head
*preempt_and_abort_list
,
1260 struct target_core_fabric_ops
*tfo
=
1261 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1262 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
1263 char i_buf
[PR_REG_ISID_ID_LEN
];
1266 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1267 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1268 PR_REG_ISID_ID_LEN
);
1270 pr_reg
->pr_reg_deve
->def_pr_registered
= 0;
1271 pr_reg
->pr_reg_deve
->pr_res_key
= 0;
1272 list_del(&pr_reg
->pr_reg_list
);
1274 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1275 * so call core_scsi3_put_pr_reg() to decrement our reference.
1278 core_scsi3_put_pr_reg(pr_reg
);
1280 * Wait until all reference from any other I_T nexuses for this
1281 * *pr_reg have been released. Because list_del() is called above,
1282 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1283 * count back to zero, and we release *pr_reg.
1285 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1286 spin_unlock(&pr_tmpl
->registration_lock
);
1287 printk("SPC-3 PR [%s] waiting for pr_res_holders\n",
1288 tfo
->get_fabric_name());
1290 spin_lock(&pr_tmpl
->registration_lock
);
1293 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1294 " Node: %s%s\n", tfo
->get_fabric_name(),
1295 pr_reg
->pr_reg_nacl
->initiatorname
,
1296 (prf_isid
) ? &i_buf
[0] : "");
1297 printk(KERN_INFO
"SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1298 " Port(s)\n", tfo
->get_fabric_name(),
1299 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1300 TRANSPORT(dev
)->name
);
1301 printk(KERN_INFO
"SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1302 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1303 pr_reg
->pr_res_generation
);
1305 if (!(preempt_and_abort_list
)) {
1306 pr_reg
->pr_reg_deve
= NULL
;
1307 pr_reg
->pr_reg_nacl
= NULL
;
1308 kfree(pr_reg
->pr_aptpl_buf
);
1309 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1313 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1314 * are released once the ABORT_TASK_SET has completed..
1316 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1319 void core_scsi3_free_pr_reg_from_nacl(
1320 struct se_device
*dev
,
1321 struct se_node_acl
*nacl
)
1323 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
1324 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1326 * If the passed se_node_acl matches the reservation holder,
1327 * release the reservation.
1329 spin_lock(&dev
->dev_reservation_lock
);
1330 pr_res_holder
= dev
->dev_pr_res_holder
;
1331 if ((pr_res_holder
!= NULL
) &&
1332 (pr_res_holder
->pr_reg_nacl
== nacl
))
1333 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0);
1334 spin_unlock(&dev
->dev_reservation_lock
);
1336 * Release any registration associated with the struct se_node_acl.
1338 spin_lock(&pr_tmpl
->registration_lock
);
1339 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1340 &pr_tmpl
->registration_list
, pr_reg_list
) {
1342 if (pr_reg
->pr_reg_nacl
!= nacl
)
1345 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1347 spin_unlock(&pr_tmpl
->registration_lock
);
1350 void core_scsi3_free_all_registrations(
1351 struct se_device
*dev
)
1353 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
1354 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1356 spin_lock(&dev
->dev_reservation_lock
);
1357 pr_res_holder
= dev
->dev_pr_res_holder
;
1358 if (pr_res_holder
!= NULL
) {
1359 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1360 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1363 spin_unlock(&dev
->dev_reservation_lock
);
1365 spin_lock(&pr_tmpl
->registration_lock
);
1366 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1367 &pr_tmpl
->registration_list
, pr_reg_list
) {
1369 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1371 spin_unlock(&pr_tmpl
->registration_lock
);
1373 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1374 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1375 pr_reg_aptpl_list
) {
1376 list_del(&pr_reg
->pr_reg_aptpl_list
);
1377 kfree(pr_reg
->pr_aptpl_buf
);
1378 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1380 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1383 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1385 return configfs_depend_item(TPG_TFO(tpg
)->tf_subsys
,
1386 &tpg
->tpg_group
.cg_item
);
1389 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1391 configfs_undepend_item(TPG_TFO(tpg
)->tf_subsys
,
1392 &tpg
->tpg_group
.cg_item
);
1394 atomic_dec(&tpg
->tpg_pr_ref_count
);
1395 smp_mb__after_atomic_dec();
1398 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1400 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1402 if (nacl
->dynamic_node_acl
)
1405 return configfs_depend_item(TPG_TFO(tpg
)->tf_subsys
,
1406 &nacl
->acl_group
.cg_item
);
1409 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1411 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1413 if (nacl
->dynamic_node_acl
) {
1414 atomic_dec(&nacl
->acl_pr_ref_count
);
1415 smp_mb__after_atomic_dec();
1419 configfs_undepend_item(TPG_TFO(tpg
)->tf_subsys
,
1420 &nacl
->acl_group
.cg_item
);
1422 atomic_dec(&nacl
->acl_pr_ref_count
);
1423 smp_mb__after_atomic_dec();
1426 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1428 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1429 struct se_node_acl
*nacl
;
1430 struct se_portal_group
*tpg
;
1432 * For nacl->dynamic_node_acl=1
1437 nacl
= lun_acl
->se_lun_nacl
;
1440 return configfs_depend_item(TPG_TFO(tpg
)->tf_subsys
,
1441 &lun_acl
->se_lun_group
.cg_item
);
1444 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1446 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1447 struct se_node_acl
*nacl
;
1448 struct se_portal_group
*tpg
;
1450 * For nacl->dynamic_node_acl=1
1453 atomic_dec(&se_deve
->pr_ref_count
);
1454 smp_mb__after_atomic_dec();
1457 nacl
= lun_acl
->se_lun_nacl
;
1460 configfs_undepend_item(TPG_TFO(tpg
)->tf_subsys
,
1461 &lun_acl
->se_lun_group
.cg_item
);
1463 atomic_dec(&se_deve
->pr_ref_count
);
1464 smp_mb__after_atomic_dec();
1467 static int core_scsi3_decode_spec_i_port(
1469 struct se_portal_group
*tpg
,
1470 unsigned char *l_isid
,
1475 struct se_device
*dev
= SE_DEV(cmd
);
1476 struct se_port
*tmp_port
;
1477 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1478 struct se_session
*se_sess
= SE_SESS(cmd
);
1479 struct se_node_acl
*dest_node_acl
= NULL
;
1480 struct se_dev_entry
*dest_se_deve
= NULL
, *local_se_deve
;
1481 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1482 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1483 struct list_head tid_dest_list
;
1484 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1485 struct target_core_fabric_ops
*tmp_tf_ops
;
1486 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
1487 unsigned char *ptr
, *i_str
= NULL
, proto_ident
, tmp_proto_ident
;
1488 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
1489 u32 tpdl
, tid_len
= 0;
1490 int ret
, dest_local_nexus
, prf_isid
;
1493 memset(dest_iport
, 0, 64);
1494 INIT_LIST_HEAD(&tid_dest_list
);
1496 local_se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
1498 * Allocate a struct pr_transport_id_holder and setup the
1499 * local_node_acl and local_se_deve pointers and add to
1500 * struct list_head tid_dest_list for add registration
1501 * processing in the loop of tid_dest_list below.
1503 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1505 printk(KERN_ERR
"Unable to allocate tidh_new\n");
1506 return PYX_TRANSPORT_LU_COMM_FAILURE
;
1508 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1509 tidh_new
->dest_tpg
= tpg
;
1510 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1511 tidh_new
->dest_se_deve
= local_se_deve
;
1513 local_pr_reg
= __core_scsi3_alloc_registration(SE_DEV(cmd
),
1514 se_sess
->se_node_acl
, local_se_deve
, l_isid
,
1515 sa_res_key
, all_tg_pt
, aptpl
);
1516 if (!(local_pr_reg
)) {
1518 return PYX_TRANSPORT_LU_COMM_FAILURE
;
1520 tidh_new
->dest_pr_reg
= local_pr_reg
;
1522 * The local I_T nexus does not hold any configfs dependances,
1523 * so we set tid_h->dest_local_nexus=1 to prevent the
1524 * configfs_undepend_item() calls in the tid_dest_list loops below.
1526 tidh_new
->dest_local_nexus
= 1;
1527 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1529 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1530 * first extract TransportID Parameter Data Length, and make sure
1531 * the value matches up to the SCSI expected data transfer length.
1533 tpdl
= (buf
[24] & 0xff) << 24;
1534 tpdl
|= (buf
[25] & 0xff) << 16;
1535 tpdl
|= (buf
[26] & 0xff) << 8;
1536 tpdl
|= buf
[27] & 0xff;
1538 if ((tpdl
+ 28) != cmd
->data_length
) {
1539 printk(KERN_ERR
"SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1540 " does not equal CDB data_length: %u\n", tpdl
,
1542 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1546 * Start processing the received transport IDs using the
1547 * receiving I_T Nexus portal's fabric dependent methods to
1548 * obtain the SCSI Initiator Port/Device Identifiers.
1553 proto_ident
= (ptr
[0] & 0x0f);
1556 spin_lock(&dev
->se_port_lock
);
1557 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1558 tmp_tpg
= tmp_port
->sep_tpg
;
1561 tmp_tf_ops
= TPG_TFO(tmp_tpg
);
1564 if (!(tmp_tf_ops
->get_fabric_proto_ident
) ||
1565 !(tmp_tf_ops
->tpg_parse_pr_out_transport_id
))
1568 * Look for the matching proto_ident provided by
1569 * the received TransportID
1571 tmp_proto_ident
= tmp_tf_ops
->get_fabric_proto_ident(tmp_tpg
);
1572 if (tmp_proto_ident
!= proto_ident
)
1574 dest_rtpi
= tmp_port
->sep_rtpi
;
1576 i_str
= tmp_tf_ops
->tpg_parse_pr_out_transport_id(
1577 tmp_tpg
, (const char *)ptr
, &tid_len
,
1582 atomic_inc(&tmp_tpg
->tpg_pr_ref_count
);
1583 smp_mb__after_atomic_inc();
1584 spin_unlock(&dev
->se_port_lock
);
1586 ret
= core_scsi3_tpg_depend_item(tmp_tpg
);
1588 printk(KERN_ERR
" core_scsi3_tpg_depend_item()"
1590 atomic_dec(&tmp_tpg
->tpg_pr_ref_count
);
1591 smp_mb__after_atomic_dec();
1592 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1596 * Locate the desination initiator ACL to be registered
1597 * from the decoded fabric module specific TransportID
1600 spin_lock_bh(&tmp_tpg
->acl_node_lock
);
1601 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1603 if (dest_node_acl
) {
1604 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
1605 smp_mb__after_atomic_inc();
1607 spin_unlock_bh(&tmp_tpg
->acl_node_lock
);
1609 if (!(dest_node_acl
)) {
1610 core_scsi3_tpg_undepend_item(tmp_tpg
);
1611 spin_lock(&dev
->se_port_lock
);
1615 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
1617 printk(KERN_ERR
"configfs_depend_item() failed"
1618 " for dest_node_acl->acl_group\n");
1619 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
1620 smp_mb__after_atomic_dec();
1621 core_scsi3_tpg_undepend_item(tmp_tpg
);
1622 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1627 printk(KERN_INFO
"SPC-3 PR SPEC_I_PT: Located %s Node:"
1628 " %s Port RTPI: %hu\n",
1629 TPG_TFO(dest_tpg
)->get_fabric_name(),
1630 dest_node_acl
->initiatorname
, dest_rtpi
);
1632 spin_lock(&dev
->se_port_lock
);
1635 spin_unlock(&dev
->se_port_lock
);
1638 printk(KERN_ERR
"SPC-3 PR SPEC_I_PT: Unable to locate"
1640 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1644 printk("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1645 " tid_len: %d for %s + %s\n",
1646 TPG_TFO(dest_tpg
)->get_fabric_name(), cmd
->data_length
,
1647 tpdl
, tid_len
, i_str
, iport_ptr
);
1649 if (tid_len
> tpdl
) {
1650 printk(KERN_ERR
"SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1651 " %u for Transport ID: %s\n", tid_len
, ptr
);
1652 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1653 core_scsi3_tpg_undepend_item(dest_tpg
);
1654 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1658 * Locate the desintation struct se_dev_entry pointer for matching
1659 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1662 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1664 if (!(dest_se_deve
)) {
1665 printk(KERN_ERR
"Unable to locate %s dest_se_deve"
1666 " from destination RTPI: %hu\n",
1667 TPG_TFO(dest_tpg
)->get_fabric_name(),
1670 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1671 core_scsi3_tpg_undepend_item(dest_tpg
);
1672 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1676 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
1678 printk(KERN_ERR
"core_scsi3_lunacl_depend_item()"
1680 atomic_dec(&dest_se_deve
->pr_ref_count
);
1681 smp_mb__after_atomic_dec();
1682 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1683 core_scsi3_tpg_undepend_item(dest_tpg
);
1684 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1688 printk(KERN_INFO
"SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1689 " dest_se_deve mapped_lun: %u\n",
1690 TPG_TFO(dest_tpg
)->get_fabric_name(),
1691 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1694 * Skip any TransportIDs that already have a registration for
1697 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1700 core_scsi3_put_pr_reg(pr_reg_e
);
1701 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1702 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1703 core_scsi3_tpg_undepend_item(dest_tpg
);
1710 * Allocate a struct pr_transport_id_holder and setup
1711 * the dest_node_acl and dest_se_deve pointers for the
1714 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1717 printk(KERN_ERR
"Unable to allocate tidh_new\n");
1718 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1719 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1720 core_scsi3_tpg_undepend_item(dest_tpg
);
1721 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1724 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1725 tidh_new
->dest_tpg
= dest_tpg
;
1726 tidh_new
->dest_node_acl
= dest_node_acl
;
1727 tidh_new
->dest_se_deve
= dest_se_deve
;
1730 * Allocate, but do NOT add the registration for the
1731 * TransportID referenced SCSI Initiator port. This
1732 * done because of the following from spc4r17 in section
1733 * 6.14.3 wrt SPEC_I_PT:
1735 * "If a registration fails for any initiator port (e.g., if th
1736 * logical unit does not have enough resources available to
1737 * hold the registration information), no registrations shall be
1738 * made, and the command shall be terminated with
1739 * CHECK CONDITION status."
1741 * That means we call __core_scsi3_alloc_registration() here,
1742 * and then call __core_scsi3_add_registration() in the
1743 * 2nd loop which will never fail.
1745 dest_pr_reg
= __core_scsi3_alloc_registration(SE_DEV(cmd
),
1746 dest_node_acl
, dest_se_deve
, iport_ptr
,
1747 sa_res_key
, all_tg_pt
, aptpl
);
1748 if (!(dest_pr_reg
)) {
1749 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1750 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1751 core_scsi3_tpg_undepend_item(dest_tpg
);
1753 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
1756 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1757 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
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(SE_DEV(cmd
), dest_node_acl
,
1794 printk(KERN_INFO
"SPC-3 PR [%s] SPEC_I_PT: Successfully"
1795 " registered Transport ID for Node: %s%s Mapped LUN:"
1796 " %u\n", TPG_TFO(dest_tpg
)->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
);
1811 * For the failure case, release everything from tid_dest_list
1812 * including *dest_pr_reg and the configfs dependances..
1814 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1815 dest_tpg
= tidh
->dest_tpg
;
1816 dest_node_acl
= tidh
->dest_node_acl
;
1817 dest_se_deve
= tidh
->dest_se_deve
;
1818 dest_pr_reg
= tidh
->dest_pr_reg
;
1819 dest_local_nexus
= tidh
->dest_local_nexus
;
1821 list_del(&tidh
->dest_list
);
1824 * Release any extra ALL_TG_PT=1 registrations for
1825 * the SPEC_I_PT=1 case.
1827 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1828 &dest_pr_reg
->pr_reg_atp_list
,
1829 pr_reg_atp_mem_list
) {
1830 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1831 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1832 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1835 kfree(dest_pr_reg
->pr_aptpl_buf
);
1836 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1838 if (dest_local_nexus
)
1841 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1842 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1843 core_scsi3_tpg_undepend_item(dest_tpg
);
1849 * Called with struct se_device->dev_reservation_lock held
1851 static int __core_scsi3_update_aptpl_buf(
1852 struct se_device
*dev
,
1854 u32 pr_aptpl_buf_len
,
1855 int clear_aptpl_metadata
)
1858 struct se_portal_group
*tpg
;
1859 struct se_subsystem_dev
*su_dev
= SU_DEV(dev
);
1860 struct t10_pr_registration
*pr_reg
;
1861 unsigned char tmp
[512], isid_buf
[32];
1865 memset(buf
, 0, pr_aptpl_buf_len
);
1867 * Called to clear metadata once APTPL has been deactivated.
1869 if (clear_aptpl_metadata
) {
1870 snprintf(buf
, pr_aptpl_buf_len
,
1871 "No Registrations or Reservations\n");
1875 * Walk the registration list..
1877 spin_lock(&T10_RES(su_dev
)->registration_lock
);
1878 list_for_each_entry(pr_reg
, &T10_RES(su_dev
)->registration_list
,
1883 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1884 lun
= pr_reg
->pr_reg_tg_pt_lun
;
1886 * Write out any ISID value to APTPL metadata that was included
1887 * in the original registration.
1889 if (pr_reg
->isid_present_at_reg
)
1890 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1891 pr_reg
->pr_reg_isid
);
1893 * Include special metadata if the pr_reg matches the
1894 * reservation holder.
1896 if (dev
->dev_pr_res_holder
== pr_reg
) {
1897 snprintf(tmp
, 512, "PR_REG_START: %d"
1898 "\ninitiator_fabric=%s\n"
1899 "initiator_node=%s\n%s"
1901 "res_holder=1\nres_type=%02x\n"
1902 "res_scope=%02x\nres_all_tg_pt=%d\n"
1903 "mapped_lun=%u\n", reg_count
,
1904 TPG_TFO(tpg
)->get_fabric_name(),
1905 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1906 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1907 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1908 pr_reg
->pr_res_mapped_lun
);
1910 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1911 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1912 "sa_res_key=%llu\nres_holder=0\n"
1913 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1914 reg_count
, TPG_TFO(tpg
)->get_fabric_name(),
1915 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1916 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1917 pr_reg
->pr_res_mapped_lun
);
1920 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1921 printk(KERN_ERR
"Unable to update renaming"
1922 " APTPL metadata\n");
1923 spin_unlock(&T10_RES(su_dev
)->registration_lock
);
1926 len
+= sprintf(buf
+len
, "%s", tmp
);
1929 * Include information about the associated SCSI target port.
1931 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1932 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1933 " %d\n", TPG_TFO(tpg
)->get_fabric_name(),
1934 TPG_TFO(tpg
)->tpg_get_wwn(tpg
),
1935 TPG_TFO(tpg
)->tpg_get_tag(tpg
),
1936 lun
->lun_sep
->sep_rtpi
, lun
->unpacked_lun
, reg_count
);
1938 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1939 printk(KERN_ERR
"Unable to update renaming"
1940 " APTPL metadata\n");
1941 spin_unlock(&T10_RES(su_dev
)->registration_lock
);
1944 len
+= sprintf(buf
+len
, "%s", tmp
);
1947 spin_unlock(&T10_RES(su_dev
)->registration_lock
);
1950 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1955 static int core_scsi3_update_aptpl_buf(
1956 struct se_device
*dev
,
1958 u32 pr_aptpl_buf_len
,
1959 int clear_aptpl_metadata
)
1963 spin_lock(&dev
->dev_reservation_lock
);
1964 ret
= __core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
1965 clear_aptpl_metadata
);
1966 spin_unlock(&dev
->dev_reservation_lock
);
1972 * Called with struct se_device->aptpl_file_mutex held
1974 static int __core_scsi3_write_aptpl_to_file(
1975 struct se_device
*dev
,
1977 u32 pr_aptpl_buf_len
)
1979 struct t10_wwn
*wwn
= &SU_DEV(dev
)->t10_wwn
;
1981 struct iovec iov
[1];
1982 mm_segment_t old_fs
;
1983 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1987 memset(iov
, 0, sizeof(struct iovec
));
1988 memset(path
, 0, 512);
1990 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
1991 printk(KERN_ERR
"WWN value for struct se_device does not fit"
1992 " into path buffer\n");
1996 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
1997 file
= filp_open(path
, flags
, 0600);
1998 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
1999 printk(KERN_ERR
"filp_open(%s) for APTPL metadata"
2004 iov
[0].iov_base
= &buf
[0];
2005 if (!(pr_aptpl_buf_len
))
2006 iov
[0].iov_len
= (strlen(&buf
[0]) + 1); /* Add extra for NULL */
2008 iov
[0].iov_len
= pr_aptpl_buf_len
;
2012 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
2016 printk("Error writing APTPL metadata file: %s\n", path
);
2017 filp_close(file
, NULL
);
2020 filp_close(file
, NULL
);
2025 static int core_scsi3_update_and_write_aptpl(
2026 struct se_device
*dev
,
2027 unsigned char *in_buf
,
2028 u32 in_pr_aptpl_buf_len
)
2030 unsigned char null_buf
[64], *buf
;
2031 u32 pr_aptpl_buf_len
;
2032 int ret
, clear_aptpl_metadata
= 0;
2034 * Can be called with a NULL pointer from PROUT service action CLEAR
2037 memset(null_buf
, 0, 64);
2040 * This will clear the APTPL metadata to:
2041 * "No Registrations or Reservations" status
2043 pr_aptpl_buf_len
= 64;
2044 clear_aptpl_metadata
= 1;
2047 pr_aptpl_buf_len
= in_pr_aptpl_buf_len
;
2050 ret
= core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
2051 clear_aptpl_metadata
);
2055 * __core_scsi3_write_aptpl_to_file() will call strlen()
2056 * on the passed buf to determine pr_aptpl_buf_len.
2058 ret
= __core_scsi3_write_aptpl_to_file(dev
, buf
, 0);
2065 static int core_scsi3_emulate_pro_register(
2074 struct se_session
*se_sess
= SE_SESS(cmd
);
2075 struct se_device
*dev
= SE_DEV(cmd
);
2076 struct se_dev_entry
*se_deve
;
2077 struct se_lun
*se_lun
= SE_LUN(cmd
);
2078 struct se_portal_group
*se_tpg
;
2079 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
, *pr_reg_e
;
2080 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
2081 /* Used for APTPL metadata w/ UNREGISTER */
2082 unsigned char *pr_aptpl_buf
= NULL
;
2083 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2084 int pr_holder
= 0, ret
= 0, type
;
2086 if (!(se_sess
) || !(se_lun
)) {
2087 printk(KERN_ERR
"SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2088 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2090 se_tpg
= se_sess
->se_tpg
;
2091 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2093 if (TPG_TFO(se_tpg
)->sess_get_initiator_sid
!= NULL
) {
2094 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2095 TPG_TFO(se_tpg
)->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2097 isid_ptr
= &isid_buf
[0];
2100 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2102 pr_reg_e
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2105 printk(KERN_WARNING
"SPC-3 PR: Reservation Key non-zero"
2106 " for SA REGISTER, returning CONFLICT\n");
2107 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2110 * Do nothing but return GOOD status.
2113 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2117 * Perform the Service Action REGISTER on the Initiator
2118 * Port Endpoint that the PRO was received from on the
2119 * Logical Unit of the SCSI device server.
2121 ret
= core_scsi3_alloc_registration(SE_DEV(cmd
),
2122 se_sess
->se_node_acl
, se_deve
, isid_ptr
,
2123 sa_res_key
, all_tg_pt
, aptpl
,
2126 printk(KERN_ERR
"Unable to allocate"
2127 " struct t10_pr_registration\n");
2128 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2132 * Register both the Initiator port that received
2133 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2134 * TransportID from Parameter list and loop through
2135 * fabric dependent parameter list while calling
2136 * logic from of core_scsi3_alloc_registration() for
2137 * each TransportID provided SCSI Initiator Port/Device
2139 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2140 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2145 * Nothing left to do for the APTPL=0 case.
2148 pr_tmpl
->pr_aptpl_active
= 0;
2149 core_scsi3_update_and_write_aptpl(SE_DEV(cmd
), NULL
, 0);
2150 printk("SPC-3 PR: Set APTPL Bit Deactivated for"
2155 * Locate the newly allocated local I_T Nexus *pr_reg, and
2156 * update the APTPL metadata information using its
2157 * preallocated *pr_reg->pr_aptpl_buf.
2159 pr_reg
= core_scsi3_locate_pr_reg(SE_DEV(cmd
),
2160 se_sess
->se_node_acl
, se_sess
);
2162 ret
= core_scsi3_update_and_write_aptpl(SE_DEV(cmd
),
2163 &pr_reg
->pr_aptpl_buf
[0],
2164 pr_tmpl
->pr_aptpl_buf_len
);
2166 pr_tmpl
->pr_aptpl_active
= 1;
2167 printk("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2170 core_scsi3_put_pr_reg(pr_reg
);
2174 * Locate the existing *pr_reg via struct se_node_acl pointers
2177 type
= pr_reg
->pr_res_type
;
2179 if (!(ignore_key
)) {
2180 if (res_key
!= pr_reg
->pr_res_key
) {
2181 printk(KERN_ERR
"SPC-3 PR REGISTER: Received"
2182 " res_key: 0x%016Lx does not match"
2183 " existing SA REGISTER res_key:"
2184 " 0x%016Lx\n", res_key
,
2185 pr_reg
->pr_res_key
);
2186 core_scsi3_put_pr_reg(pr_reg
);
2187 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2191 printk(KERN_ERR
"SPC-3 PR UNREGISTER: SPEC_I_PT"
2192 " set while sa_res_key=0\n");
2193 core_scsi3_put_pr_reg(pr_reg
);
2194 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2197 * An existing ALL_TG_PT=1 registration being released
2198 * must also set ALL_TG_PT=1 in the incoming PROUT.
2200 if (pr_reg
->pr_reg_all_tg_pt
&& !(all_tg_pt
)) {
2201 printk(KERN_ERR
"SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2202 " registration exists, but ALL_TG_PT=1 bit not"
2203 " present in received PROUT\n");
2204 core_scsi3_put_pr_reg(pr_reg
);
2205 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
2208 * Allocate APTPL metadata buffer used for UNREGISTER ops
2211 pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
,
2213 if (!(pr_aptpl_buf
)) {
2214 printk(KERN_ERR
"Unable to allocate"
2216 core_scsi3_put_pr_reg(pr_reg
);
2217 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2221 * sa_res_key=0 Unregister Reservation Key for registered I_T
2222 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2225 if (!(sa_res_key
)) {
2226 pr_holder
= core_scsi3_check_implict_release(
2227 SE_DEV(cmd
), pr_reg
);
2228 if (pr_holder
< 0) {
2229 kfree(pr_aptpl_buf
);
2230 core_scsi3_put_pr_reg(pr_reg
);
2231 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2234 spin_lock(&pr_tmpl
->registration_lock
);
2236 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2237 * and matching pr_res_key.
2239 if (pr_reg
->pr_reg_all_tg_pt
) {
2240 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2241 &pr_tmpl
->registration_list
,
2244 if (!(pr_reg_p
->pr_reg_all_tg_pt
))
2247 if (pr_reg_p
->pr_res_key
!= res_key
)
2250 if (pr_reg
== pr_reg_p
)
2253 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2254 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2257 __core_scsi3_free_registration(dev
,
2262 * Release the calling I_T Nexus registration now..
2264 __core_scsi3_free_registration(SE_DEV(cmd
), pr_reg
,
2267 * From spc4r17, section 5.7.11.3 Unregistering
2269 * If the persistent reservation is a registrants only
2270 * type, the device server shall establish a unit
2271 * attention condition for the initiator port associated
2272 * with every registered I_T nexus except for the I_T
2273 * nexus on which the PERSISTENT RESERVE OUT command was
2274 * received, with the additional sense code set to
2275 * RESERVATIONS RELEASED.
2278 ((type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
2279 (type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
))) {
2280 list_for_each_entry(pr_reg_p
,
2281 &pr_tmpl
->registration_list
,
2284 core_scsi3_ua_allocate(
2285 pr_reg_p
->pr_reg_nacl
,
2286 pr_reg_p
->pr_res_mapped_lun
,
2288 ASCQ_2AH_RESERVATIONS_RELEASED
);
2291 spin_unlock(&pr_tmpl
->registration_lock
);
2294 pr_tmpl
->pr_aptpl_active
= 0;
2295 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2296 printk("SPC-3 PR: Set APTPL Bit Deactivated"
2297 " for UNREGISTER\n");
2301 ret
= core_scsi3_update_and_write_aptpl(dev
,
2303 pr_tmpl
->pr_aptpl_buf_len
);
2305 pr_tmpl
->pr_aptpl_active
= 1;
2306 printk("SPC-3 PR: Set APTPL Bit Activated"
2307 " for UNREGISTER\n");
2310 kfree(pr_aptpl_buf
);
2314 * Increment PRgeneration counter for struct se_device"
2315 * upon a successful REGISTER, see spc4r17 section 6.3.2
2316 * READ_KEYS service action.
2318 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(
2320 pr_reg
->pr_res_key
= sa_res_key
;
2321 printk("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2322 " Key for %s to: 0x%016Lx PRgeneration:"
2323 " 0x%08x\n", CMD_TFO(cmd
)->get_fabric_name(),
2324 (ignore_key
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2325 pr_reg
->pr_reg_nacl
->initiatorname
,
2326 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2329 pr_tmpl
->pr_aptpl_active
= 0;
2330 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2331 core_scsi3_put_pr_reg(pr_reg
);
2332 printk("SPC-3 PR: Set APTPL Bit Deactivated"
2337 ret
= core_scsi3_update_and_write_aptpl(dev
,
2339 pr_tmpl
->pr_aptpl_buf_len
);
2341 pr_tmpl
->pr_aptpl_active
= 1;
2342 printk("SPC-3 PR: Set APTPL Bit Activated"
2346 kfree(pr_aptpl_buf
);
2347 core_scsi3_put_pr_reg(pr_reg
);
2353 unsigned char *core_scsi3_pr_dump_type(int type
)
2356 case PR_TYPE_WRITE_EXCLUSIVE
:
2357 return "Write Exclusive Access";
2358 case PR_TYPE_EXCLUSIVE_ACCESS
:
2359 return "Exclusive Access";
2360 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2361 return "Write Exclusive Access, Registrants Only";
2362 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2363 return "Exclusive Access, Registrants Only";
2364 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2365 return "Write Exclusive Access, All Registrants";
2366 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2367 return "Exclusive Access, All Registrants";
2372 return "Unknown SPC-3 PR Type";
2375 static int core_scsi3_pro_reserve(
2377 struct se_device
*dev
,
2382 struct se_session
*se_sess
= SE_SESS(cmd
);
2383 struct se_dev_entry
*se_deve
;
2384 struct se_lun
*se_lun
= SE_LUN(cmd
);
2385 struct se_portal_group
*se_tpg
;
2386 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2387 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
2388 char i_buf
[PR_REG_ISID_ID_LEN
];
2391 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2393 if (!(se_sess
) || !(se_lun
)) {
2394 printk(KERN_ERR
"SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2395 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2397 se_tpg
= se_sess
->se_tpg
;
2398 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2400 * Locate the existing *pr_reg via struct se_node_acl pointers
2402 pr_reg
= core_scsi3_locate_pr_reg(SE_DEV(cmd
), se_sess
->se_node_acl
,
2405 printk(KERN_ERR
"SPC-3 PR: Unable to locate"
2406 " PR_REGISTERED *pr_reg for RESERVE\n");
2407 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2410 * From spc4r17 Section 5.7.9: Reserving:
2412 * An application client creates a persistent reservation by issuing
2413 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2414 * a registered I_T nexus with the following parameters:
2415 * a) RESERVATION KEY set to the value of the reservation key that is
2416 * registered with the logical unit for the I_T nexus; and
2418 if (res_key
!= pr_reg
->pr_res_key
) {
2419 printk(KERN_ERR
"SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2420 " does not match existing SA REGISTER res_key:"
2421 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2422 core_scsi3_put_pr_reg(pr_reg
);
2423 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2426 * From spc4r17 Section 5.7.9: Reserving:
2429 * b) TYPE field and SCOPE field set to the persistent reservation
2432 * Only one persistent reservation is allowed at a time per logical unit
2433 * and that persistent reservation has a scope of LU_SCOPE.
2435 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2436 printk(KERN_ERR
"SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2437 core_scsi3_put_pr_reg(pr_reg
);
2438 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2441 * See if we have an existing PR reservation holder pointer at
2442 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2445 spin_lock(&dev
->dev_reservation_lock
);
2446 pr_res_holder
= dev
->dev_pr_res_holder
;
2447 if ((pr_res_holder
)) {
2449 * From spc4r17 Section 5.7.9: Reserving:
2451 * If the device server receives a PERSISTENT RESERVE OUT
2452 * command from an I_T nexus other than a persistent reservation
2453 * holder (see 5.7.10) that attempts to create a persistent
2454 * reservation when a persistent reservation already exists for
2455 * the logical unit, then the command shall be completed with
2456 * RESERVATION CONFLICT status.
2458 if (pr_res_holder
!= pr_reg
) {
2459 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2460 printk(KERN_ERR
"SPC-3 PR: Attempted RESERVE from"
2461 " [%s]: %s while reservation already held by"
2462 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2463 CMD_TFO(cmd
)->get_fabric_name(),
2464 se_sess
->se_node_acl
->initiatorname
,
2465 TPG_TFO(pr_res_nacl
->se_tpg
)->get_fabric_name(),
2466 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2468 spin_unlock(&dev
->dev_reservation_lock
);
2469 core_scsi3_put_pr_reg(pr_reg
);
2470 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2473 * From spc4r17 Section 5.7.9: Reserving:
2475 * If a persistent reservation holder attempts to modify the
2476 * type or scope of an existing persistent reservation, the
2477 * command shall be completed with RESERVATION CONFLICT status.
2479 if ((pr_res_holder
->pr_res_type
!= type
) ||
2480 (pr_res_holder
->pr_res_scope
!= scope
)) {
2481 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2482 printk(KERN_ERR
"SPC-3 PR: Attempted RESERVE from"
2483 " [%s]: %s trying to change TYPE and/or SCOPE,"
2484 " while reservation already held by [%s]: %s,"
2485 " returning RESERVATION_CONFLICT\n",
2486 CMD_TFO(cmd
)->get_fabric_name(),
2487 se_sess
->se_node_acl
->initiatorname
,
2488 TPG_TFO(pr_res_nacl
->se_tpg
)->get_fabric_name(),
2489 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2491 spin_unlock(&dev
->dev_reservation_lock
);
2492 core_scsi3_put_pr_reg(pr_reg
);
2493 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2496 * From spc4r17 Section 5.7.9: Reserving:
2498 * If the device server receives a PERSISTENT RESERVE OUT
2499 * command with RESERVE service action where the TYPE field and
2500 * the SCOPE field contain the same values as the existing type
2501 * and scope from a persistent reservation holder, it shall not
2502 * make any change to the existing persistent reservation and
2503 * shall completethe command with GOOD status.
2505 spin_unlock(&dev
->dev_reservation_lock
);
2506 core_scsi3_put_pr_reg(pr_reg
);
2507 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2510 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2511 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2513 pr_reg
->pr_res_scope
= scope
;
2514 pr_reg
->pr_res_type
= type
;
2515 pr_reg
->pr_res_holder
= 1;
2516 dev
->dev_pr_res_holder
= pr_reg
;
2517 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2518 PR_REG_ISID_ID_LEN
);
2520 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: RESERVE created new"
2521 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2522 CMD_TFO(cmd
)->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2523 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2524 printk(KERN_INFO
"SPC-3 PR [%s] RESERVE Node: %s%s\n",
2525 CMD_TFO(cmd
)->get_fabric_name(),
2526 se_sess
->se_node_acl
->initiatorname
,
2527 (prf_isid
) ? &i_buf
[0] : "");
2528 spin_unlock(&dev
->dev_reservation_lock
);
2530 if (pr_tmpl
->pr_aptpl_active
) {
2531 ret
= core_scsi3_update_and_write_aptpl(SE_DEV(cmd
),
2532 &pr_reg
->pr_aptpl_buf
[0],
2533 pr_tmpl
->pr_aptpl_buf_len
);
2535 printk(KERN_INFO
"SPC-3 PR: Updated APTPL metadata"
2539 core_scsi3_put_pr_reg(pr_reg
);
2543 static int core_scsi3_emulate_pro_reserve(
2549 struct se_device
*dev
= cmd
->se_dev
;
2553 case PR_TYPE_WRITE_EXCLUSIVE
:
2554 case PR_TYPE_EXCLUSIVE_ACCESS
:
2555 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2556 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2557 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2558 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2559 ret
= core_scsi3_pro_reserve(cmd
, dev
, type
, scope
, res_key
);
2562 printk(KERN_ERR
"SPC-3 PR: Unknown Service Action RESERVE Type:"
2564 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
2571 * Called with struct se_device->dev_reservation_lock held.
2573 static void __core_scsi3_complete_pro_release(
2574 struct se_device
*dev
,
2575 struct se_node_acl
*se_nacl
,
2576 struct t10_pr_registration
*pr_reg
,
2579 struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2580 char i_buf
[PR_REG_ISID_ID_LEN
];
2583 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2584 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2585 PR_REG_ISID_ID_LEN
);
2587 * Go ahead and release the current PR reservation holder.
2589 dev
->dev_pr_res_holder
= NULL
;
2591 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2592 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2593 tfo
->get_fabric_name(), (explict
) ? "explict" : "implict",
2594 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
2595 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2596 printk(KERN_INFO
"SPC-3 PR [%s] RELEASE Node: %s%s\n",
2597 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2598 (prf_isid
) ? &i_buf
[0] : "");
2600 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2602 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2605 static int core_scsi3_emulate_pro_release(
2611 struct se_device
*dev
= cmd
->se_dev
;
2612 struct se_session
*se_sess
= SE_SESS(cmd
);
2613 struct se_lun
*se_lun
= SE_LUN(cmd
);
2614 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2615 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
2616 int ret
, all_reg
= 0;
2618 if (!(se_sess
) || !(se_lun
)) {
2619 printk(KERN_ERR
"SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2620 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2623 * Locate the existing *pr_reg via struct se_node_acl pointers
2625 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2627 printk(KERN_ERR
"SPC-3 PR: Unable to locate"
2628 " PR_REGISTERED *pr_reg for RELEASE\n");
2629 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2632 * From spc4r17 Section 5.7.11.2 Releasing:
2634 * If there is no persistent reservation or in response to a persistent
2635 * reservation release request from a registered I_T nexus that is not a
2636 * persistent reservation holder (see 5.7.10), the device server shall
2639 * a) Not release the persistent reservation, if any;
2640 * b) Not remove any registrations; and
2641 * c) Complete the command with GOOD status.
2643 spin_lock(&dev
->dev_reservation_lock
);
2644 pr_res_holder
= dev
->dev_pr_res_holder
;
2645 if (!(pr_res_holder
)) {
2647 * No persistent reservation, return GOOD status.
2649 spin_unlock(&dev
->dev_reservation_lock
);
2650 core_scsi3_put_pr_reg(pr_reg
);
2651 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2653 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2654 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
2657 if ((all_reg
== 0) && (pr_res_holder
!= pr_reg
)) {
2659 * Non 'All Registrants' PR Type cases..
2660 * Release request from a registered I_T nexus that is not a
2661 * persistent reservation holder. return GOOD status.
2663 spin_unlock(&dev
->dev_reservation_lock
);
2664 core_scsi3_put_pr_reg(pr_reg
);
2665 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
2668 * From spc4r17 Section 5.7.11.2 Releasing:
2670 * Only the persistent reservation holder (see 5.7.10) is allowed to
2671 * release a persistent reservation.
2673 * An application client releases the persistent reservation by issuing
2674 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2675 * an I_T nexus that is a persistent reservation holder with the
2676 * following parameters:
2678 * a) RESERVATION KEY field set to the value of the reservation key
2679 * that is registered with the logical unit for the I_T nexus;
2681 if (res_key
!= pr_reg
->pr_res_key
) {
2682 printk(KERN_ERR
"SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2683 " does not match existing SA REGISTER res_key:"
2684 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2685 spin_unlock(&dev
->dev_reservation_lock
);
2686 core_scsi3_put_pr_reg(pr_reg
);
2687 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2690 * From spc4r17 Section 5.7.11.2 Releasing and above:
2692 * b) TYPE field and SCOPE field set to match the persistent
2693 * reservation being released.
2695 if ((pr_res_holder
->pr_res_type
!= type
) ||
2696 (pr_res_holder
->pr_res_scope
!= scope
)) {
2697 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2698 printk(KERN_ERR
"SPC-3 PR RELEASE: Attempted to release"
2699 " reservation from [%s]: %s with different TYPE "
2700 "and/or SCOPE while reservation already held by"
2701 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2702 CMD_TFO(cmd
)->get_fabric_name(),
2703 se_sess
->se_node_acl
->initiatorname
,
2704 TPG_TFO(pr_res_nacl
->se_tpg
)->get_fabric_name(),
2705 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2707 spin_unlock(&dev
->dev_reservation_lock
);
2708 core_scsi3_put_pr_reg(pr_reg
);
2709 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2712 * In response to a persistent reservation release request from the
2713 * persistent reservation holder the device server shall perform a
2714 * release by doing the following as an uninterrupted series of actions:
2715 * a) Release the persistent reservation;
2716 * b) Not remove any registration(s);
2717 * c) If the released persistent reservation is a registrants only type
2718 * or all registrants type persistent reservation,
2719 * the device server shall establish a unit attention condition for
2720 * the initiator port associated with every regis-
2721 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2722 * RESERVE OUT command with RELEASE service action was received,
2723 * with the additional sense code set to RESERVATIONS RELEASED; and
2724 * d) If the persistent reservation is of any other type, the device
2725 * server shall not establish a unit attention condition.
2727 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2730 spin_unlock(&dev
->dev_reservation_lock
);
2732 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2733 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2734 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2735 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2737 * If no UNIT ATTENTION conditions will be established for
2738 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2739 * go ahead and check for APTPL=1 update+write below
2744 spin_lock(&pr_tmpl
->registration_lock
);
2745 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2748 * Do not establish a UNIT ATTENTION condition
2749 * for the calling I_T Nexus
2751 if (pr_reg_p
== pr_reg
)
2754 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2755 pr_reg_p
->pr_res_mapped_lun
,
2756 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2758 spin_unlock(&pr_tmpl
->registration_lock
);
2761 if (pr_tmpl
->pr_aptpl_active
) {
2762 ret
= core_scsi3_update_and_write_aptpl(SE_DEV(cmd
),
2763 &pr_reg
->pr_aptpl_buf
[0],
2764 pr_tmpl
->pr_aptpl_buf_len
);
2766 printk("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2769 core_scsi3_put_pr_reg(pr_reg
);
2773 static int core_scsi3_emulate_pro_clear(
2777 struct se_device
*dev
= cmd
->se_dev
;
2778 struct se_node_acl
*pr_reg_nacl
;
2779 struct se_session
*se_sess
= SE_SESS(cmd
);
2780 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
2781 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2782 u32 pr_res_mapped_lun
= 0;
2783 int calling_it_nexus
= 0;
2785 * Locate the existing *pr_reg via struct se_node_acl pointers
2787 pr_reg_n
= core_scsi3_locate_pr_reg(SE_DEV(cmd
),
2788 se_sess
->se_node_acl
, se_sess
);
2790 printk(KERN_ERR
"SPC-3 PR: Unable to locate"
2791 " PR_REGISTERED *pr_reg for CLEAR\n");
2792 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2795 * From spc4r17 section 5.7.11.6, Clearing:
2797 * Any application client may release the persistent reservation and
2798 * remove all registrations from a device server by issuing a
2799 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2800 * registered I_T nexus with the following parameter:
2802 * a) RESERVATION KEY field set to the value of the reservation key
2803 * that is registered with the logical unit for the I_T nexus.
2805 if (res_key
!= pr_reg_n
->pr_res_key
) {
2806 printk(KERN_ERR
"SPC-3 PR REGISTER: Received"
2807 " res_key: 0x%016Lx does not match"
2808 " existing SA REGISTER res_key:"
2809 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2810 core_scsi3_put_pr_reg(pr_reg_n
);
2811 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2814 * a) Release the persistent reservation, if any;
2816 spin_lock(&dev
->dev_reservation_lock
);
2817 pr_res_holder
= dev
->dev_pr_res_holder
;
2818 if (pr_res_holder
) {
2819 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2820 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2823 spin_unlock(&dev
->dev_reservation_lock
);
2825 * b) Remove all registration(s) (see spc4r17 5.7.7);
2827 spin_lock(&pr_tmpl
->registration_lock
);
2828 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2829 &pr_tmpl
->registration_list
, pr_reg_list
) {
2831 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2832 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2833 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2834 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2837 * e) Establish a unit attention condition for the initiator
2838 * port associated with every registered I_T nexus other
2839 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2840 * command with CLEAR service action was received, with the
2841 * additional sense code set to RESERVATIONS PREEMPTED.
2843 if (!(calling_it_nexus
))
2844 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2845 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2847 spin_unlock(&pr_tmpl
->registration_lock
);
2849 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: CLEAR complete\n",
2850 CMD_TFO(cmd
)->get_fabric_name());
2852 if (pr_tmpl
->pr_aptpl_active
) {
2853 core_scsi3_update_and_write_aptpl(SE_DEV(cmd
), NULL
, 0);
2854 printk(KERN_INFO
"SPC-3 PR: Updated APTPL metadata"
2858 core_scsi3_pr_generation(dev
);
2863 * Called with struct se_device->dev_reservation_lock held.
2865 static void __core_scsi3_complete_pro_preempt(
2866 struct se_device
*dev
,
2867 struct t10_pr_registration
*pr_reg
,
2868 struct list_head
*preempt_and_abort_list
,
2873 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2874 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2875 char i_buf
[PR_REG_ISID_ID_LEN
];
2878 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2879 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2880 PR_REG_ISID_ID_LEN
);
2882 * Do an implict RELEASE of the existing reservation.
2884 if (dev
->dev_pr_res_holder
)
2885 __core_scsi3_complete_pro_release(dev
, nacl
,
2886 dev
->dev_pr_res_holder
, 0);
2888 dev
->dev_pr_res_holder
= pr_reg
;
2889 pr_reg
->pr_res_holder
= 1;
2890 pr_reg
->pr_res_type
= type
;
2891 pr_reg
->pr_res_scope
= scope
;
2893 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2894 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2895 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2896 core_scsi3_pr_dump_type(type
),
2897 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2898 printk(KERN_INFO
"SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2899 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2900 nacl
->initiatorname
, (prf_isid
) ? &i_buf
[0] : "");
2902 * For PREEMPT_AND_ABORT, add the preempting reservation's
2903 * struct t10_pr_registration to the list that will be compared
2904 * against received CDBs..
2906 if (preempt_and_abort_list
)
2907 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2908 preempt_and_abort_list
);
2911 static void core_scsi3_release_preempt_and_abort(
2912 struct list_head
*preempt_and_abort_list
,
2913 struct t10_pr_registration
*pr_reg_holder
)
2915 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2917 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2918 pr_reg_abort_list
) {
2920 list_del(&pr_reg
->pr_reg_abort_list
);
2921 if (pr_reg_holder
== pr_reg
)
2923 if (pr_reg
->pr_res_holder
) {
2924 printk(KERN_WARNING
"pr_reg->pr_res_holder still set\n");
2928 pr_reg
->pr_reg_deve
= NULL
;
2929 pr_reg
->pr_reg_nacl
= NULL
;
2930 kfree(pr_reg
->pr_aptpl_buf
);
2931 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2935 int core_scsi3_check_cdb_abort_and_preempt(
2936 struct list_head
*preempt_and_abort_list
,
2939 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2941 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2942 pr_reg_abort_list
) {
2943 if (pr_reg
->pr_res_key
== cmd
->pr_res_key
)
2950 static int core_scsi3_pro_preempt(
2958 struct se_device
*dev
= SE_DEV(cmd
);
2959 struct se_dev_entry
*se_deve
;
2960 struct se_node_acl
*pr_reg_nacl
;
2961 struct se_session
*se_sess
= SE_SESS(cmd
);
2962 struct list_head preempt_and_abort_list
;
2963 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2964 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
2965 u32 pr_res_mapped_lun
= 0;
2966 int all_reg
= 0, calling_it_nexus
= 0, released_regs
= 0;
2967 int prh_type
= 0, prh_scope
= 0, ret
;
2970 return PYX_TRANSPORT_LU_COMM_FAILURE
;
2972 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2973 pr_reg_n
= core_scsi3_locate_pr_reg(SE_DEV(cmd
), se_sess
->se_node_acl
,
2976 printk(KERN_ERR
"SPC-3 PR: Unable to locate"
2977 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2978 (abort
) ? "_AND_ABORT" : "");
2979 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2981 if (pr_reg_n
->pr_res_key
!= res_key
) {
2982 core_scsi3_put_pr_reg(pr_reg_n
);
2983 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
2985 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2986 printk(KERN_ERR
"SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2987 core_scsi3_put_pr_reg(pr_reg_n
);
2988 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
2990 INIT_LIST_HEAD(&preempt_and_abort_list
);
2992 spin_lock(&dev
->dev_reservation_lock
);
2993 pr_res_holder
= dev
->dev_pr_res_holder
;
2994 if (pr_res_holder
&&
2995 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2996 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
2999 if (!(all_reg
) && !(sa_res_key
)) {
3000 spin_unlock(&dev
->dev_reservation_lock
);
3001 core_scsi3_put_pr_reg(pr_reg_n
);
3002 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3005 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3007 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3008 * persistent reservation holder or there is no persistent reservation
3009 * holder (i.e., there is no persistent reservation), then the device
3010 * server shall perform a preempt by doing the following in an
3011 * uninterrupted series of actions. (See below..)
3013 if (!(pr_res_holder
) || (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
3015 * No existing or SA Reservation Key matching reservations..
3017 * PROUT SA PREEMPT with All Registrant type reservations are
3018 * allowed to be processed without a matching SA Reservation Key
3020 spin_lock(&pr_tmpl
->registration_lock
);
3021 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3022 &pr_tmpl
->registration_list
, pr_reg_list
) {
3024 * Removing of registrations in non all registrants
3025 * type reservations without a matching SA reservation
3028 * a) Remove the registrations for all I_T nexuses
3029 * specified by the SERVICE ACTION RESERVATION KEY
3031 * b) Ignore the contents of the SCOPE and TYPE fields;
3032 * c) Process tasks as defined in 5.7.1; and
3033 * d) Establish a unit attention condition for the
3034 * initiator port associated with every I_T nexus
3035 * that lost its registration other than the I_T
3036 * nexus on which the PERSISTENT RESERVE OUT command
3037 * was received, with the additional sense code set
3038 * to REGISTRATIONS PREEMPTED.
3041 if (pr_reg
->pr_res_key
!= sa_res_key
)
3044 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3045 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3046 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3047 __core_scsi3_free_registration(dev
, pr_reg
,
3048 (abort
) ? &preempt_and_abort_list
:
3049 NULL
, calling_it_nexus
);
3053 * Case for any existing all registrants type
3054 * reservation, follow logic in spc4r17 section
3055 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3057 * For a ZERO SA Reservation key, release
3058 * all other registrations and do an implict
3059 * release of active persistent reservation.
3061 * For a non-ZERO SA Reservation key, only
3062 * release the matching reservation key from
3066 (pr_reg
->pr_res_key
!= sa_res_key
))
3069 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3070 if (calling_it_nexus
)
3073 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3074 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3075 __core_scsi3_free_registration(dev
, pr_reg
,
3076 (abort
) ? &preempt_and_abort_list
:
3080 if (!(calling_it_nexus
))
3081 core_scsi3_ua_allocate(pr_reg_nacl
,
3082 pr_res_mapped_lun
, 0x2A,
3083 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3085 spin_unlock(&pr_tmpl
->registration_lock
);
3087 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3088 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3089 * RESERVATION KEY field to a value that does not match any
3090 * registered reservation key, then the device server shall
3091 * complete the command with RESERVATION CONFLICT status.
3093 if (!(released_regs
)) {
3094 spin_unlock(&dev
->dev_reservation_lock
);
3095 core_scsi3_put_pr_reg(pr_reg_n
);
3096 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
3099 * For an existing all registrants type reservation
3100 * with a zero SA rservation key, preempt the existing
3101 * reservation with the new PR type and scope.
3103 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
3104 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3105 (abort
) ? &preempt_and_abort_list
: NULL
,
3106 type
, scope
, abort
);
3109 core_scsi3_release_preempt_and_abort(
3110 &preempt_and_abort_list
, pr_reg_n
);
3112 spin_unlock(&dev
->dev_reservation_lock
);
3114 if (pr_tmpl
->pr_aptpl_active
) {
3115 ret
= core_scsi3_update_and_write_aptpl(SE_DEV(cmd
),
3116 &pr_reg_n
->pr_aptpl_buf
[0],
3117 pr_tmpl
->pr_aptpl_buf_len
);
3119 printk(KERN_INFO
"SPC-3 PR: Updated APTPL"
3120 " metadata for PREEMPT%s\n", (abort
) ?
3124 core_scsi3_put_pr_reg(pr_reg_n
);
3125 core_scsi3_pr_generation(SE_DEV(cmd
));
3129 * The PREEMPTing SA reservation key matches that of the
3130 * existing persistent reservation, first, we check if
3131 * we are preempting our own reservation.
3132 * From spc4r17, section 5.7.11.4.3 Preempting
3133 * persistent reservations and registration handling
3135 * If an all registrants persistent reservation is not
3136 * present, it is not an error for the persistent
3137 * reservation holder to preempt itself (i.e., a
3138 * PERSISTENT RESERVE OUT with a PREEMPT service action
3139 * or a PREEMPT AND ABORT service action with the
3140 * SERVICE ACTION RESERVATION KEY value equal to the
3141 * persistent reservation holder's reservation key that
3142 * is received from the persistent reservation holder).
3143 * In that case, the device server shall establish the
3144 * new persistent reservation and maintain the
3147 prh_type
= pr_res_holder
->pr_res_type
;
3148 prh_scope
= pr_res_holder
->pr_res_scope
;
3150 * If the SERVICE ACTION RESERVATION KEY field identifies a
3151 * persistent reservation holder (see 5.7.10), the device
3152 * server shall perform a preempt by doing the following as
3153 * an uninterrupted series of actions:
3155 * a) Release the persistent reservation for the holder
3156 * identified by the SERVICE ACTION RESERVATION KEY field;
3158 if (pr_reg_n
!= pr_res_holder
)
3159 __core_scsi3_complete_pro_release(dev
,
3160 pr_res_holder
->pr_reg_nacl
,
3161 dev
->dev_pr_res_holder
, 0);
3163 * b) Remove the registrations for all I_T nexuses identified
3164 * by the SERVICE ACTION RESERVATION KEY field, except the
3165 * I_T nexus that is being used for the PERSISTENT RESERVE
3166 * OUT command. If an all registrants persistent reservation
3167 * is present and the SERVICE ACTION RESERVATION KEY field
3168 * is set to zero, then all registrations shall be removed
3169 * except for that of the I_T nexus that is being used for
3170 * the PERSISTENT RESERVE OUT command;
3172 spin_lock(&pr_tmpl
->registration_lock
);
3173 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3174 &pr_tmpl
->registration_list
, pr_reg_list
) {
3176 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3177 if (calling_it_nexus
)
3180 if (pr_reg
->pr_res_key
!= sa_res_key
)
3183 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3184 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3185 __core_scsi3_free_registration(dev
, pr_reg
,
3186 (abort
) ? &preempt_and_abort_list
: NULL
,
3189 * e) Establish a unit attention condition for the initiator
3190 * port associated with every I_T nexus that lost its
3191 * persistent reservation and/or registration, with the
3192 * additional sense code set to REGISTRATIONS PREEMPTED;
3194 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3195 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3197 spin_unlock(&pr_tmpl
->registration_lock
);
3199 * c) Establish a persistent reservation for the preempting
3200 * I_T nexus using the contents of the SCOPE and TYPE fields;
3202 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3203 (abort
) ? &preempt_and_abort_list
: NULL
,
3204 type
, scope
, abort
);
3206 * d) Process tasks as defined in 5.7.1;
3208 * f) If the type or scope has changed, then for every I_T nexus
3209 * whose reservation key was not removed, except for the I_T
3210 * nexus on which the PERSISTENT RESERVE OUT command was
3211 * received, the device server shall establish a unit
3212 * attention condition for the initiator port associated with
3213 * that I_T nexus, with the additional sense code set to
3214 * RESERVATIONS RELEASED. If the type or scope have not
3215 * changed, then no unit attention condition(s) shall be
3216 * established for this reason.
3218 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3219 spin_lock(&pr_tmpl
->registration_lock
);
3220 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3221 &pr_tmpl
->registration_list
, pr_reg_list
) {
3223 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3224 if (calling_it_nexus
)
3227 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3228 pr_reg
->pr_res_mapped_lun
, 0x2A,
3229 ASCQ_2AH_RESERVATIONS_RELEASED
);
3231 spin_unlock(&pr_tmpl
->registration_lock
);
3233 spin_unlock(&dev
->dev_reservation_lock
);
3235 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3236 * All received CDBs for the matching existing reservation and
3237 * registrations undergo ABORT_TASK logic.
3239 * From there, core_scsi3_release_preempt_and_abort() will
3240 * release every registration in the list (which have already
3241 * been removed from the primary pr_reg list), except the
3242 * new persistent reservation holder, the calling Initiator Port.
3245 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3246 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3250 if (pr_tmpl
->pr_aptpl_active
) {
3251 ret
= core_scsi3_update_and_write_aptpl(SE_DEV(cmd
),
3252 &pr_reg_n
->pr_aptpl_buf
[0],
3253 pr_tmpl
->pr_aptpl_buf_len
);
3255 printk("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3256 "%s\n", (abort
) ? "_AND_ABORT" : "");
3259 core_scsi3_put_pr_reg(pr_reg_n
);
3260 core_scsi3_pr_generation(SE_DEV(cmd
));
3264 static int core_scsi3_emulate_pro_preempt(
3275 case PR_TYPE_WRITE_EXCLUSIVE
:
3276 case PR_TYPE_EXCLUSIVE_ACCESS
:
3277 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3278 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3279 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3280 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3281 ret
= core_scsi3_pro_preempt(cmd
, type
, scope
,
3282 res_key
, sa_res_key
, abort
);
3285 printk(KERN_ERR
"SPC-3 PR: Unknown Service Action PREEMPT%s"
3286 " Type: 0x%02x\n", (abort
) ? "_AND_ABORT" : "", type
);
3287 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3294 static int core_scsi3_emulate_pro_register_and_move(
3301 struct se_session
*se_sess
= SE_SESS(cmd
);
3302 struct se_device
*dev
= SE_DEV(cmd
);
3303 struct se_dev_entry
*se_deve
, *dest_se_deve
= NULL
;
3304 struct se_lun
*se_lun
= SE_LUN(cmd
);
3305 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3306 struct se_port
*se_port
;
3307 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3308 struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3309 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3310 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
3311 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
3312 unsigned char *initiator_str
;
3313 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
3314 u32 tid_len
, tmp_tid_len
;
3315 int new_reg
= 0, type
, scope
, ret
, matching_iname
, prf_isid
;
3316 unsigned short rtpi
;
3317 unsigned char proto_ident
;
3319 if (!(se_sess
) || !(se_lun
)) {
3320 printk(KERN_ERR
"SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3321 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3323 memset(dest_iport
, 0, 64);
3324 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3325 se_tpg
= se_sess
->se_tpg
;
3326 tf_ops
= TPG_TFO(se_tpg
);
3327 se_deve
= &se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
3329 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3330 * Register behaviors for a REGISTER AND MOVE service action
3332 * Locate the existing *pr_reg via struct se_node_acl pointers
3334 pr_reg
= core_scsi3_locate_pr_reg(SE_DEV(cmd
), se_sess
->se_node_acl
,
3337 printk(KERN_ERR
"SPC-3 PR: Unable to locate PR_REGISTERED"
3338 " *pr_reg for REGISTER_AND_MOVE\n");
3339 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3342 * The provided reservation key much match the existing reservation key
3343 * provided during this initiator's I_T nexus registration.
3345 if (res_key
!= pr_reg
->pr_res_key
) {
3346 printk(KERN_WARNING
"SPC-3 PR REGISTER_AND_MOVE: Received"
3347 " res_key: 0x%016Lx does not match existing SA REGISTER"
3348 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3349 core_scsi3_put_pr_reg(pr_reg
);
3350 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
3353 * The service active reservation key needs to be non zero
3355 if (!(sa_res_key
)) {
3356 printk(KERN_WARNING
"SPC-3 PR REGISTER_AND_MOVE: Received zero"
3358 core_scsi3_put_pr_reg(pr_reg
);
3359 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3362 * Determine the Relative Target Port Identifier where the reservation
3363 * will be moved to for the TransportID containing SCSI initiator WWN
3366 rtpi
= (buf
[18] & 0xff) << 8;
3367 rtpi
|= buf
[19] & 0xff;
3368 tid_len
= (buf
[20] & 0xff) << 24;
3369 tid_len
|= (buf
[21] & 0xff) << 16;
3370 tid_len
|= (buf
[22] & 0xff) << 8;
3371 tid_len
|= buf
[23] & 0xff;
3373 if ((tid_len
+ 24) != cmd
->data_length
) {
3374 printk(KERN_ERR
"SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3375 " does not equal CDB data_length: %u\n", tid_len
,
3377 core_scsi3_put_pr_reg(pr_reg
);
3378 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3381 spin_lock(&dev
->se_port_lock
);
3382 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3383 if (se_port
->sep_rtpi
!= rtpi
)
3385 dest_se_tpg
= se_port
->sep_tpg
;
3388 dest_tf_ops
= TPG_TFO(dest_se_tpg
);
3392 atomic_inc(&dest_se_tpg
->tpg_pr_ref_count
);
3393 smp_mb__after_atomic_inc();
3394 spin_unlock(&dev
->se_port_lock
);
3396 ret
= core_scsi3_tpg_depend_item(dest_se_tpg
);
3398 printk(KERN_ERR
"core_scsi3_tpg_depend_item() failed"
3399 " for dest_se_tpg\n");
3400 atomic_dec(&dest_se_tpg
->tpg_pr_ref_count
);
3401 smp_mb__after_atomic_dec();
3402 core_scsi3_put_pr_reg(pr_reg
);
3403 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3406 spin_lock(&dev
->se_port_lock
);
3409 spin_unlock(&dev
->se_port_lock
);
3411 if (!(dest_se_tpg
) || (!dest_tf_ops
)) {
3412 printk(KERN_ERR
"SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3413 " fabric ops from Relative Target Port Identifier:"
3415 core_scsi3_put_pr_reg(pr_reg
);
3416 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3418 proto_ident
= (buf
[24] & 0x0f);
3420 printk("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3421 " 0x%02x\n", proto_ident
);
3423 if (proto_ident
!= dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
)) {
3424 printk(KERN_ERR
"SPC-3 PR REGISTER_AND_MOVE: Received"
3425 " proto_ident: 0x%02x does not match ident: 0x%02x"
3426 " from fabric: %s\n", proto_ident
,
3427 dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
),
3428 dest_tf_ops
->get_fabric_name());
3429 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3432 if (dest_tf_ops
->tpg_parse_pr_out_transport_id
== NULL
) {
3433 printk(KERN_ERR
"SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3434 " containg a valid tpg_parse_pr_out_transport_id"
3435 " function pointer\n");
3436 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
3439 initiator_str
= dest_tf_ops
->tpg_parse_pr_out_transport_id(dest_se_tpg
,
3440 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3441 if (!(initiator_str
)) {
3442 printk(KERN_ERR
"SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3443 " initiator_str from Transport ID\n");
3444 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3448 printk(KERN_INFO
"SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3449 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3450 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3453 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3454 * action specifies a TransportID that is the same as the initiator port
3455 * of the I_T nexus for the command received, then the command shall
3456 * be terminated with CHECK CONDITION status, with the sense key set to
3457 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3458 * IN PARAMETER LIST.
3460 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3461 matching_iname
= (!strcmp(initiator_str
,
3462 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3463 if (!(matching_iname
))
3464 goto after_iport_check
;
3466 if (!(iport_ptr
) || !(pr_reg
->isid_present_at_reg
)) {
3467 printk(KERN_ERR
"SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3468 " matches: %s on received I_T Nexus\n", initiator_str
,
3469 pr_reg_nacl
->initiatorname
);
3470 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3473 if (!(strcmp(iport_ptr
, pr_reg
->pr_reg_isid
))) {
3474 printk(KERN_ERR
"SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3475 " matches: %s %s on received I_T Nexus\n",
3476 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3477 pr_reg
->pr_reg_isid
);
3478 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3483 * Locate the destination struct se_node_acl from the received Transport ID
3485 spin_lock_bh(&dest_se_tpg
->acl_node_lock
);
3486 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3488 if (dest_node_acl
) {
3489 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
3490 smp_mb__after_atomic_inc();
3492 spin_unlock_bh(&dest_se_tpg
->acl_node_lock
);
3494 if (!(dest_node_acl
)) {
3495 printk(KERN_ERR
"Unable to locate %s dest_node_acl for"
3496 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3498 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3501 ret
= core_scsi3_nodeacl_depend_item(dest_node_acl
);
3503 printk(KERN_ERR
"core_scsi3_nodeacl_depend_item() for"
3504 " dest_node_acl\n");
3505 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
3506 smp_mb__after_atomic_dec();
3507 dest_node_acl
= NULL
;
3508 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
3512 printk(KERN_INFO
"SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3513 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3514 dest_node_acl
->initiatorname
);
3517 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3520 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3521 if (!(dest_se_deve
)) {
3522 printk(KERN_ERR
"Unable to locate %s dest_se_deve from RTPI:"
3523 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3524 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3528 ret
= core_scsi3_lunacl_depend_item(dest_se_deve
);
3530 printk(KERN_ERR
"core_scsi3_lunacl_depend_item() failed\n");
3531 atomic_dec(&dest_se_deve
->pr_ref_count
);
3532 smp_mb__after_atomic_dec();
3533 dest_se_deve
= NULL
;
3534 ret
= PYX_TRANSPORT_LU_COMM_FAILURE
;
3538 printk(KERN_INFO
"SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3539 " ACL for dest_se_deve->mapped_lun: %u\n",
3540 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3541 dest_se_deve
->mapped_lun
);
3544 * A persistent reservation needs to already existing in order to
3545 * successfully complete the REGISTER_AND_MOVE service action..
3547 spin_lock(&dev
->dev_reservation_lock
);
3548 pr_res_holder
= dev
->dev_pr_res_holder
;
3549 if (!(pr_res_holder
)) {
3550 printk(KERN_WARNING
"SPC-3 PR REGISTER_AND_MOVE: No reservation"
3551 " currently held\n");
3552 spin_unlock(&dev
->dev_reservation_lock
);
3553 ret
= PYX_TRANSPORT_INVALID_CDB_FIELD
;
3557 * The received on I_T Nexus must be the reservation holder.
3559 * From spc4r17 section 5.7.8 Table 50 --
3560 * Register behaviors for a REGISTER AND MOVE service action
3562 if (pr_res_holder
!= pr_reg
) {
3563 printk(KERN_WARNING
"SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3564 " Nexus is not reservation holder\n");
3565 spin_unlock(&dev
->dev_reservation_lock
);
3566 ret
= PYX_TRANSPORT_RESERVATION_CONFLICT
;
3570 * From spc4r17 section 5.7.8: registering and moving reservation
3572 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3573 * action is received and the established persistent reservation is a
3574 * Write Exclusive - All Registrants type or Exclusive Access -
3575 * All Registrants type reservation, then the command shall be completed
3576 * with RESERVATION CONFLICT status.
3578 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3579 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3580 printk(KERN_WARNING
"SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3581 " reservation for type: %s\n",
3582 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3583 spin_unlock(&dev
->dev_reservation_lock
);
3584 ret
= PYX_TRANSPORT_RESERVATION_CONFLICT
;
3587 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3589 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3591 type
= pr_res_holder
->pr_res_type
;
3592 scope
= pr_res_holder
->pr_res_type
;
3594 * c) Associate the reservation key specified in the SERVICE ACTION
3595 * RESERVATION KEY field with the I_T nexus specified as the
3596 * destination of the register and move, where:
3597 * A) The I_T nexus is specified by the TransportID and the
3598 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3599 * B) Regardless of the TransportID format used, the association for
3600 * the initiator port is based on either the initiator port name
3601 * (see 3.1.71) on SCSI transport protocols where port names are
3602 * required or the initiator port identifier (see 3.1.70) on SCSI
3603 * transport protocols where port names are not required;
3604 * d) Register the reservation key specified in the SERVICE ACTION
3605 * RESERVATION KEY field;
3606 * e) Retain the reservation key specified in the SERVICE ACTION
3607 * RESERVATION KEY field and associated information;
3609 * Also, It is not an error for a REGISTER AND MOVE service action to
3610 * register an I_T nexus that is already registered with the same
3611 * reservation key or a different reservation key.
3613 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3615 if (!(dest_pr_reg
)) {
3616 ret
= core_scsi3_alloc_registration(SE_DEV(cmd
),
3617 dest_node_acl
, dest_se_deve
, iport_ptr
,
3618 sa_res_key
, 0, aptpl
, 2, 1);
3620 spin_unlock(&dev
->dev_reservation_lock
);
3621 ret
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3624 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3629 * f) Release the persistent reservation for the persistent reservation
3630 * holder (i.e., the I_T nexus on which the
3632 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3633 dev
->dev_pr_res_holder
, 0);
3635 * g) Move the persistent reservation to the specified I_T nexus using
3636 * the same scope and type as the persistent reservation released in
3639 dev
->dev_pr_res_holder
= dest_pr_reg
;
3640 dest_pr_reg
->pr_res_holder
= 1;
3641 dest_pr_reg
->pr_res_type
= type
;
3642 pr_reg
->pr_res_scope
= scope
;
3643 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
3644 PR_REG_ISID_ID_LEN
);
3646 * Increment PRGeneration for existing registrations..
3649 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3650 spin_unlock(&dev
->dev_reservation_lock
);
3652 printk(KERN_INFO
"SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3653 " created new reservation holder TYPE: %s on object RTPI:"
3654 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3655 core_scsi3_pr_dump_type(type
), rtpi
,
3656 dest_pr_reg
->pr_res_generation
);
3657 printk(KERN_INFO
"SPC-3 PR Successfully moved reservation from"
3658 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3659 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3660 (prf_isid
) ? &i_buf
[0] : "", dest_tf_ops
->get_fabric_name(),
3661 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3664 * It is now safe to release configfs group dependencies for destination
3665 * of Transport ID Initiator Device/Port Identifier
3667 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3668 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3669 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3671 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3672 * nexus on which PERSISTENT RESERVE OUT command was received.
3675 spin_lock(&pr_tmpl
->registration_lock
);
3676 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3677 spin_unlock(&pr_tmpl
->registration_lock
);
3679 core_scsi3_put_pr_reg(pr_reg
);
3682 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3683 * write out the updated metadata to struct file for this SCSI device.
3686 pr_tmpl
->pr_aptpl_active
= 0;
3687 core_scsi3_update_and_write_aptpl(SE_DEV(cmd
), NULL
, 0);
3688 printk("SPC-3 PR: Set APTPL Bit Deactivated for"
3689 " REGISTER_AND_MOVE\n");
3691 pr_tmpl
->pr_aptpl_active
= 1;
3692 ret
= core_scsi3_update_and_write_aptpl(SE_DEV(cmd
),
3693 &dest_pr_reg
->pr_aptpl_buf
[0],
3694 pr_tmpl
->pr_aptpl_buf_len
);
3696 printk("SPC-3 PR: Set APTPL Bit Activated for"
3697 " REGISTER_AND_MOVE\n");
3700 core_scsi3_put_pr_reg(dest_pr_reg
);
3704 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3706 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3707 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3708 core_scsi3_put_pr_reg(pr_reg
);
3712 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3714 unsigned int __v1
, __v2
;
3716 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3717 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3719 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3723 * See spc4r17 section 6.14 Table 170
3725 static int core_scsi3_emulate_pr_out(struct se_cmd
*cmd
, unsigned char *cdb
)
3727 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
3728 u64 res_key
, sa_res_key
;
3729 int sa
, scope
, type
, aptpl
;
3730 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3732 * FIXME: A NULL struct se_session pointer means an this is not coming from
3733 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3735 if (!(SE_SESS(cmd
)))
3736 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3738 if (cmd
->data_length
< 24) {
3739 printk(KERN_WARNING
"SPC-PR: Received PR OUT parameter list"
3740 " length too small: %u\n", cmd
->data_length
);
3741 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3744 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3746 sa
= (cdb
[1] & 0x1f);
3747 scope
= (cdb
[2] & 0xf0);
3748 type
= (cdb
[2] & 0x0f);
3750 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3752 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3753 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3755 * REGISTER_AND_MOVE uses a different SA parameter list containing
3756 * SCSI TransportIDs.
3758 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3759 spec_i_pt
= (buf
[20] & 0x08);
3760 all_tg_pt
= (buf
[20] & 0x04);
3761 aptpl
= (buf
[20] & 0x01);
3763 aptpl
= (buf
[17] & 0x01);
3764 unreg
= (buf
[17] & 0x02);
3767 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3769 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
))
3770 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3772 * From spc4r17 section 6.14:
3774 * If the SPEC_I_PT bit is set to zero, the service action is not
3775 * REGISTER AND MOVE, and the parameter list length is not 24, then
3776 * the command shall be terminated with CHECK CONDITION status, with
3777 * the sense key set to ILLEGAL REQUEST, and the additional sense
3778 * code set to PARAMETER LIST LENGTH ERROR.
3780 if (!(spec_i_pt
) && ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3781 (cmd
->data_length
!= 24)) {
3782 printk(KERN_WARNING
"SPC-PR: Received PR OUT illegal parameter"
3783 " list length: %u\n", cmd
->data_length
);
3784 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
3787 * (core_scsi3_emulate_pro_* function parameters
3788 * are defined by spc4r17 Table 174:
3789 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3793 return core_scsi3_emulate_pro_register(cmd
,
3794 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 0);
3796 return core_scsi3_emulate_pro_reserve(cmd
,
3797 type
, scope
, res_key
);
3799 return core_scsi3_emulate_pro_release(cmd
,
3800 type
, scope
, res_key
);
3802 return core_scsi3_emulate_pro_clear(cmd
, res_key
);
3804 return core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3805 res_key
, sa_res_key
, 0);
3806 case PRO_PREEMPT_AND_ABORT
:
3807 return core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3808 res_key
, sa_res_key
, 1);
3809 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3810 return core_scsi3_emulate_pro_register(cmd
,
3811 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 1);
3812 case PRO_REGISTER_AND_MOVE
:
3813 return core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3814 sa_res_key
, aptpl
, unreg
);
3816 printk(KERN_ERR
"Unknown PERSISTENT_RESERVE_OUT service"
3817 " action: 0x%02x\n", cdb
[1] & 0x1f);
3818 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3821 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3825 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3827 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3829 static int core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3831 struct se_device
*se_dev
= SE_DEV(cmd
);
3832 struct se_subsystem_dev
*su_dev
= SU_DEV(se_dev
);
3833 struct t10_pr_registration
*pr_reg
;
3834 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
3835 u32 add_len
= 0, off
= 8;
3837 if (cmd
->data_length
< 8) {
3838 printk(KERN_ERR
"PRIN SA READ_KEYS SCSI Data Length: %u"
3839 " too small\n", cmd
->data_length
);
3840 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3843 buf
[0] = ((T10_RES(su_dev
)->pr_generation
>> 24) & 0xff);
3844 buf
[1] = ((T10_RES(su_dev
)->pr_generation
>> 16) & 0xff);
3845 buf
[2] = ((T10_RES(su_dev
)->pr_generation
>> 8) & 0xff);
3846 buf
[3] = (T10_RES(su_dev
)->pr_generation
& 0xff);
3848 spin_lock(&T10_RES(su_dev
)->registration_lock
);
3849 list_for_each_entry(pr_reg
, &T10_RES(su_dev
)->registration_list
,
3852 * Check for overflow of 8byte PRI READ_KEYS payload and
3853 * next reservation key list descriptor.
3855 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3858 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3859 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3860 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3861 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3862 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3863 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3864 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3865 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3869 spin_unlock(&T10_RES(su_dev
)->registration_lock
);
3871 buf
[4] = ((add_len
>> 24) & 0xff);
3872 buf
[5] = ((add_len
>> 16) & 0xff);
3873 buf
[6] = ((add_len
>> 8) & 0xff);
3874 buf
[7] = (add_len
& 0xff);
3880 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3882 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3884 static int core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
3886 struct se_device
*se_dev
= SE_DEV(cmd
);
3887 struct se_subsystem_dev
*su_dev
= SU_DEV(se_dev
);
3888 struct t10_pr_registration
*pr_reg
;
3889 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
3891 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
3893 if (cmd
->data_length
< 8) {
3894 printk(KERN_ERR
"PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3895 " too small\n", cmd
->data_length
);
3896 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3899 buf
[0] = ((T10_RES(su_dev
)->pr_generation
>> 24) & 0xff);
3900 buf
[1] = ((T10_RES(su_dev
)->pr_generation
>> 16) & 0xff);
3901 buf
[2] = ((T10_RES(su_dev
)->pr_generation
>> 8) & 0xff);
3902 buf
[3] = (T10_RES(su_dev
)->pr_generation
& 0xff);
3904 spin_lock(&se_dev
->dev_reservation_lock
);
3905 pr_reg
= se_dev
->dev_pr_res_holder
;
3908 * Set the hardcoded Additional Length
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 if (cmd
->data_length
< 22) {
3916 spin_unlock(&se_dev
->dev_reservation_lock
);
3920 * Set the Reservation key.
3922 * From spc4r17, section 5.7.10:
3923 * A persistent reservation holder has its reservation key
3924 * returned in the parameter data from a PERSISTENT
3925 * RESERVE IN command with READ RESERVATION service action as
3927 * a) For a persistent reservation of the type Write Exclusive
3928 * - All Registrants or Exclusive Access  All Regitrants,
3929 * the reservation key shall be set to zero; or
3930 * b) For all other persistent reservation types, the
3931 * reservation key shall be set to the registered
3932 * reservation key for the I_T nexus that holds the
3933 * persistent reservation.
3935 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3936 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
3939 pr_res_key
= pr_reg
->pr_res_key
;
3941 buf
[8] = ((pr_res_key
>> 56) & 0xff);
3942 buf
[9] = ((pr_res_key
>> 48) & 0xff);
3943 buf
[10] = ((pr_res_key
>> 40) & 0xff);
3944 buf
[11] = ((pr_res_key
>> 32) & 0xff);
3945 buf
[12] = ((pr_res_key
>> 24) & 0xff);
3946 buf
[13] = ((pr_res_key
>> 16) & 0xff);
3947 buf
[14] = ((pr_res_key
>> 8) & 0xff);
3948 buf
[15] = (pr_res_key
& 0xff);
3950 * Set the SCOPE and TYPE
3952 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
3953 (pr_reg
->pr_res_type
& 0x0f);
3955 spin_unlock(&se_dev
->dev_reservation_lock
);
3961 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3963 * See spc4r17 section 6.13.4 Table 165
3965 static int core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
3967 struct se_device
*dev
= SE_DEV(cmd
);
3968 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(dev
)->t10_reservation
;
3969 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
3970 u16 add_len
= 8; /* Hardcoded to 8. */
3972 if (cmd
->data_length
< 6) {
3973 printk(KERN_ERR
"PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3974 " %u too small\n", cmd
->data_length
);
3975 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
3978 buf
[0] = ((add_len
<< 8) & 0xff);
3979 buf
[1] = (add_len
& 0xff);
3980 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3981 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3982 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3983 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3985 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3986 * set the TMV: Task Mask Valid bit.
3990 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3992 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3994 * PTPL_A: Persistence across Target Power Loss Active bit
3996 if (pr_tmpl
->pr_aptpl_active
)
3999 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4001 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4002 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4003 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4004 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4005 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4006 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4012 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4014 * See spc4r17 section 6.13.5 Table 168 and 169
4016 static int core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
4018 struct se_device
*se_dev
= SE_DEV(cmd
);
4019 struct se_node_acl
*se_nacl
;
4020 struct se_subsystem_dev
*su_dev
= SU_DEV(se_dev
);
4021 struct se_portal_group
*se_tpg
;
4022 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
4023 struct t10_reservation_template
*pr_tmpl
= &SU_DEV(se_dev
)->t10_reservation
;
4024 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
4025 u32 add_desc_len
= 0, add_len
= 0, desc_len
, exp_desc_len
;
4026 u32 off
= 8; /* off into first Full Status descriptor */
4027 int format_code
= 0;
4029 if (cmd
->data_length
< 8) {
4030 printk(KERN_ERR
"PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4031 " too small\n", cmd
->data_length
);
4032 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
4035 buf
[0] = ((T10_RES(su_dev
)->pr_generation
>> 24) & 0xff);
4036 buf
[1] = ((T10_RES(su_dev
)->pr_generation
>> 16) & 0xff);
4037 buf
[2] = ((T10_RES(su_dev
)->pr_generation
>> 8) & 0xff);
4038 buf
[3] = (T10_RES(su_dev
)->pr_generation
& 0xff);
4040 spin_lock(&pr_tmpl
->registration_lock
);
4041 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
4042 &pr_tmpl
->registration_list
, pr_reg_list
) {
4044 se_nacl
= pr_reg
->pr_reg_nacl
;
4045 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
4048 atomic_inc(&pr_reg
->pr_res_holders
);
4049 smp_mb__after_atomic_inc();
4050 spin_unlock(&pr_tmpl
->registration_lock
);
4052 * Determine expected length of $FABRIC_MOD specific
4053 * TransportID full status descriptor..
4055 exp_desc_len
= TPG_TFO(se_tpg
)->tpg_get_pr_transport_id_len(
4056 se_tpg
, se_nacl
, pr_reg
, &format_code
);
4058 if ((exp_desc_len
+ add_len
) > cmd
->data_length
) {
4059 printk(KERN_WARNING
"SPC-3 PRIN READ_FULL_STATUS ran"
4060 " out of buffer: %d\n", cmd
->data_length
);
4061 spin_lock(&pr_tmpl
->registration_lock
);
4062 atomic_dec(&pr_reg
->pr_res_holders
);
4063 smp_mb__after_atomic_dec();
4067 * Set RESERVATION KEY
4069 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
4070 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
4071 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
4072 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4073 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4074 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4075 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4076 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4077 off
+= 4; /* Skip Over Reserved area */
4080 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4082 if (pr_reg
->pr_reg_all_tg_pt
)
4085 * The struct se_lun pointer will be present for the
4086 * reservation holder for PR_HOLDER bit.
4088 * Also, if this registration is the reservation
4089 * holder, fill in SCOPE and TYPE in the next byte.
4091 if (pr_reg
->pr_res_holder
) {
4093 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4094 (pr_reg
->pr_res_type
& 0x0f);
4098 off
+= 4; /* Skip over reserved area */
4100 * From spc4r17 6.3.15:
4102 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4103 * IDENTIFIER field contains the relative port identifier (see
4104 * 3.1.120) of the target port that is part of the I_T nexus
4105 * described by this full status descriptor. If the ALL_TG_PT
4106 * bit is set to one, the contents of the RELATIVE TARGET PORT
4107 * IDENTIFIER field are not defined by this standard.
4109 if (!(pr_reg
->pr_reg_all_tg_pt
)) {
4110 struct se_port
*port
= pr_reg
->pr_reg_tg_pt_lun
->lun_sep
;
4112 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
4113 buf
[off
++] = (port
->sep_rtpi
& 0xff);
4115 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4118 * Now, have the $FABRIC_MOD fill in the protocol identifier
4120 desc_len
= TPG_TFO(se_tpg
)->tpg_get_pr_transport_id(se_tpg
,
4121 se_nacl
, pr_reg
, &format_code
, &buf
[off
+4]);
4123 spin_lock(&pr_tmpl
->registration_lock
);
4124 atomic_dec(&pr_reg
->pr_res_holders
);
4125 smp_mb__after_atomic_dec();
4127 * Set the ADDITIONAL DESCRIPTOR LENGTH
4129 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4130 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4131 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4132 buf
[off
++] = (desc_len
& 0xff);
4134 * Size of full desctipor header minus TransportID
4135 * containing $FABRIC_MOD specific) initiator device/port
4138 * See spc4r17 Section 6.13.5 Table 169
4140 add_desc_len
= (24 + desc_len
);
4143 add_len
+= add_desc_len
;
4145 spin_unlock(&pr_tmpl
->registration_lock
);
4147 * Set ADDITIONAL_LENGTH
4149 buf
[4] = ((add_len
>> 24) & 0xff);
4150 buf
[5] = ((add_len
>> 16) & 0xff);
4151 buf
[6] = ((add_len
>> 8) & 0xff);
4152 buf
[7] = (add_len
& 0xff);
4157 static int core_scsi3_emulate_pr_in(struct se_cmd
*cmd
, unsigned char *cdb
)
4159 switch (cdb
[1] & 0x1f) {
4161 return core_scsi3_pri_read_keys(cmd
);
4162 case PRI_READ_RESERVATION
:
4163 return core_scsi3_pri_read_reservation(cmd
);
4164 case PRI_REPORT_CAPABILITIES
:
4165 return core_scsi3_pri_report_capabilities(cmd
);
4166 case PRI_READ_FULL_STATUS
:
4167 return core_scsi3_pri_read_full_status(cmd
);
4169 printk(KERN_ERR
"Unknown PERSISTENT_RESERVE_IN service"
4170 " action: 0x%02x\n", cdb
[1] & 0x1f);
4171 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
4176 int core_scsi3_emulate_pr(struct se_cmd
*cmd
)
4178 unsigned char *cdb
= &T_TASK(cmd
)->t_task_cdb
[0];
4179 struct se_device
*dev
= cmd
->se_dev
;
4181 * Following spc2r20 5.5.1 Reservations overview:
4183 * If a logical unit has been reserved by any RESERVE command and is
4184 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4185 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4186 * initiator or service action and shall terminate with a RESERVATION
4189 if (dev
->dev_flags
& DF_SPC2_RESERVATIONS
) {
4190 printk(KERN_ERR
"Received PERSISTENT_RESERVE CDB while legacy"
4191 " SPC-2 reservation is held, returning"
4192 " RESERVATION_CONFLICT\n");
4193 return PYX_TRANSPORT_RESERVATION_CONFLICT
;
4196 return (cdb
[0] == PERSISTENT_RESERVE_OUT
) ?
4197 core_scsi3_emulate_pr_out(cmd
, cdb
) :
4198 core_scsi3_emulate_pr_in(cmd
, cdb
);
4201 static int core_pt_reservation_check(struct se_cmd
*cmd
, u32
*pr_res_type
)
4206 static int core_pt_seq_non_holder(
4214 int core_setup_reservations(struct se_device
*dev
, int force_pt
)
4216 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
4217 struct t10_reservation_template
*rest
= &su_dev
->t10_reservation
;
4219 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4220 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4221 * cause a problem because libata and some SATA RAID HBAs appear
4222 * under Linux/SCSI, but to emulate reservations themselves.
4224 if (((TRANSPORT(dev
)->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
4225 !(DEV_ATTRIB(dev
)->emulate_reservations
)) || force_pt
) {
4226 rest
->res_type
= SPC_PASSTHROUGH
;
4227 rest
->pr_ops
.t10_reservation_check
= &core_pt_reservation_check
;
4228 rest
->pr_ops
.t10_seq_non_holder
= &core_pt_seq_non_holder
;
4229 printk(KERN_INFO
"%s: Using SPC_PASSTHROUGH, no reservation"
4230 " emulation\n", TRANSPORT(dev
)->name
);
4234 * If SPC-3 or above is reported by real or emulated struct se_device,
4235 * use emulated Persistent Reservations.
4237 if (TRANSPORT(dev
)->get_device_rev(dev
) >= SCSI_3
) {
4238 rest
->res_type
= SPC3_PERSISTENT_RESERVATIONS
;
4239 rest
->pr_ops
.t10_reservation_check
= &core_scsi3_pr_reservation_check
;
4240 rest
->pr_ops
.t10_seq_non_holder
= &core_scsi3_pr_seq_non_holder
;
4241 printk(KERN_INFO
"%s: Using SPC3_PERSISTENT_RESERVATIONS"
4242 " emulation\n", TRANSPORT(dev
)->name
);
4244 rest
->res_type
= SPC2_RESERVATIONS
;
4245 rest
->pr_ops
.t10_reservation_check
= &core_scsi2_reservation_check
;
4246 rest
->pr_ops
.t10_seq_non_holder
=
4247 &core_scsi2_reservation_seq_non_holder
;
4248 printk(KERN_INFO
"%s: Using SPC2_RESERVATIONS emulation\n",
4249 TRANSPORT(dev
)->name
);