1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@kernel.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ******************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_pr.h"
42 #include "target_core_ua.h"
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 struct pr_transport_id_holder {
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
56 int core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
61 if (!pr_reg->isid_present_at_reg)
64 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
68 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69 struct t10_pr_registration *, int);
71 static int core_scsi2_reservation_seq_non_holder(
88 static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
90 struct se_device *dev = cmd->se_dev;
91 struct se_session *sess = cmd->se_sess;
97 spin_lock(&dev->dev_reservation_lock);
98 if (!dev->dev_reserved_node_acl || !sess) {
99 spin_unlock(&dev->dev_reservation_lock);
102 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103 spin_unlock(&dev->dev_reservation_lock);
106 if (!(dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID)) {
107 spin_unlock(&dev->dev_reservation_lock);
110 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
111 spin_unlock(&dev->dev_reservation_lock);
116 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117 struct se_node_acl *, struct se_session *);
118 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
120 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
122 struct se_session *se_sess = cmd->se_sess;
123 struct se_device *dev = cmd->se_dev;
124 struct t10_pr_registration *pr_reg;
125 struct t10_reservation *pr_tmpl = &dev->t10_pr;
126 int crh = (dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
132 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
156 if (pr_reg->pr_res_holder) {
157 core_scsi3_put_pr_reg(pr_reg);
160 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
161 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
162 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
163 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
164 core_scsi3_put_pr_reg(pr_reg);
167 core_scsi3_put_pr_reg(pr_reg);
171 * Following spc2r20 5.5.1 Reservations overview:
173 * If a logical unit has executed a PERSISTENT RESERVE OUT
174 * command with the REGISTER or the REGISTER AND IGNORE
175 * EXISTING KEY service action and is still registered by any
176 * initiator, all RESERVE commands and all RELEASE commands
177 * regardless of initiator shall conflict and shall terminate
178 * with a RESERVATION CONFLICT status.
180 spin_lock(&pr_tmpl->registration_lock);
181 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
182 spin_unlock(&pr_tmpl->registration_lock);
186 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
187 " while active SPC-3 registrations exist,"
188 " returning RESERVATION_CONFLICT\n");
189 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
196 int target_scsi2_reservation_release(struct se_cmd *cmd)
198 struct se_device *dev = cmd->se_dev;
199 struct se_session *sess = cmd->se_sess;
200 struct se_portal_group *tpg;
203 if (!sess || !sess->se_tpg)
205 rc = target_check_scsi2_reservation_conflict(cmd);
209 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
215 spin_lock(&dev->dev_reservation_lock);
216 if (!dev->dev_reserved_node_acl || !sess)
219 if (dev->dev_reserved_node_acl != sess->se_node_acl)
222 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
225 dev->dev_reserved_node_acl = NULL;
226 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
227 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
228 dev->dev_res_bin_isid = 0;
229 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
232 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
233 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
234 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
235 sess->se_node_acl->initiatorname);
238 spin_unlock(&dev->dev_reservation_lock);
241 target_complete_cmd(cmd, GOOD);
245 int target_scsi2_reservation_reserve(struct se_cmd *cmd)
247 struct se_device *dev = cmd->se_dev;
248 struct se_session *sess = cmd->se_sess;
249 struct se_portal_group *tpg;
252 if ((cmd->t_task_cdb[1] & 0x01) &&
253 (cmd->t_task_cdb[1] & 0x02)) {
254 pr_err("LongIO and Obselete Bits set, returning"
255 " ILLEGAL_REQUEST\n");
256 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
261 * This is currently the case for target_core_mod passthrough struct se_cmd
264 if (!sess || !sess->se_tpg)
266 rc = target_check_scsi2_reservation_conflict(cmd);
270 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
277 spin_lock(&dev->dev_reservation_lock);
278 if (dev->dev_reserved_node_acl &&
279 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
280 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
281 tpg->se_tpg_tfo->get_fabric_name());
282 pr_err("Original reserver LUN: %u %s\n",
283 cmd->se_lun->unpacked_lun,
284 dev->dev_reserved_node_acl->initiatorname);
285 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
286 " from %s \n", cmd->se_lun->unpacked_lun,
287 cmd->se_deve->mapped_lun,
288 sess->se_node_acl->initiatorname);
289 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
294 dev->dev_reserved_node_acl = sess->se_node_acl;
295 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
296 if (sess->sess_bin_isid != 0) {
297 dev->dev_res_bin_isid = sess->sess_bin_isid;
298 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
300 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
301 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
302 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
303 sess->se_node_acl->initiatorname);
306 spin_unlock(&dev->dev_reservation_lock);
309 target_complete_cmd(cmd, GOOD);
315 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
317 * This function is called by those initiator ports who are *NOT*
318 * the active PR reservation holder when a reservation is present.
320 static int core_scsi3_pr_seq_non_holder(
325 struct se_dev_entry *se_deve;
326 struct se_session *se_sess = cmd->se_sess;
327 int other_cdb = 0, ignore_reg;
328 int registered_nexus = 0, ret = 1; /* Conflict by default */
329 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
330 int we = 0; /* Write Exclusive */
331 int legacy = 0; /* Act like a legacy device and return
332 * RESERVATION CONFLICT on some CDBs */
334 * A legacy SPC-2 reservation is being held.
336 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
337 return core_scsi2_reservation_seq_non_holder(cmd,
340 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
342 * Determine if the registration should be ignored due to
343 * non-matching ISIDs in core_scsi3_pr_reservation_check().
345 ignore_reg = (pr_reg_type & 0x80000000);
347 pr_reg_type &= ~0x80000000;
349 switch (pr_reg_type) {
350 case PR_TYPE_WRITE_EXCLUSIVE:
352 case PR_TYPE_EXCLUSIVE_ACCESS:
354 * Some commands are only allowed for the persistent reservation
357 if ((se_deve->def_pr_registered) && !(ignore_reg))
358 registered_nexus = 1;
360 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
362 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
364 * Some commands are only allowed for registered I_T Nexuses.
367 if ((se_deve->def_pr_registered) && !(ignore_reg))
368 registered_nexus = 1;
370 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
372 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
374 * Each registered I_T Nexus is a reservation holder.
377 if ((se_deve->def_pr_registered) && !(ignore_reg))
378 registered_nexus = 1;
384 * Referenced from spc4r17 table 45 for *NON* PR holder access
387 case SECURITY_PROTOCOL_IN:
388 if (registered_nexus)
396 case RECEIVE_DIAGNOSTIC:
401 if (registered_nexus) {
405 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
407 case PERSISTENT_RESERVE_OUT:
409 * This follows PERSISTENT_RESERVE_OUT service actions that
410 * are allowed in the presence of various reservations.
411 * See spc4r17, table 46
413 switch (cdb[1] & 0x1f) {
416 case PRO_PREEMPT_AND_ABORT:
417 ret = (registered_nexus) ? 0 : 1;
420 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
423 case PRO_REGISTER_AND_MOVE:
428 ret = (registered_nexus) ? 0 : 1;
431 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
432 " action: 0x%02x\n", cdb[1] & 0x1f);
438 /* Handled by CRH=1 in target_scsi2_reservation_release() */
443 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
446 case TEST_UNIT_READY:
447 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
450 switch (cdb[1] & 0x1f) {
451 case MI_MANAGEMENT_PROTOCOL_IN:
452 if (registered_nexus) {
456 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
458 case MI_REPORT_SUPPORTED_OPERATION_CODES:
459 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
464 if (registered_nexus) {
468 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
470 case MI_REPORT_ALIASES:
471 case MI_REPORT_IDENTIFYING_INFORMATION:
472 case MI_REPORT_PRIORITY:
473 case MI_REPORT_TARGET_PGS:
474 case MI_REPORT_TIMESTAMP:
475 ret = 0; /* Allowed */
478 pr_err("Unknown MI Service Action: 0x%02x\n",
483 case ACCESS_CONTROL_IN:
484 case ACCESS_CONTROL_OUT:
487 case READ_MEDIA_SERIAL_NUMBER:
490 case PERSISTENT_RESERVE_IN:
491 ret = 0; /*/ Allowed CDBs */
498 * Case where the CDB is explicitly allowed in the above switch
501 if (!ret && !other_cdb) {
502 pr_debug("Allowing explict CDB: 0x%02x for %s"
503 " reservation holder\n", cdb[0],
504 core_scsi3_pr_dump_type(pr_reg_type));
509 * Check if write exclusive initiator ports *NOT* holding the
510 * WRITE_EXCLUSIVE_* reservation.
512 if (we && !registered_nexus) {
513 if (cmd->data_direction == DMA_TO_DEVICE) {
515 * Conflict for write exclusive
517 pr_debug("%s Conflict for unregistered nexus"
518 " %s CDB: 0x%02x to %s reservation\n",
519 transport_dump_cmd_direction(cmd),
520 se_sess->se_node_acl->initiatorname, cdb[0],
521 core_scsi3_pr_dump_type(pr_reg_type));
525 * Allow non WRITE CDBs for all Write Exclusive
526 * PR TYPEs to pass for registered and
527 * non-registered_nexuxes NOT holding the reservation.
529 * We only make noise for the unregisterd nexuses,
530 * as we expect registered non-reservation holding
531 * nexuses to issue CDBs.
534 if (!registered_nexus) {
535 pr_debug("Allowing implict CDB: 0x%02x"
536 " for %s reservation on unregistered"
538 core_scsi3_pr_dump_type(pr_reg_type));
543 } else if ((reg_only) || (all_reg)) {
544 if (registered_nexus) {
546 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
547 * allow commands from registered nexuses.
550 pr_debug("Allowing implict CDB: 0x%02x for %s"
551 " reservation\n", cdb[0],
552 core_scsi3_pr_dump_type(pr_reg_type));
557 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
558 " for %s reservation\n", transport_dump_cmd_direction(cmd),
559 (registered_nexus) ? "" : "un",
560 se_sess->se_node_acl->initiatorname, cdb[0],
561 core_scsi3_pr_dump_type(pr_reg_type));
563 return 1; /* Conflict by default */
566 static u32 core_scsi3_pr_generation(struct se_device *dev)
571 * PRGeneration field shall contain the value of a 32-bit wrapping
572 * counter mainted by the device server.
574 * Note that this is done regardless of Active Persist across
575 * Target PowerLoss (APTPL)
577 * See spc4r17 section 6.3.12 READ_KEYS service action
579 spin_lock(&dev->dev_reservation_lock);
580 prg = dev->t10_pr.pr_generation++;
581 spin_unlock(&dev->dev_reservation_lock);
586 static int core_scsi3_pr_reservation_check(
590 struct se_device *dev = cmd->se_dev;
591 struct se_session *sess = cmd->se_sess;
597 * A legacy SPC-2 reservation is being held.
599 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
600 return core_scsi2_reservation_check(cmd, pr_reg_type);
602 spin_lock(&dev->dev_reservation_lock);
603 if (!dev->dev_pr_res_holder) {
604 spin_unlock(&dev->dev_reservation_lock);
607 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
608 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
609 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
610 spin_unlock(&dev->dev_reservation_lock);
613 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
614 spin_unlock(&dev->dev_reservation_lock);
617 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
618 sess->sess_bin_isid) ? 0 : -EINVAL;
620 * Use bit in *pr_reg_type to notify ISID mismatch in
621 * core_scsi3_pr_seq_non_holder().
624 *pr_reg_type |= 0x80000000;
625 spin_unlock(&dev->dev_reservation_lock);
630 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
631 struct se_device *dev,
632 struct se_node_acl *nacl,
633 struct se_dev_entry *deve,
639 struct t10_pr_registration *pr_reg;
641 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
643 pr_err("Unable to allocate struct t10_pr_registration\n");
647 pr_reg->pr_aptpl_buf = kzalloc(dev->t10_pr.pr_aptpl_buf_len,
649 if (!pr_reg->pr_aptpl_buf) {
650 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
651 kmem_cache_free(t10_pr_reg_cache, pr_reg);
655 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
656 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
657 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
658 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
659 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
660 atomic_set(&pr_reg->pr_res_holders, 0);
661 pr_reg->pr_reg_nacl = nacl;
662 pr_reg->pr_reg_deve = deve;
663 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
664 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
665 pr_reg->pr_res_key = sa_res_key;
666 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
667 pr_reg->pr_reg_aptpl = aptpl;
668 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
670 * If an ISID value for this SCSI Initiator Port exists,
671 * save it to the registration now.
674 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
675 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
676 pr_reg->isid_present_at_reg = 1;
682 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
683 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
686 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
689 static struct t10_pr_registration *__core_scsi3_alloc_registration(
690 struct se_device *dev,
691 struct se_node_acl *nacl,
692 struct se_dev_entry *deve,
698 struct se_dev_entry *deve_tmp;
699 struct se_node_acl *nacl_tmp;
700 struct se_port *port, *port_tmp;
701 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
702 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
705 * Create a registration for the I_T Nexus upon which the
706 * PROUT REGISTER was received.
708 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
709 sa_res_key, all_tg_pt, aptpl);
713 * Return pointer to pr_reg for ALL_TG_PT=0
718 * Create list of matching SCSI Initiator Port registrations
721 spin_lock(&dev->se_port_lock);
722 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
723 atomic_inc(&port->sep_tg_pt_ref_cnt);
724 smp_mb__after_atomic_inc();
725 spin_unlock(&dev->se_port_lock);
727 spin_lock_bh(&port->sep_alua_lock);
728 list_for_each_entry(deve_tmp, &port->sep_alua_list,
731 * This pointer will be NULL for demo mode MappedLUNs
732 * that have not been make explict via a ConfigFS
733 * MappedLUN group for the SCSI Initiator Node ACL.
735 if (!deve_tmp->se_lun_acl)
738 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
740 * Skip the matching struct se_node_acl that is allocated
743 if (nacl == nacl_tmp)
746 * Only perform PR registrations for target ports on
747 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
750 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
753 * Look for a matching Initiator Node ACL in ASCII format
755 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
758 atomic_inc(&deve_tmp->pr_ref_count);
759 smp_mb__after_atomic_inc();
760 spin_unlock_bh(&port->sep_alua_lock);
762 * Grab a configfs group dependency that is released
763 * for the exception path at label out: below, or upon
764 * completion of adding ALL_TG_PT=1 registrations in
765 * __core_scsi3_add_registration()
767 ret = core_scsi3_lunacl_depend_item(deve_tmp);
769 pr_err("core_scsi3_lunacl_depend"
771 atomic_dec(&port->sep_tg_pt_ref_cnt);
772 smp_mb__after_atomic_dec();
773 atomic_dec(&deve_tmp->pr_ref_count);
774 smp_mb__after_atomic_dec();
778 * Located a matching SCSI Initiator Port on a different
779 * port, allocate the pr_reg_atp and attach it to the
780 * pr_reg->pr_reg_atp_list that will be processed once
781 * the original *pr_reg is processed in
782 * __core_scsi3_add_registration()
784 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
785 nacl_tmp, deve_tmp, NULL,
786 sa_res_key, all_tg_pt, aptpl);
788 atomic_dec(&port->sep_tg_pt_ref_cnt);
789 smp_mb__after_atomic_dec();
790 atomic_dec(&deve_tmp->pr_ref_count);
791 smp_mb__after_atomic_dec();
792 core_scsi3_lunacl_undepend_item(deve_tmp);
796 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
797 &pr_reg->pr_reg_atp_list);
798 spin_lock_bh(&port->sep_alua_lock);
800 spin_unlock_bh(&port->sep_alua_lock);
802 spin_lock(&dev->se_port_lock);
803 atomic_dec(&port->sep_tg_pt_ref_cnt);
804 smp_mb__after_atomic_dec();
806 spin_unlock(&dev->se_port_lock);
810 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
811 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
812 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
813 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
814 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
816 kmem_cache_free(t10_pr_reg_cache, pr_reg);
820 int core_scsi3_alloc_aptpl_registration(
821 struct t10_reservation *pr_tmpl,
823 unsigned char *i_port,
826 unsigned char *t_port,
833 struct t10_pr_registration *pr_reg;
835 if (!i_port || !t_port || !sa_res_key) {
836 pr_err("Illegal parameters for APTPL registration\n");
840 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
842 pr_err("Unable to allocate struct t10_pr_registration\n");
845 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
847 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
848 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
849 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
850 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
851 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
852 atomic_set(&pr_reg->pr_res_holders, 0);
853 pr_reg->pr_reg_nacl = NULL;
854 pr_reg->pr_reg_deve = NULL;
855 pr_reg->pr_res_mapped_lun = mapped_lun;
856 pr_reg->pr_aptpl_target_lun = target_lun;
857 pr_reg->pr_res_key = sa_res_key;
858 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
859 pr_reg->pr_reg_aptpl = 1;
860 pr_reg->pr_reg_tg_pt_lun = NULL;
861 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
862 pr_reg->pr_res_type = type;
864 * If an ISID value had been saved in APTPL metadata for this
865 * SCSI Initiator Port, restore it now.
868 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
869 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
870 pr_reg->isid_present_at_reg = 1;
873 * Copy the i_port and t_port information from caller.
875 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
876 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
877 pr_reg->pr_reg_tpgt = tpgt;
879 * Set pr_res_holder from caller, the pr_reg who is the reservation
880 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
881 * the Initiator Node LUN ACL from the fabric module is created for
884 pr_reg->pr_res_holder = res_holder;
886 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
887 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
888 " metadata\n", (res_holder) ? "+reservation" : "");
892 static void core_scsi3_aptpl_reserve(
893 struct se_device *dev,
894 struct se_portal_group *tpg,
895 struct se_node_acl *node_acl,
896 struct t10_pr_registration *pr_reg)
898 char i_buf[PR_REG_ISID_ID_LEN];
901 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
902 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
905 spin_lock(&dev->dev_reservation_lock);
906 dev->dev_pr_res_holder = pr_reg;
907 spin_unlock(&dev->dev_reservation_lock);
909 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
910 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
911 tpg->se_tpg_tfo->get_fabric_name(),
912 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
913 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
914 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
915 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
916 (prf_isid) ? &i_buf[0] : "");
919 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
920 struct t10_pr_registration *, int, int);
922 static int __core_scsi3_check_aptpl_registration(
923 struct se_device *dev,
924 struct se_portal_group *tpg,
927 struct se_node_acl *nacl,
928 struct se_dev_entry *deve)
930 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
931 struct t10_reservation *pr_tmpl = &dev->t10_pr;
932 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
933 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
936 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
937 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
939 * Copy Initiator Port information from struct se_node_acl
941 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
942 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
943 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
944 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
946 * Look for the matching registrations+reservation from those
947 * created from APTPL metadata. Note that multiple registrations
948 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
951 spin_lock(&pr_tmpl->aptpl_reg_lock);
952 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
954 if (!strcmp(pr_reg->pr_iport, i_port) &&
955 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
956 !(strcmp(pr_reg->pr_tport, t_port)) &&
957 (pr_reg->pr_reg_tpgt == tpgt) &&
958 (pr_reg->pr_aptpl_target_lun == target_lun)) {
960 pr_reg->pr_reg_nacl = nacl;
961 pr_reg->pr_reg_deve = deve;
962 pr_reg->pr_reg_tg_pt_lun = lun;
964 list_del(&pr_reg->pr_reg_aptpl_list);
965 spin_unlock(&pr_tmpl->aptpl_reg_lock);
967 * At this point all of the pointers in *pr_reg will
968 * be setup, so go ahead and add the registration.
971 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
973 * If this registration is the reservation holder,
974 * make that happen now..
976 if (pr_reg->pr_res_holder)
977 core_scsi3_aptpl_reserve(dev, tpg,
980 * Reenable pr_aptpl_active to accept new metadata
981 * updates once the SCSI device is active again..
983 spin_lock(&pr_tmpl->aptpl_reg_lock);
984 pr_tmpl->pr_aptpl_active = 1;
987 spin_unlock(&pr_tmpl->aptpl_reg_lock);
992 int core_scsi3_check_aptpl_registration(
993 struct se_device *dev,
994 struct se_portal_group *tpg,
996 struct se_lun_acl *lun_acl)
998 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
999 struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
1001 if (dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1004 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1005 lun->unpacked_lun, nacl, deve);
1008 static void __core_scsi3_dump_registration(
1009 struct target_core_fabric_ops *tfo,
1010 struct se_device *dev,
1011 struct se_node_acl *nacl,
1012 struct t10_pr_registration *pr_reg,
1015 struct se_portal_group *se_tpg = nacl->se_tpg;
1016 char i_buf[PR_REG_ISID_ID_LEN];
1019 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1020 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1021 PR_REG_ISID_ID_LEN);
1023 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1024 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1025 "_AND_MOVE" : (register_type == 1) ?
1026 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1027 (prf_isid) ? i_buf : "");
1028 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1029 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1030 tfo->tpg_get_tag(se_tpg));
1031 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1032 " Port(s)\n", tfo->get_fabric_name(),
1033 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1034 dev->transport->name);
1035 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1036 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1037 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1038 pr_reg->pr_reg_aptpl);
1042 * this function can be called with struct se_device->dev_reservation_lock
1043 * when register_move = 1
1045 static void __core_scsi3_add_registration(
1046 struct se_device *dev,
1047 struct se_node_acl *nacl,
1048 struct t10_pr_registration *pr_reg,
1052 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1053 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1054 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1057 * Increment PRgeneration counter for struct se_device upon a successful
1058 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1060 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1061 * action, the struct se_device->dev_reservation_lock will already be held,
1062 * so we do not call core_scsi3_pr_generation() which grabs the lock
1065 pr_reg->pr_res_generation = (register_move) ?
1066 dev->t10_pr.pr_generation++ :
1067 core_scsi3_pr_generation(dev);
1069 spin_lock(&pr_tmpl->registration_lock);
1070 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1071 pr_reg->pr_reg_deve->def_pr_registered = 1;
1073 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1074 spin_unlock(&pr_tmpl->registration_lock);
1076 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1078 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1081 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1082 * allocated in __core_scsi3_alloc_registration()
1084 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1085 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1086 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1088 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1090 spin_lock(&pr_tmpl->registration_lock);
1091 list_add_tail(&pr_reg_tmp->pr_reg_list,
1092 &pr_tmpl->registration_list);
1093 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1095 __core_scsi3_dump_registration(tfo, dev,
1096 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1098 spin_unlock(&pr_tmpl->registration_lock);
1100 * Drop configfs group dependency reference from
1101 * __core_scsi3_alloc_registration()
1103 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1107 static int core_scsi3_alloc_registration(
1108 struct se_device *dev,
1109 struct se_node_acl *nacl,
1110 struct se_dev_entry *deve,
1111 unsigned char *isid,
1118 struct t10_pr_registration *pr_reg;
1120 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1121 sa_res_key, all_tg_pt, aptpl);
1125 __core_scsi3_add_registration(dev, nacl, pr_reg,
1126 register_type, register_move);
1130 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1131 struct se_device *dev,
1132 struct se_node_acl *nacl,
1133 unsigned char *isid)
1135 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1136 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1137 struct se_portal_group *tpg;
1139 spin_lock(&pr_tmpl->registration_lock);
1140 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1141 &pr_tmpl->registration_list, pr_reg_list) {
1143 * First look for a matching struct se_node_acl
1145 if (pr_reg->pr_reg_nacl != nacl)
1148 tpg = pr_reg->pr_reg_nacl->se_tpg;
1150 * If this registration does NOT contain a fabric provided
1151 * ISID, then we have found a match.
1153 if (!pr_reg->isid_present_at_reg) {
1155 * Determine if this SCSI device server requires that
1156 * SCSI Intiatior TransportID w/ ISIDs is enforced
1157 * for fabric modules (iSCSI) requiring them.
1159 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1160 if (dev->dev_attrib.enforce_pr_isids)
1163 atomic_inc(&pr_reg->pr_res_holders);
1164 smp_mb__after_atomic_inc();
1165 spin_unlock(&pr_tmpl->registration_lock);
1169 * If the *pr_reg contains a fabric defined ISID for multi-value
1170 * SCSI Initiator Port TransportIDs, then we expect a valid
1171 * matching ISID to be provided by the local SCSI Initiator Port.
1175 if (strcmp(isid, pr_reg->pr_reg_isid))
1178 atomic_inc(&pr_reg->pr_res_holders);
1179 smp_mb__after_atomic_inc();
1180 spin_unlock(&pr_tmpl->registration_lock);
1183 spin_unlock(&pr_tmpl->registration_lock);
1188 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1189 struct se_device *dev,
1190 struct se_node_acl *nacl,
1191 struct se_session *sess)
1193 struct se_portal_group *tpg = nacl->se_tpg;
1194 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1196 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1197 memset(&buf[0], 0, PR_REG_ISID_LEN);
1198 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1203 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1206 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1208 atomic_dec(&pr_reg->pr_res_holders);
1209 smp_mb__after_atomic_dec();
1212 static int core_scsi3_check_implict_release(
1213 struct se_device *dev,
1214 struct t10_pr_registration *pr_reg)
1216 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1217 struct t10_pr_registration *pr_res_holder;
1220 spin_lock(&dev->dev_reservation_lock);
1221 pr_res_holder = dev->dev_pr_res_holder;
1222 if (!pr_res_holder) {
1223 spin_unlock(&dev->dev_reservation_lock);
1226 if (pr_res_holder == pr_reg) {
1228 * Perform an implict RELEASE if the registration that
1229 * is being released is holding the reservation.
1231 * From spc4r17, section 5.7.11.1:
1233 * e) If the I_T nexus is the persistent reservation holder
1234 * and the persistent reservation is not an all registrants
1235 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1236 * service action or REGISTER AND IGNORE EXISTING KEY
1237 * service action with the SERVICE ACTION RESERVATION KEY
1238 * field set to zero (see 5.7.11.3).
1240 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1243 * For 'All Registrants' reservation types, all existing
1244 * registrations are still processed as reservation holders
1245 * in core_scsi3_pr_seq_non_holder() after the initial
1246 * reservation holder is implictly released here.
1248 } else if (pr_reg->pr_reg_all_tg_pt &&
1249 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1250 pr_reg->pr_reg_nacl->initiatorname)) &&
1251 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1252 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1253 " UNREGISTER while existing reservation with matching"
1254 " key 0x%016Lx is present from another SCSI Initiator"
1255 " Port\n", pr_reg->pr_res_key);
1258 spin_unlock(&dev->dev_reservation_lock);
1264 * Called with struct t10_reservation->registration_lock held.
1266 static void __core_scsi3_free_registration(
1267 struct se_device *dev,
1268 struct t10_pr_registration *pr_reg,
1269 struct list_head *preempt_and_abort_list,
1272 struct target_core_fabric_ops *tfo =
1273 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1274 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1275 char i_buf[PR_REG_ISID_ID_LEN];
1278 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1279 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1280 PR_REG_ISID_ID_LEN);
1282 pr_reg->pr_reg_deve->def_pr_registered = 0;
1283 pr_reg->pr_reg_deve->pr_res_key = 0;
1284 list_del(&pr_reg->pr_reg_list);
1286 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1287 * so call core_scsi3_put_pr_reg() to decrement our reference.
1290 core_scsi3_put_pr_reg(pr_reg);
1292 * Wait until all reference from any other I_T nexuses for this
1293 * *pr_reg have been released. Because list_del() is called above,
1294 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1295 * count back to zero, and we release *pr_reg.
1297 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1298 spin_unlock(&pr_tmpl->registration_lock);
1299 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1300 tfo->get_fabric_name());
1302 spin_lock(&pr_tmpl->registration_lock);
1305 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1306 " Node: %s%s\n", tfo->get_fabric_name(),
1307 pr_reg->pr_reg_nacl->initiatorname,
1308 (prf_isid) ? &i_buf[0] : "");
1309 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1310 " Port(s)\n", tfo->get_fabric_name(),
1311 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1312 dev->transport->name);
1313 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1314 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1315 pr_reg->pr_res_generation);
1317 if (!preempt_and_abort_list) {
1318 pr_reg->pr_reg_deve = NULL;
1319 pr_reg->pr_reg_nacl = NULL;
1320 kfree(pr_reg->pr_aptpl_buf);
1321 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1325 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1326 * are released once the ABORT_TASK_SET has completed..
1328 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1331 void core_scsi3_free_pr_reg_from_nacl(
1332 struct se_device *dev,
1333 struct se_node_acl *nacl)
1335 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1336 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1338 * If the passed se_node_acl matches the reservation holder,
1339 * release the reservation.
1341 spin_lock(&dev->dev_reservation_lock);
1342 pr_res_holder = dev->dev_pr_res_holder;
1343 if ((pr_res_holder != NULL) &&
1344 (pr_res_holder->pr_reg_nacl == nacl))
1345 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1346 spin_unlock(&dev->dev_reservation_lock);
1348 * Release any registration associated with the struct se_node_acl.
1350 spin_lock(&pr_tmpl->registration_lock);
1351 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1352 &pr_tmpl->registration_list, pr_reg_list) {
1354 if (pr_reg->pr_reg_nacl != nacl)
1357 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1359 spin_unlock(&pr_tmpl->registration_lock);
1362 void core_scsi3_free_all_registrations(
1363 struct se_device *dev)
1365 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1366 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1368 spin_lock(&dev->dev_reservation_lock);
1369 pr_res_holder = dev->dev_pr_res_holder;
1370 if (pr_res_holder != NULL) {
1371 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1372 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1375 spin_unlock(&dev->dev_reservation_lock);
1377 spin_lock(&pr_tmpl->registration_lock);
1378 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1379 &pr_tmpl->registration_list, pr_reg_list) {
1381 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1383 spin_unlock(&pr_tmpl->registration_lock);
1385 spin_lock(&pr_tmpl->aptpl_reg_lock);
1386 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1387 pr_reg_aptpl_list) {
1388 list_del(&pr_reg->pr_reg_aptpl_list);
1389 kfree(pr_reg->pr_aptpl_buf);
1390 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1392 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1395 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1397 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1398 &tpg->tpg_group.cg_item);
1401 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1403 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1404 &tpg->tpg_group.cg_item);
1406 atomic_dec(&tpg->tpg_pr_ref_count);
1407 smp_mb__after_atomic_dec();
1410 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1412 struct se_portal_group *tpg = nacl->se_tpg;
1414 if (nacl->dynamic_node_acl)
1417 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1418 &nacl->acl_group.cg_item);
1421 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1423 struct se_portal_group *tpg = nacl->se_tpg;
1425 if (nacl->dynamic_node_acl) {
1426 atomic_dec(&nacl->acl_pr_ref_count);
1427 smp_mb__after_atomic_dec();
1431 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1432 &nacl->acl_group.cg_item);
1434 atomic_dec(&nacl->acl_pr_ref_count);
1435 smp_mb__after_atomic_dec();
1438 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1440 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1441 struct se_node_acl *nacl;
1442 struct se_portal_group *tpg;
1444 * For nacl->dynamic_node_acl=1
1449 nacl = lun_acl->se_lun_nacl;
1452 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1453 &lun_acl->se_lun_group.cg_item);
1456 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1458 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1459 struct se_node_acl *nacl;
1460 struct se_portal_group *tpg;
1462 * For nacl->dynamic_node_acl=1
1465 atomic_dec(&se_deve->pr_ref_count);
1466 smp_mb__after_atomic_dec();
1469 nacl = lun_acl->se_lun_nacl;
1472 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1473 &lun_acl->se_lun_group.cg_item);
1475 atomic_dec(&se_deve->pr_ref_count);
1476 smp_mb__after_atomic_dec();
1479 static int core_scsi3_decode_spec_i_port(
1481 struct se_portal_group *tpg,
1482 unsigned char *l_isid,
1487 struct se_device *dev = cmd->se_dev;
1488 struct se_port *tmp_port;
1489 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1490 struct se_session *se_sess = cmd->se_sess;
1491 struct se_node_acl *dest_node_acl = NULL;
1492 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1493 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1494 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1495 LIST_HEAD(tid_dest_list);
1496 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1497 struct target_core_fabric_ops *tmp_tf_ops;
1499 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1500 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1501 u32 tpdl, tid_len = 0;
1502 int ret, dest_local_nexus, prf_isid;
1505 memset(dest_iport, 0, 64);
1507 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1509 * Allocate a struct pr_transport_id_holder and setup the
1510 * local_node_acl and local_se_deve pointers and add to
1511 * struct list_head tid_dest_list for add registration
1512 * processing in the loop of tid_dest_list below.
1514 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1516 pr_err("Unable to allocate tidh_new\n");
1517 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1520 INIT_LIST_HEAD(&tidh_new->dest_list);
1521 tidh_new->dest_tpg = tpg;
1522 tidh_new->dest_node_acl = se_sess->se_node_acl;
1523 tidh_new->dest_se_deve = local_se_deve;
1525 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1526 se_sess->se_node_acl, local_se_deve, l_isid,
1527 sa_res_key, all_tg_pt, aptpl);
1528 if (!local_pr_reg) {
1530 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1533 tidh_new->dest_pr_reg = local_pr_reg;
1535 * The local I_T nexus does not hold any configfs dependances,
1536 * so we set tid_h->dest_local_nexus=1 to prevent the
1537 * configfs_undepend_item() calls in the tid_dest_list loops below.
1539 tidh_new->dest_local_nexus = 1;
1540 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1542 if (cmd->data_length < 28) {
1543 pr_warn("SPC-PR: Received PR OUT parameter list"
1544 " length too small: %u\n", cmd->data_length);
1545 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1550 buf = transport_kmap_data_sg(cmd);
1552 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1553 * first extract TransportID Parameter Data Length, and make sure
1554 * the value matches up to the SCSI expected data transfer length.
1556 tpdl = (buf[24] & 0xff) << 24;
1557 tpdl |= (buf[25] & 0xff) << 16;
1558 tpdl |= (buf[26] & 0xff) << 8;
1559 tpdl |= buf[27] & 0xff;
1561 if ((tpdl + 28) != cmd->data_length) {
1562 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1563 " does not equal CDB data_length: %u\n", tpdl,
1565 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1570 * Start processing the received transport IDs using the
1571 * receiving I_T Nexus portal's fabric dependent methods to
1572 * obtain the SCSI Initiator Port/Device Identifiers.
1577 proto_ident = (ptr[0] & 0x0f);
1580 spin_lock(&dev->se_port_lock);
1581 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1582 tmp_tpg = tmp_port->sep_tpg;
1585 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1588 if (!tmp_tf_ops->get_fabric_proto_ident ||
1589 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1592 * Look for the matching proto_ident provided by
1593 * the received TransportID
1595 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1596 if (tmp_proto_ident != proto_ident)
1598 dest_rtpi = tmp_port->sep_rtpi;
1600 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1601 tmp_tpg, (const char *)ptr, &tid_len,
1606 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1607 smp_mb__after_atomic_inc();
1608 spin_unlock(&dev->se_port_lock);
1610 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1612 pr_err(" core_scsi3_tpg_depend_item()"
1614 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1615 smp_mb__after_atomic_dec();
1616 cmd->scsi_sense_reason =
1617 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1622 * Locate the destination initiator ACL to be registered
1623 * from the decoded fabric module specific TransportID
1626 spin_lock_irq(&tmp_tpg->acl_node_lock);
1627 dest_node_acl = __core_tpg_get_initiator_node_acl(
1629 if (dest_node_acl) {
1630 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1631 smp_mb__after_atomic_inc();
1633 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1635 if (!dest_node_acl) {
1636 core_scsi3_tpg_undepend_item(tmp_tpg);
1637 spin_lock(&dev->se_port_lock);
1641 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1643 pr_err("configfs_depend_item() failed"
1644 " for dest_node_acl->acl_group\n");
1645 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1646 smp_mb__after_atomic_dec();
1647 core_scsi3_tpg_undepend_item(tmp_tpg);
1648 cmd->scsi_sense_reason =
1649 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1655 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1656 " %s Port RTPI: %hu\n",
1657 dest_tpg->se_tpg_tfo->get_fabric_name(),
1658 dest_node_acl->initiatorname, dest_rtpi);
1660 spin_lock(&dev->se_port_lock);
1663 spin_unlock(&dev->se_port_lock);
1666 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1668 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1673 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1674 " tid_len: %d for %s + %s\n",
1675 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1676 tpdl, tid_len, i_str, iport_ptr);
1678 if (tid_len > tpdl) {
1679 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1680 " %u for Transport ID: %s\n", tid_len, ptr);
1681 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1682 core_scsi3_tpg_undepend_item(dest_tpg);
1683 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1688 * Locate the desintation struct se_dev_entry pointer for matching
1689 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1692 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1694 if (!dest_se_deve) {
1695 pr_err("Unable to locate %s dest_se_deve"
1696 " from destination RTPI: %hu\n",
1697 dest_tpg->se_tpg_tfo->get_fabric_name(),
1700 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1701 core_scsi3_tpg_undepend_item(dest_tpg);
1702 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1707 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1709 pr_err("core_scsi3_lunacl_depend_item()"
1711 atomic_dec(&dest_se_deve->pr_ref_count);
1712 smp_mb__after_atomic_dec();
1713 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1714 core_scsi3_tpg_undepend_item(dest_tpg);
1715 cmd->scsi_sense_reason =
1716 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1721 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1722 " dest_se_deve mapped_lun: %u\n",
1723 dest_tpg->se_tpg_tfo->get_fabric_name(),
1724 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1727 * Skip any TransportIDs that already have a registration for
1730 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1733 core_scsi3_put_pr_reg(pr_reg_e);
1734 core_scsi3_lunacl_undepend_item(dest_se_deve);
1735 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1736 core_scsi3_tpg_undepend_item(dest_tpg);
1743 * Allocate a struct pr_transport_id_holder and setup
1744 * the dest_node_acl and dest_se_deve pointers for the
1747 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1750 pr_err("Unable to allocate tidh_new\n");
1751 core_scsi3_lunacl_undepend_item(dest_se_deve);
1752 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1753 core_scsi3_tpg_undepend_item(dest_tpg);
1754 cmd->scsi_sense_reason =
1755 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1759 INIT_LIST_HEAD(&tidh_new->dest_list);
1760 tidh_new->dest_tpg = dest_tpg;
1761 tidh_new->dest_node_acl = dest_node_acl;
1762 tidh_new->dest_se_deve = dest_se_deve;
1765 * Allocate, but do NOT add the registration for the
1766 * TransportID referenced SCSI Initiator port. This
1767 * done because of the following from spc4r17 in section
1768 * 6.14.3 wrt SPEC_I_PT:
1770 * "If a registration fails for any initiator port (e.g., if th
1771 * logical unit does not have enough resources available to
1772 * hold the registration information), no registrations shall be
1773 * made, and the command shall be terminated with
1774 * CHECK CONDITION status."
1776 * That means we call __core_scsi3_alloc_registration() here,
1777 * and then call __core_scsi3_add_registration() in the
1778 * 2nd loop which will never fail.
1780 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1781 dest_node_acl, dest_se_deve, iport_ptr,
1782 sa_res_key, all_tg_pt, aptpl);
1784 core_scsi3_lunacl_undepend_item(dest_se_deve);
1785 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1786 core_scsi3_tpg_undepend_item(dest_tpg);
1788 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1792 tidh_new->dest_pr_reg = dest_pr_reg;
1793 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1801 transport_kunmap_data_sg(cmd);
1804 * Go ahead and create a registrations from tid_dest_list for the
1805 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1808 * The SA Reservation Key from the PROUT is set for the
1809 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1810 * means that the TransportID Initiator port will be
1811 * registered on all of the target ports in the SCSI target device
1812 * ALL_TG_PT=0 means the registration will only be for the
1813 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1816 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1817 dest_tpg = tidh->dest_tpg;
1818 dest_node_acl = tidh->dest_node_acl;
1819 dest_se_deve = tidh->dest_se_deve;
1820 dest_pr_reg = tidh->dest_pr_reg;
1821 dest_local_nexus = tidh->dest_local_nexus;
1823 list_del(&tidh->dest_list);
1826 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1827 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1828 PR_REG_ISID_ID_LEN);
1830 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1833 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1834 " registered Transport ID for Node: %s%s Mapped LUN:"
1835 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1836 dest_node_acl->initiatorname, (prf_isid) ?
1837 &i_buf[0] : "", dest_se_deve->mapped_lun);
1839 if (dest_local_nexus)
1842 core_scsi3_lunacl_undepend_item(dest_se_deve);
1843 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1844 core_scsi3_tpg_undepend_item(dest_tpg);
1849 transport_kunmap_data_sg(cmd);
1851 * For the failure case, release everything from tid_dest_list
1852 * including *dest_pr_reg and the configfs dependances..
1854 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1855 dest_tpg = tidh->dest_tpg;
1856 dest_node_acl = tidh->dest_node_acl;
1857 dest_se_deve = tidh->dest_se_deve;
1858 dest_pr_reg = tidh->dest_pr_reg;
1859 dest_local_nexus = tidh->dest_local_nexus;
1861 list_del(&tidh->dest_list);
1864 * Release any extra ALL_TG_PT=1 registrations for
1865 * the SPEC_I_PT=1 case.
1867 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1868 &dest_pr_reg->pr_reg_atp_list,
1869 pr_reg_atp_mem_list) {
1870 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1871 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1872 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1875 kfree(dest_pr_reg->pr_aptpl_buf);
1876 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1878 if (dest_local_nexus)
1881 core_scsi3_lunacl_undepend_item(dest_se_deve);
1882 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1883 core_scsi3_tpg_undepend_item(dest_tpg);
1889 * Called with struct se_device->dev_reservation_lock held
1891 static int __core_scsi3_update_aptpl_buf(
1892 struct se_device *dev,
1894 u32 pr_aptpl_buf_len,
1895 int clear_aptpl_metadata)
1898 struct se_portal_group *tpg;
1899 struct t10_pr_registration *pr_reg;
1900 unsigned char tmp[512], isid_buf[32];
1904 memset(buf, 0, pr_aptpl_buf_len);
1906 * Called to clear metadata once APTPL has been deactivated.
1908 if (clear_aptpl_metadata) {
1909 snprintf(buf, pr_aptpl_buf_len,
1910 "No Registrations or Reservations\n");
1914 * Walk the registration list..
1916 spin_lock(&dev->t10_pr.registration_lock);
1917 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1922 tpg = pr_reg->pr_reg_nacl->se_tpg;
1923 lun = pr_reg->pr_reg_tg_pt_lun;
1925 * Write out any ISID value to APTPL metadata that was included
1926 * in the original registration.
1928 if (pr_reg->isid_present_at_reg)
1929 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1930 pr_reg->pr_reg_isid);
1932 * Include special metadata if the pr_reg matches the
1933 * reservation holder.
1935 if (dev->dev_pr_res_holder == pr_reg) {
1936 snprintf(tmp, 512, "PR_REG_START: %d"
1937 "\ninitiator_fabric=%s\n"
1938 "initiator_node=%s\n%s"
1940 "res_holder=1\nres_type=%02x\n"
1941 "res_scope=%02x\nres_all_tg_pt=%d\n"
1942 "mapped_lun=%u\n", reg_count,
1943 tpg->se_tpg_tfo->get_fabric_name(),
1944 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1945 pr_reg->pr_res_key, pr_reg->pr_res_type,
1946 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1947 pr_reg->pr_res_mapped_lun);
1949 snprintf(tmp, 512, "PR_REG_START: %d\n"
1950 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1951 "sa_res_key=%llu\nres_holder=0\n"
1952 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1953 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1954 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1955 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1956 pr_reg->pr_res_mapped_lun);
1959 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1960 pr_err("Unable to update renaming"
1961 " APTPL metadata\n");
1962 spin_unlock(&dev->t10_pr.registration_lock);
1965 len += sprintf(buf+len, "%s", tmp);
1968 * Include information about the associated SCSI target port.
1970 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1971 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1972 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1973 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1974 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1975 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1977 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1978 pr_err("Unable to update renaming"
1979 " APTPL metadata\n");
1980 spin_unlock(&dev->t10_pr.registration_lock);
1983 len += sprintf(buf+len, "%s", tmp);
1986 spin_unlock(&dev->t10_pr.registration_lock);
1989 len += sprintf(buf+len, "No Registrations or Reservations");
1994 static int core_scsi3_update_aptpl_buf(
1995 struct se_device *dev,
1997 u32 pr_aptpl_buf_len,
1998 int clear_aptpl_metadata)
2002 spin_lock(&dev->dev_reservation_lock);
2003 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2004 clear_aptpl_metadata);
2005 spin_unlock(&dev->dev_reservation_lock);
2011 * Called with struct se_device->aptpl_file_mutex held
2013 static int __core_scsi3_write_aptpl_to_file(
2014 struct se_device *dev,
2016 u32 pr_aptpl_buf_len)
2018 struct t10_wwn *wwn = &dev->t10_wwn;
2020 struct iovec iov[1];
2021 mm_segment_t old_fs;
2022 int flags = O_RDWR | O_CREAT | O_TRUNC;
2026 memset(iov, 0, sizeof(struct iovec));
2027 memset(path, 0, 512);
2029 if (strlen(&wwn->unit_serial[0]) >= 512) {
2030 pr_err("WWN value for struct se_device does not fit"
2031 " into path buffer\n");
2035 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2036 file = filp_open(path, flags, 0600);
2037 if (IS_ERR(file) || !file || !file->f_dentry) {
2038 pr_err("filp_open(%s) for APTPL metadata"
2040 return IS_ERR(file) ? PTR_ERR(file) : -ENOENT;
2043 iov[0].iov_base = &buf[0];
2044 if (!pr_aptpl_buf_len)
2045 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2047 iov[0].iov_len = pr_aptpl_buf_len;
2051 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2055 pr_debug("Error writing APTPL metadata file: %s\n", path);
2056 filp_close(file, NULL);
2059 filp_close(file, NULL);
2064 static int core_scsi3_update_and_write_aptpl(
2065 struct se_device *dev,
2066 unsigned char *in_buf,
2067 u32 in_pr_aptpl_buf_len)
2069 unsigned char null_buf[64], *buf;
2070 u32 pr_aptpl_buf_len;
2071 int ret, clear_aptpl_metadata = 0;
2073 * Can be called with a NULL pointer from PROUT service action CLEAR
2076 memset(null_buf, 0, 64);
2079 * This will clear the APTPL metadata to:
2080 * "No Registrations or Reservations" status
2082 pr_aptpl_buf_len = 64;
2083 clear_aptpl_metadata = 1;
2086 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2089 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2090 clear_aptpl_metadata);
2094 * __core_scsi3_write_aptpl_to_file() will call strlen()
2095 * on the passed buf to determine pr_aptpl_buf_len.
2097 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2104 static int core_scsi3_emulate_pro_register(
2113 struct se_session *se_sess = cmd->se_sess;
2114 struct se_device *dev = cmd->se_dev;
2115 struct se_dev_entry *se_deve;
2116 struct se_lun *se_lun = cmd->se_lun;
2117 struct se_portal_group *se_tpg;
2118 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2119 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2120 /* Used for APTPL metadata w/ UNREGISTER */
2121 unsigned char *pr_aptpl_buf = NULL;
2122 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2123 int pr_holder = 0, ret = 0, type;
2125 if (!se_sess || !se_lun) {
2126 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2127 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2130 se_tpg = se_sess->se_tpg;
2131 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2133 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2134 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2135 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2137 isid_ptr = &isid_buf[0];
2140 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2142 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2145 pr_warn("SPC-3 PR: Reservation Key non-zero"
2146 " for SA REGISTER, returning CONFLICT\n");
2147 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2151 * Do nothing but return GOOD status.
2158 * Perform the Service Action REGISTER on the Initiator
2159 * Port Endpoint that the PRO was received from on the
2160 * Logical Unit of the SCSI device server.
2162 ret = core_scsi3_alloc_registration(cmd->se_dev,
2163 se_sess->se_node_acl, se_deve, isid_ptr,
2164 sa_res_key, all_tg_pt, aptpl,
2167 pr_err("Unable to allocate"
2168 " struct t10_pr_registration\n");
2169 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2174 * Register both the Initiator port that received
2175 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2176 * TransportID from Parameter list and loop through
2177 * fabric dependent parameter list while calling
2178 * logic from of core_scsi3_alloc_registration() for
2179 * each TransportID provided SCSI Initiator Port/Device
2181 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2182 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2187 * Nothing left to do for the APTPL=0 case.
2190 pr_tmpl->pr_aptpl_active = 0;
2191 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2192 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2197 * Locate the newly allocated local I_T Nexus *pr_reg, and
2198 * update the APTPL metadata information using its
2199 * preallocated *pr_reg->pr_aptpl_buf.
2201 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2202 se_sess->se_node_acl, se_sess);
2204 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2205 &pr_reg->pr_aptpl_buf[0],
2206 pr_tmpl->pr_aptpl_buf_len);
2208 pr_tmpl->pr_aptpl_active = 1;
2209 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2212 core_scsi3_put_pr_reg(pr_reg);
2216 * Locate the existing *pr_reg via struct se_node_acl pointers
2219 type = pr_reg->pr_res_type;
2222 if (res_key != pr_reg->pr_res_key) {
2223 pr_err("SPC-3 PR REGISTER: Received"
2224 " res_key: 0x%016Lx does not match"
2225 " existing SA REGISTER res_key:"
2226 " 0x%016Lx\n", res_key,
2227 pr_reg->pr_res_key);
2228 core_scsi3_put_pr_reg(pr_reg);
2229 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2234 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2235 " set while sa_res_key=0\n");
2236 core_scsi3_put_pr_reg(pr_reg);
2237 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2241 * An existing ALL_TG_PT=1 registration being released
2242 * must also set ALL_TG_PT=1 in the incoming PROUT.
2244 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2245 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2246 " registration exists, but ALL_TG_PT=1 bit not"
2247 " present in received PROUT\n");
2248 core_scsi3_put_pr_reg(pr_reg);
2249 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2253 * Allocate APTPL metadata buffer used for UNREGISTER ops
2256 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2258 if (!pr_aptpl_buf) {
2259 pr_err("Unable to allocate"
2261 core_scsi3_put_pr_reg(pr_reg);
2262 cmd->scsi_sense_reason =
2263 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2268 * sa_res_key=0 Unregister Reservation Key for registered I_T
2269 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2273 pr_holder = core_scsi3_check_implict_release(
2274 cmd->se_dev, pr_reg);
2275 if (pr_holder < 0) {
2276 kfree(pr_aptpl_buf);
2277 core_scsi3_put_pr_reg(pr_reg);
2278 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2282 spin_lock(&pr_tmpl->registration_lock);
2284 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2285 * and matching pr_res_key.
2287 if (pr_reg->pr_reg_all_tg_pt) {
2288 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2289 &pr_tmpl->registration_list,
2292 if (!pr_reg_p->pr_reg_all_tg_pt)
2295 if (pr_reg_p->pr_res_key != res_key)
2298 if (pr_reg == pr_reg_p)
2301 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2302 pr_reg_p->pr_reg_nacl->initiatorname))
2305 __core_scsi3_free_registration(dev,
2310 * Release the calling I_T Nexus registration now..
2312 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2315 * From spc4r17, section 5.7.11.3 Unregistering
2317 * If the persistent reservation is a registrants only
2318 * type, the device server shall establish a unit
2319 * attention condition for the initiator port associated
2320 * with every registered I_T nexus except for the I_T
2321 * nexus on which the PERSISTENT RESERVE OUT command was
2322 * received, with the additional sense code set to
2323 * RESERVATIONS RELEASED.
2326 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2327 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2328 list_for_each_entry(pr_reg_p,
2329 &pr_tmpl->registration_list,
2332 core_scsi3_ua_allocate(
2333 pr_reg_p->pr_reg_nacl,
2334 pr_reg_p->pr_res_mapped_lun,
2336 ASCQ_2AH_RESERVATIONS_RELEASED);
2339 spin_unlock(&pr_tmpl->registration_lock);
2342 pr_tmpl->pr_aptpl_active = 0;
2343 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2344 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2345 " for UNREGISTER\n");
2349 ret = core_scsi3_update_and_write_aptpl(dev,
2351 pr_tmpl->pr_aptpl_buf_len);
2353 pr_tmpl->pr_aptpl_active = 1;
2354 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2355 " for UNREGISTER\n");
2358 kfree(pr_aptpl_buf);
2362 * Increment PRgeneration counter for struct se_device"
2363 * upon a successful REGISTER, see spc4r17 section 6.3.2
2364 * READ_KEYS service action.
2366 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2368 pr_reg->pr_res_key = sa_res_key;
2369 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2370 " Key for %s to: 0x%016Lx PRgeneration:"
2371 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2372 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2373 pr_reg->pr_reg_nacl->initiatorname,
2374 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2377 pr_tmpl->pr_aptpl_active = 0;
2378 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2379 core_scsi3_put_pr_reg(pr_reg);
2380 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2385 ret = core_scsi3_update_and_write_aptpl(dev,
2387 pr_tmpl->pr_aptpl_buf_len);
2389 pr_tmpl->pr_aptpl_active = 1;
2390 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2394 kfree(pr_aptpl_buf);
2395 core_scsi3_put_pr_reg(pr_reg);
2401 unsigned char *core_scsi3_pr_dump_type(int type)
2404 case PR_TYPE_WRITE_EXCLUSIVE:
2405 return "Write Exclusive Access";
2406 case PR_TYPE_EXCLUSIVE_ACCESS:
2407 return "Exclusive Access";
2408 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2409 return "Write Exclusive Access, Registrants Only";
2410 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2411 return "Exclusive Access, Registrants Only";
2412 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2413 return "Write Exclusive Access, All Registrants";
2414 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2415 return "Exclusive Access, All Registrants";
2420 return "Unknown SPC-3 PR Type";
2423 static int core_scsi3_pro_reserve(
2425 struct se_device *dev,
2430 struct se_session *se_sess = cmd->se_sess;
2431 struct se_lun *se_lun = cmd->se_lun;
2432 struct t10_pr_registration *pr_reg, *pr_res_holder;
2433 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2434 char i_buf[PR_REG_ISID_ID_LEN];
2437 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2439 if (!se_sess || !se_lun) {
2440 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2441 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2445 * Locate the existing *pr_reg via struct se_node_acl pointers
2447 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2450 pr_err("SPC-3 PR: Unable to locate"
2451 " PR_REGISTERED *pr_reg for RESERVE\n");
2452 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2456 * From spc4r17 Section 5.7.9: Reserving:
2458 * An application client creates a persistent reservation by issuing
2459 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2460 * a registered I_T nexus with the following parameters:
2461 * a) RESERVATION KEY set to the value of the reservation key that is
2462 * registered with the logical unit for the I_T nexus; and
2464 if (res_key != pr_reg->pr_res_key) {
2465 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2466 " does not match existing SA REGISTER res_key:"
2467 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2468 core_scsi3_put_pr_reg(pr_reg);
2469 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2473 * From spc4r17 Section 5.7.9: Reserving:
2476 * b) TYPE field and SCOPE field set to the persistent reservation
2479 * Only one persistent reservation is allowed at a time per logical unit
2480 * and that persistent reservation has a scope of LU_SCOPE.
2482 if (scope != PR_SCOPE_LU_SCOPE) {
2483 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2484 core_scsi3_put_pr_reg(pr_reg);
2485 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2489 * See if we have an existing PR reservation holder pointer at
2490 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2493 spin_lock(&dev->dev_reservation_lock);
2494 pr_res_holder = dev->dev_pr_res_holder;
2495 if (pr_res_holder) {
2497 * From spc4r17 Section 5.7.9: Reserving:
2499 * If the device server receives a PERSISTENT RESERVE OUT
2500 * command from an I_T nexus other than a persistent reservation
2501 * holder (see 5.7.10) that attempts to create a persistent
2502 * reservation when a persistent reservation already exists for
2503 * the logical unit, then the command shall be completed with
2504 * RESERVATION CONFLICT status.
2506 if (pr_res_holder != pr_reg) {
2507 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2508 pr_err("SPC-3 PR: Attempted RESERVE from"
2509 " [%s]: %s while reservation already held by"
2510 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2511 cmd->se_tfo->get_fabric_name(),
2512 se_sess->se_node_acl->initiatorname,
2513 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2514 pr_res_holder->pr_reg_nacl->initiatorname);
2516 spin_unlock(&dev->dev_reservation_lock);
2517 core_scsi3_put_pr_reg(pr_reg);
2518 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2522 * From spc4r17 Section 5.7.9: Reserving:
2524 * If a persistent reservation holder attempts to modify the
2525 * type or scope of an existing persistent reservation, the
2526 * command shall be completed with RESERVATION CONFLICT status.
2528 if ((pr_res_holder->pr_res_type != type) ||
2529 (pr_res_holder->pr_res_scope != scope)) {
2530 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2531 pr_err("SPC-3 PR: Attempted RESERVE from"
2532 " [%s]: %s trying to change TYPE and/or SCOPE,"
2533 " while reservation already held by [%s]: %s,"
2534 " returning RESERVATION_CONFLICT\n",
2535 cmd->se_tfo->get_fabric_name(),
2536 se_sess->se_node_acl->initiatorname,
2537 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2538 pr_res_holder->pr_reg_nacl->initiatorname);
2540 spin_unlock(&dev->dev_reservation_lock);
2541 core_scsi3_put_pr_reg(pr_reg);
2542 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2546 * From spc4r17 Section 5.7.9: Reserving:
2548 * If the device server receives a PERSISTENT RESERVE OUT
2549 * command with RESERVE service action where the TYPE field and
2550 * the SCOPE field contain the same values as the existing type
2551 * and scope from a persistent reservation holder, it shall not
2552 * make any change to the existing persistent reservation and
2553 * shall completethe command with GOOD status.
2555 spin_unlock(&dev->dev_reservation_lock);
2556 core_scsi3_put_pr_reg(pr_reg);
2560 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2561 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2563 pr_reg->pr_res_scope = scope;
2564 pr_reg->pr_res_type = type;
2565 pr_reg->pr_res_holder = 1;
2566 dev->dev_pr_res_holder = pr_reg;
2567 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2568 PR_REG_ISID_ID_LEN);
2570 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2571 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2572 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2573 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2574 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2575 cmd->se_tfo->get_fabric_name(),
2576 se_sess->se_node_acl->initiatorname,
2577 (prf_isid) ? &i_buf[0] : "");
2578 spin_unlock(&dev->dev_reservation_lock);
2580 if (pr_tmpl->pr_aptpl_active) {
2581 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2582 &pr_reg->pr_aptpl_buf[0],
2583 pr_tmpl->pr_aptpl_buf_len);
2585 pr_debug("SPC-3 PR: Updated APTPL metadata"
2589 core_scsi3_put_pr_reg(pr_reg);
2593 static int core_scsi3_emulate_pro_reserve(
2599 struct se_device *dev = cmd->se_dev;
2603 case PR_TYPE_WRITE_EXCLUSIVE:
2604 case PR_TYPE_EXCLUSIVE_ACCESS:
2605 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2606 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2607 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2608 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2609 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2612 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2614 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2622 * Called with struct se_device->dev_reservation_lock held.
2624 static void __core_scsi3_complete_pro_release(
2625 struct se_device *dev,
2626 struct se_node_acl *se_nacl,
2627 struct t10_pr_registration *pr_reg,
2630 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2631 char i_buf[PR_REG_ISID_ID_LEN];
2634 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2635 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2636 PR_REG_ISID_ID_LEN);
2638 * Go ahead and release the current PR reservation holder.
2640 dev->dev_pr_res_holder = NULL;
2642 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2643 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2644 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2645 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2646 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2647 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2648 tfo->get_fabric_name(), se_nacl->initiatorname,
2649 (prf_isid) ? &i_buf[0] : "");
2651 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2653 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2656 static int core_scsi3_emulate_pro_release(
2662 struct se_device *dev = cmd->se_dev;
2663 struct se_session *se_sess = cmd->se_sess;
2664 struct se_lun *se_lun = cmd->se_lun;
2665 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2666 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2667 int ret, all_reg = 0;
2669 if (!se_sess || !se_lun) {
2670 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2671 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2675 * Locate the existing *pr_reg via struct se_node_acl pointers
2677 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2679 pr_err("SPC-3 PR: Unable to locate"
2680 " PR_REGISTERED *pr_reg for RELEASE\n");
2681 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2685 * From spc4r17 Section 5.7.11.2 Releasing:
2687 * If there is no persistent reservation or in response to a persistent
2688 * reservation release request from a registered I_T nexus that is not a
2689 * persistent reservation holder (see 5.7.10), the device server shall
2692 * a) Not release the persistent reservation, if any;
2693 * b) Not remove any registrations; and
2694 * c) Complete the command with GOOD status.
2696 spin_lock(&dev->dev_reservation_lock);
2697 pr_res_holder = dev->dev_pr_res_holder;
2698 if (!pr_res_holder) {
2700 * No persistent reservation, return GOOD status.
2702 spin_unlock(&dev->dev_reservation_lock);
2703 core_scsi3_put_pr_reg(pr_reg);
2706 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2707 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2710 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2712 * Non 'All Registrants' PR Type cases..
2713 * Release request from a registered I_T nexus that is not a
2714 * persistent reservation holder. return GOOD status.
2716 spin_unlock(&dev->dev_reservation_lock);
2717 core_scsi3_put_pr_reg(pr_reg);
2721 * From spc4r17 Section 5.7.11.2 Releasing:
2723 * Only the persistent reservation holder (see 5.7.10) is allowed to
2724 * release a persistent reservation.
2726 * An application client releases the persistent reservation by issuing
2727 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2728 * an I_T nexus that is a persistent reservation holder with the
2729 * following parameters:
2731 * a) RESERVATION KEY field set to the value of the reservation key
2732 * that is registered with the logical unit for the I_T nexus;
2734 if (res_key != pr_reg->pr_res_key) {
2735 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2736 " does not match existing SA REGISTER res_key:"
2737 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2738 spin_unlock(&dev->dev_reservation_lock);
2739 core_scsi3_put_pr_reg(pr_reg);
2740 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2744 * From spc4r17 Section 5.7.11.2 Releasing and above:
2746 * b) TYPE field and SCOPE field set to match the persistent
2747 * reservation being released.
2749 if ((pr_res_holder->pr_res_type != type) ||
2750 (pr_res_holder->pr_res_scope != scope)) {
2751 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2752 pr_err("SPC-3 PR RELEASE: Attempted to release"
2753 " reservation from [%s]: %s with different TYPE "
2754 "and/or SCOPE while reservation already held by"
2755 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2756 cmd->se_tfo->get_fabric_name(),
2757 se_sess->se_node_acl->initiatorname,
2758 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2759 pr_res_holder->pr_reg_nacl->initiatorname);
2761 spin_unlock(&dev->dev_reservation_lock);
2762 core_scsi3_put_pr_reg(pr_reg);
2763 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2767 * In response to a persistent reservation release request from the
2768 * persistent reservation holder the device server shall perform a
2769 * release by doing the following as an uninterrupted series of actions:
2770 * a) Release the persistent reservation;
2771 * b) Not remove any registration(s);
2772 * c) If the released persistent reservation is a registrants only type
2773 * or all registrants type persistent reservation,
2774 * the device server shall establish a unit attention condition for
2775 * the initiator port associated with every regis-
2776 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2777 * RESERVE OUT command with RELEASE service action was received,
2778 * with the additional sense code set to RESERVATIONS RELEASED; and
2779 * d) If the persistent reservation is of any other type, the device
2780 * server shall not establish a unit attention condition.
2782 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2785 spin_unlock(&dev->dev_reservation_lock);
2787 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2788 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2789 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2790 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2792 * If no UNIT ATTENTION conditions will be established for
2793 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2794 * go ahead and check for APTPL=1 update+write below
2799 spin_lock(&pr_tmpl->registration_lock);
2800 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2803 * Do not establish a UNIT ATTENTION condition
2804 * for the calling I_T Nexus
2806 if (pr_reg_p == pr_reg)
2809 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2810 pr_reg_p->pr_res_mapped_lun,
2811 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2813 spin_unlock(&pr_tmpl->registration_lock);
2816 if (pr_tmpl->pr_aptpl_active) {
2817 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2818 &pr_reg->pr_aptpl_buf[0],
2819 pr_tmpl->pr_aptpl_buf_len);
2821 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2824 core_scsi3_put_pr_reg(pr_reg);
2828 static int core_scsi3_emulate_pro_clear(
2832 struct se_device *dev = cmd->se_dev;
2833 struct se_node_acl *pr_reg_nacl;
2834 struct se_session *se_sess = cmd->se_sess;
2835 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2836 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2837 u32 pr_res_mapped_lun = 0;
2838 int calling_it_nexus = 0;
2840 * Locate the existing *pr_reg via struct se_node_acl pointers
2842 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2843 se_sess->se_node_acl, se_sess);
2845 pr_err("SPC-3 PR: Unable to locate"
2846 " PR_REGISTERED *pr_reg for CLEAR\n");
2847 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2851 * From spc4r17 section 5.7.11.6, Clearing:
2853 * Any application client may release the persistent reservation and
2854 * remove all registrations from a device server by issuing a
2855 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2856 * registered I_T nexus with the following parameter:
2858 * a) RESERVATION KEY field set to the value of the reservation key
2859 * that is registered with the logical unit for the I_T nexus.
2861 if (res_key != pr_reg_n->pr_res_key) {
2862 pr_err("SPC-3 PR REGISTER: Received"
2863 " res_key: 0x%016Lx does not match"
2864 " existing SA REGISTER res_key:"
2865 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2866 core_scsi3_put_pr_reg(pr_reg_n);
2867 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2871 * a) Release the persistent reservation, if any;
2873 spin_lock(&dev->dev_reservation_lock);
2874 pr_res_holder = dev->dev_pr_res_holder;
2875 if (pr_res_holder) {
2876 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2877 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2880 spin_unlock(&dev->dev_reservation_lock);
2882 * b) Remove all registration(s) (see spc4r17 5.7.7);
2884 spin_lock(&pr_tmpl->registration_lock);
2885 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2886 &pr_tmpl->registration_list, pr_reg_list) {
2888 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2889 pr_reg_nacl = pr_reg->pr_reg_nacl;
2890 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2891 __core_scsi3_free_registration(dev, pr_reg, NULL,
2894 * e) Establish a unit attention condition for the initiator
2895 * port associated with every registered I_T nexus other
2896 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2897 * command with CLEAR service action was received, with the
2898 * additional sense code set to RESERVATIONS PREEMPTED.
2900 if (!calling_it_nexus)
2901 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2902 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2904 spin_unlock(&pr_tmpl->registration_lock);
2906 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2907 cmd->se_tfo->get_fabric_name());
2909 if (pr_tmpl->pr_aptpl_active) {
2910 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2911 pr_debug("SPC-3 PR: Updated APTPL metadata"
2915 core_scsi3_pr_generation(dev);
2920 * Called with struct se_device->dev_reservation_lock held.
2922 static void __core_scsi3_complete_pro_preempt(
2923 struct se_device *dev,
2924 struct t10_pr_registration *pr_reg,
2925 struct list_head *preempt_and_abort_list,
2930 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2931 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2932 char i_buf[PR_REG_ISID_ID_LEN];
2935 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2936 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2937 PR_REG_ISID_ID_LEN);
2939 * Do an implict RELEASE of the existing reservation.
2941 if (dev->dev_pr_res_holder)
2942 __core_scsi3_complete_pro_release(dev, nacl,
2943 dev->dev_pr_res_holder, 0);
2945 dev->dev_pr_res_holder = pr_reg;
2946 pr_reg->pr_res_holder = 1;
2947 pr_reg->pr_res_type = type;
2948 pr_reg->pr_res_scope = scope;
2950 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2951 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2952 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2953 core_scsi3_pr_dump_type(type),
2954 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2955 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2956 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2957 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2959 * For PREEMPT_AND_ABORT, add the preempting reservation's
2960 * struct t10_pr_registration to the list that will be compared
2961 * against received CDBs..
2963 if (preempt_and_abort_list)
2964 list_add_tail(&pr_reg->pr_reg_abort_list,
2965 preempt_and_abort_list);
2968 static void core_scsi3_release_preempt_and_abort(
2969 struct list_head *preempt_and_abort_list,
2970 struct t10_pr_registration *pr_reg_holder)
2972 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2974 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2975 pr_reg_abort_list) {
2977 list_del(&pr_reg->pr_reg_abort_list);
2978 if (pr_reg_holder == pr_reg)
2980 if (pr_reg->pr_res_holder) {
2981 pr_warn("pr_reg->pr_res_holder still set\n");
2985 pr_reg->pr_reg_deve = NULL;
2986 pr_reg->pr_reg_nacl = NULL;
2987 kfree(pr_reg->pr_aptpl_buf);
2988 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2992 static int core_scsi3_pro_preempt(
3000 struct se_device *dev = cmd->se_dev;
3001 struct se_node_acl *pr_reg_nacl;
3002 struct se_session *se_sess = cmd->se_sess;
3003 LIST_HEAD(preempt_and_abort_list);
3004 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
3005 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3006 u32 pr_res_mapped_lun = 0;
3007 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3008 int prh_type = 0, prh_scope = 0, ret;
3011 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3015 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3018 pr_err("SPC-3 PR: Unable to locate"
3019 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3020 (abort) ? "_AND_ABORT" : "");
3021 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3024 if (pr_reg_n->pr_res_key != res_key) {
3025 core_scsi3_put_pr_reg(pr_reg_n);
3026 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3029 if (scope != PR_SCOPE_LU_SCOPE) {
3030 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3031 core_scsi3_put_pr_reg(pr_reg_n);
3032 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3036 spin_lock(&dev->dev_reservation_lock);
3037 pr_res_holder = dev->dev_pr_res_holder;
3038 if (pr_res_holder &&
3039 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3040 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3043 if (!all_reg && !sa_res_key) {
3044 spin_unlock(&dev->dev_reservation_lock);
3045 core_scsi3_put_pr_reg(pr_reg_n);
3046 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3050 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3052 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3053 * persistent reservation holder or there is no persistent reservation
3054 * holder (i.e., there is no persistent reservation), then the device
3055 * server shall perform a preempt by doing the following in an
3056 * uninterrupted series of actions. (See below..)
3058 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3060 * No existing or SA Reservation Key matching reservations..
3062 * PROUT SA PREEMPT with All Registrant type reservations are
3063 * allowed to be processed without a matching SA Reservation Key
3065 spin_lock(&pr_tmpl->registration_lock);
3066 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3067 &pr_tmpl->registration_list, pr_reg_list) {
3069 * Removing of registrations in non all registrants
3070 * type reservations without a matching SA reservation
3073 * a) Remove the registrations for all I_T nexuses
3074 * specified by the SERVICE ACTION RESERVATION KEY
3076 * b) Ignore the contents of the SCOPE and TYPE fields;
3077 * c) Process tasks as defined in 5.7.1; and
3078 * d) Establish a unit attention condition for the
3079 * initiator port associated with every I_T nexus
3080 * that lost its registration other than the I_T
3081 * nexus on which the PERSISTENT RESERVE OUT command
3082 * was received, with the additional sense code set
3083 * to REGISTRATIONS PREEMPTED.
3086 if (pr_reg->pr_res_key != sa_res_key)
3089 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3090 pr_reg_nacl = pr_reg->pr_reg_nacl;
3091 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3092 __core_scsi3_free_registration(dev, pr_reg,
3093 (abort) ? &preempt_and_abort_list :
3094 NULL, calling_it_nexus);
3098 * Case for any existing all registrants type
3099 * reservation, follow logic in spc4r17 section
3100 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3102 * For a ZERO SA Reservation key, release
3103 * all other registrations and do an implict
3104 * release of active persistent reservation.
3106 * For a non-ZERO SA Reservation key, only
3107 * release the matching reservation key from
3111 (pr_reg->pr_res_key != sa_res_key))
3114 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3115 if (calling_it_nexus)
3118 pr_reg_nacl = pr_reg->pr_reg_nacl;
3119 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3120 __core_scsi3_free_registration(dev, pr_reg,
3121 (abort) ? &preempt_and_abort_list :
3125 if (!calling_it_nexus)
3126 core_scsi3_ua_allocate(pr_reg_nacl,
3127 pr_res_mapped_lun, 0x2A,
3128 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3130 spin_unlock(&pr_tmpl->registration_lock);
3132 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3133 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3134 * RESERVATION KEY field to a value that does not match any
3135 * registered reservation key, then the device server shall
3136 * complete the command with RESERVATION CONFLICT status.
3138 if (!released_regs) {
3139 spin_unlock(&dev->dev_reservation_lock);
3140 core_scsi3_put_pr_reg(pr_reg_n);
3141 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3145 * For an existing all registrants type reservation
3146 * with a zero SA rservation key, preempt the existing
3147 * reservation with the new PR type and scope.
3149 if (pr_res_holder && all_reg && !(sa_res_key)) {
3150 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3151 (abort) ? &preempt_and_abort_list : NULL,
3152 type, scope, abort);
3155 core_scsi3_release_preempt_and_abort(
3156 &preempt_and_abort_list, pr_reg_n);
3158 spin_unlock(&dev->dev_reservation_lock);
3160 if (pr_tmpl->pr_aptpl_active) {
3161 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3162 &pr_reg_n->pr_aptpl_buf[0],
3163 pr_tmpl->pr_aptpl_buf_len);
3165 pr_debug("SPC-3 PR: Updated APTPL"
3166 " metadata for PREEMPT%s\n", (abort) ?
3170 core_scsi3_put_pr_reg(pr_reg_n);
3171 core_scsi3_pr_generation(cmd->se_dev);
3175 * The PREEMPTing SA reservation key matches that of the
3176 * existing persistent reservation, first, we check if
3177 * we are preempting our own reservation.
3178 * From spc4r17, section 5.7.11.4.3 Preempting
3179 * persistent reservations and registration handling
3181 * If an all registrants persistent reservation is not
3182 * present, it is not an error for the persistent
3183 * reservation holder to preempt itself (i.e., a
3184 * PERSISTENT RESERVE OUT with a PREEMPT service action
3185 * or a PREEMPT AND ABORT service action with the
3186 * SERVICE ACTION RESERVATION KEY value equal to the
3187 * persistent reservation holder's reservation key that
3188 * is received from the persistent reservation holder).
3189 * In that case, the device server shall establish the
3190 * new persistent reservation and maintain the
3193 prh_type = pr_res_holder->pr_res_type;
3194 prh_scope = pr_res_holder->pr_res_scope;
3196 * If the SERVICE ACTION RESERVATION KEY field identifies a
3197 * persistent reservation holder (see 5.7.10), the device
3198 * server shall perform a preempt by doing the following as
3199 * an uninterrupted series of actions:
3201 * a) Release the persistent reservation for the holder
3202 * identified by the SERVICE ACTION RESERVATION KEY field;
3204 if (pr_reg_n != pr_res_holder)
3205 __core_scsi3_complete_pro_release(dev,
3206 pr_res_holder->pr_reg_nacl,
3207 dev->dev_pr_res_holder, 0);
3209 * b) Remove the registrations for all I_T nexuses identified
3210 * by the SERVICE ACTION RESERVATION KEY field, except the
3211 * I_T nexus that is being used for the PERSISTENT RESERVE
3212 * OUT command. If an all registrants persistent reservation
3213 * is present and the SERVICE ACTION RESERVATION KEY field
3214 * is set to zero, then all registrations shall be removed
3215 * except for that of the I_T nexus that is being used for
3216 * the PERSISTENT RESERVE OUT command;
3218 spin_lock(&pr_tmpl->registration_lock);
3219 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3220 &pr_tmpl->registration_list, pr_reg_list) {
3222 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3223 if (calling_it_nexus)
3226 if (pr_reg->pr_res_key != sa_res_key)
3229 pr_reg_nacl = pr_reg->pr_reg_nacl;
3230 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3231 __core_scsi3_free_registration(dev, pr_reg,
3232 (abort) ? &preempt_and_abort_list : NULL,
3235 * e) Establish a unit attention condition for the initiator
3236 * port associated with every I_T nexus that lost its
3237 * persistent reservation and/or registration, with the
3238 * additional sense code set to REGISTRATIONS PREEMPTED;
3240 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3241 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3243 spin_unlock(&pr_tmpl->registration_lock);
3245 * c) Establish a persistent reservation for the preempting
3246 * I_T nexus using the contents of the SCOPE and TYPE fields;
3248 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3249 (abort) ? &preempt_and_abort_list : NULL,
3250 type, scope, abort);
3252 * d) Process tasks as defined in 5.7.1;
3254 * f) If the type or scope has changed, then for every I_T nexus
3255 * whose reservation key was not removed, except for the I_T
3256 * nexus on which the PERSISTENT RESERVE OUT command was
3257 * received, the device server shall establish a unit
3258 * attention condition for the initiator port associated with
3259 * that I_T nexus, with the additional sense code set to
3260 * RESERVATIONS RELEASED. If the type or scope have not
3261 * changed, then no unit attention condition(s) shall be
3262 * established for this reason.
3264 if ((prh_type != type) || (prh_scope != scope)) {
3265 spin_lock(&pr_tmpl->registration_lock);
3266 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3267 &pr_tmpl->registration_list, pr_reg_list) {
3269 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3270 if (calling_it_nexus)
3273 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3274 pr_reg->pr_res_mapped_lun, 0x2A,
3275 ASCQ_2AH_RESERVATIONS_RELEASED);
3277 spin_unlock(&pr_tmpl->registration_lock);
3279 spin_unlock(&dev->dev_reservation_lock);
3281 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3282 * All received CDBs for the matching existing reservation and
3283 * registrations undergo ABORT_TASK logic.
3285 * From there, core_scsi3_release_preempt_and_abort() will
3286 * release every registration in the list (which have already
3287 * been removed from the primary pr_reg list), except the
3288 * new persistent reservation holder, the calling Initiator Port.
3291 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3292 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3296 if (pr_tmpl->pr_aptpl_active) {
3297 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3298 &pr_reg_n->pr_aptpl_buf[0],
3299 pr_tmpl->pr_aptpl_buf_len);
3301 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3302 "%s\n", (abort) ? "_AND_ABORT" : "");
3305 core_scsi3_put_pr_reg(pr_reg_n);
3306 core_scsi3_pr_generation(cmd->se_dev);
3310 static int core_scsi3_emulate_pro_preempt(
3321 case PR_TYPE_WRITE_EXCLUSIVE:
3322 case PR_TYPE_EXCLUSIVE_ACCESS:
3323 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3324 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3325 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3326 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3327 ret = core_scsi3_pro_preempt(cmd, type, scope,
3328 res_key, sa_res_key, abort);
3331 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3332 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3333 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3341 static int core_scsi3_emulate_pro_register_and_move(
3348 struct se_session *se_sess = cmd->se_sess;
3349 struct se_device *dev = cmd->se_dev;
3350 struct se_dev_entry *dest_se_deve = NULL;
3351 struct se_lun *se_lun = cmd->se_lun;
3352 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3353 struct se_port *se_port;
3354 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3355 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3356 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3357 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3359 unsigned char *initiator_str;
3360 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3361 u32 tid_len, tmp_tid_len;
3362 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3363 unsigned short rtpi;
3364 unsigned char proto_ident;
3366 if (!se_sess || !se_lun) {
3367 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3368 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3371 memset(dest_iport, 0, 64);
3372 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3373 se_tpg = se_sess->se_tpg;
3374 tf_ops = se_tpg->se_tpg_tfo;
3376 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3377 * Register behaviors for a REGISTER AND MOVE service action
3379 * Locate the existing *pr_reg via struct se_node_acl pointers
3381 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3384 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3385 " *pr_reg for REGISTER_AND_MOVE\n");
3386 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3390 * The provided reservation key much match the existing reservation key
3391 * provided during this initiator's I_T nexus registration.
3393 if (res_key != pr_reg->pr_res_key) {
3394 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3395 " res_key: 0x%016Lx does not match existing SA REGISTER"
3396 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3397 core_scsi3_put_pr_reg(pr_reg);
3398 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3402 * The service active reservation key needs to be non zero
3405 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3407 core_scsi3_put_pr_reg(pr_reg);
3408 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3413 * Determine the Relative Target Port Identifier where the reservation
3414 * will be moved to for the TransportID containing SCSI initiator WWN
3417 buf = transport_kmap_data_sg(cmd);
3418 rtpi = (buf[18] & 0xff) << 8;
3419 rtpi |= buf[19] & 0xff;
3420 tid_len = (buf[20] & 0xff) << 24;
3421 tid_len |= (buf[21] & 0xff) << 16;
3422 tid_len |= (buf[22] & 0xff) << 8;
3423 tid_len |= buf[23] & 0xff;
3424 transport_kunmap_data_sg(cmd);
3427 if ((tid_len + 24) != cmd->data_length) {
3428 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3429 " does not equal CDB data_length: %u\n", tid_len,
3431 core_scsi3_put_pr_reg(pr_reg);
3432 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3436 spin_lock(&dev->se_port_lock);
3437 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3438 if (se_port->sep_rtpi != rtpi)
3440 dest_se_tpg = se_port->sep_tpg;
3443 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3447 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3448 smp_mb__after_atomic_inc();
3449 spin_unlock(&dev->se_port_lock);
3451 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3453 pr_err("core_scsi3_tpg_depend_item() failed"
3454 " for dest_se_tpg\n");
3455 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3456 smp_mb__after_atomic_dec();
3457 core_scsi3_put_pr_reg(pr_reg);
3458 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3462 spin_lock(&dev->se_port_lock);
3465 spin_unlock(&dev->se_port_lock);
3467 if (!dest_se_tpg || !dest_tf_ops) {
3468 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3469 " fabric ops from Relative Target Port Identifier:"
3471 core_scsi3_put_pr_reg(pr_reg);
3472 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3476 buf = transport_kmap_data_sg(cmd);
3477 proto_ident = (buf[24] & 0x0f);
3479 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3480 " 0x%02x\n", proto_ident);
3482 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3483 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3484 " proto_ident: 0x%02x does not match ident: 0x%02x"
3485 " from fabric: %s\n", proto_ident,
3486 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3487 dest_tf_ops->get_fabric_name());
3488 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3492 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3493 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3494 " containg a valid tpg_parse_pr_out_transport_id"
3495 " function pointer\n");
3496 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3500 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3501 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3502 if (!initiator_str) {
3503 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3504 " initiator_str from Transport ID\n");
3505 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3510 transport_kunmap_data_sg(cmd);
3513 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3514 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3515 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3518 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3519 * action specifies a TransportID that is the same as the initiator port
3520 * of the I_T nexus for the command received, then the command shall
3521 * be terminated with CHECK CONDITION status, with the sense key set to
3522 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3523 * IN PARAMETER LIST.
3525 pr_reg_nacl = pr_reg->pr_reg_nacl;
3526 matching_iname = (!strcmp(initiator_str,
3527 pr_reg_nacl->initiatorname)) ? 1 : 0;
3528 if (!matching_iname)
3529 goto after_iport_check;
3531 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3532 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3533 " matches: %s on received I_T Nexus\n", initiator_str,
3534 pr_reg_nacl->initiatorname);
3535 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3539 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3540 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3541 " matches: %s %s on received I_T Nexus\n",
3542 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3543 pr_reg->pr_reg_isid);
3544 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3550 * Locate the destination struct se_node_acl from the received Transport ID
3552 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3553 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3555 if (dest_node_acl) {
3556 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3557 smp_mb__after_atomic_inc();
3559 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3561 if (!dest_node_acl) {
3562 pr_err("Unable to locate %s dest_node_acl for"
3563 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3565 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3569 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3571 pr_err("core_scsi3_nodeacl_depend_item() for"
3572 " dest_node_acl\n");
3573 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3574 smp_mb__after_atomic_dec();
3575 dest_node_acl = NULL;
3576 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3581 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3582 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3583 dest_node_acl->initiatorname);
3586 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3589 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3590 if (!dest_se_deve) {
3591 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3592 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3593 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3598 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3600 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3601 atomic_dec(&dest_se_deve->pr_ref_count);
3602 smp_mb__after_atomic_dec();
3603 dest_se_deve = NULL;
3604 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3609 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3610 " ACL for dest_se_deve->mapped_lun: %u\n",
3611 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3612 dest_se_deve->mapped_lun);
3615 * A persistent reservation needs to already existing in order to
3616 * successfully complete the REGISTER_AND_MOVE service action..
3618 spin_lock(&dev->dev_reservation_lock);
3619 pr_res_holder = dev->dev_pr_res_holder;
3620 if (!pr_res_holder) {
3621 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3622 " currently held\n");
3623 spin_unlock(&dev->dev_reservation_lock);
3624 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3629 * The received on I_T Nexus must be the reservation holder.
3631 * From spc4r17 section 5.7.8 Table 50 --
3632 * Register behaviors for a REGISTER AND MOVE service action
3634 if (pr_res_holder != pr_reg) {
3635 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3636 " Nexus is not reservation holder\n");
3637 spin_unlock(&dev->dev_reservation_lock);
3638 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3643 * From spc4r17 section 5.7.8: registering and moving reservation
3645 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3646 * action is received and the established persistent reservation is a
3647 * Write Exclusive - All Registrants type or Exclusive Access -
3648 * All Registrants type reservation, then the command shall be completed
3649 * with RESERVATION CONFLICT status.
3651 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3652 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3653 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3654 " reservation for type: %s\n",
3655 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3656 spin_unlock(&dev->dev_reservation_lock);
3657 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3661 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3663 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3665 type = pr_res_holder->pr_res_type;
3666 scope = pr_res_holder->pr_res_type;
3668 * c) Associate the reservation key specified in the SERVICE ACTION
3669 * RESERVATION KEY field with the I_T nexus specified as the
3670 * destination of the register and move, where:
3671 * A) The I_T nexus is specified by the TransportID and the
3672 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3673 * B) Regardless of the TransportID format used, the association for
3674 * the initiator port is based on either the initiator port name
3675 * (see 3.1.71) on SCSI transport protocols where port names are
3676 * required or the initiator port identifier (see 3.1.70) on SCSI
3677 * transport protocols where port names are not required;
3678 * d) Register the reservation key specified in the SERVICE ACTION
3679 * RESERVATION KEY field;
3680 * e) Retain the reservation key specified in the SERVICE ACTION
3681 * RESERVATION KEY field and associated information;
3683 * Also, It is not an error for a REGISTER AND MOVE service action to
3684 * register an I_T nexus that is already registered with the same
3685 * reservation key or a different reservation key.
3687 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3690 ret = core_scsi3_alloc_registration(cmd->se_dev,
3691 dest_node_acl, dest_se_deve, iport_ptr,
3692 sa_res_key, 0, aptpl, 2, 1);
3694 spin_unlock(&dev->dev_reservation_lock);
3695 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3699 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3704 * f) Release the persistent reservation for the persistent reservation
3705 * holder (i.e., the I_T nexus on which the
3707 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3708 dev->dev_pr_res_holder, 0);
3710 * g) Move the persistent reservation to the specified I_T nexus using
3711 * the same scope and type as the persistent reservation released in
3714 dev->dev_pr_res_holder = dest_pr_reg;
3715 dest_pr_reg->pr_res_holder = 1;
3716 dest_pr_reg->pr_res_type = type;
3717 pr_reg->pr_res_scope = scope;
3718 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3719 PR_REG_ISID_ID_LEN);
3721 * Increment PRGeneration for existing registrations..
3724 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3725 spin_unlock(&dev->dev_reservation_lock);
3727 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3728 " created new reservation holder TYPE: %s on object RTPI:"
3729 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3730 core_scsi3_pr_dump_type(type), rtpi,
3731 dest_pr_reg->pr_res_generation);
3732 pr_debug("SPC-3 PR Successfully moved reservation from"
3733 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3734 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3735 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3736 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3739 * It is now safe to release configfs group dependencies for destination
3740 * of Transport ID Initiator Device/Port Identifier
3742 core_scsi3_lunacl_undepend_item(dest_se_deve);
3743 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3744 core_scsi3_tpg_undepend_item(dest_se_tpg);
3746 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3747 * nexus on which PERSISTENT RESERVE OUT command was received.
3750 spin_lock(&pr_tmpl->registration_lock);
3751 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3752 spin_unlock(&pr_tmpl->registration_lock);
3754 core_scsi3_put_pr_reg(pr_reg);
3757 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3758 * write out the updated metadata to struct file for this SCSI device.
3761 pr_tmpl->pr_aptpl_active = 0;
3762 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3763 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3764 " REGISTER_AND_MOVE\n");
3766 pr_tmpl->pr_aptpl_active = 1;
3767 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3768 &dest_pr_reg->pr_aptpl_buf[0],
3769 pr_tmpl->pr_aptpl_buf_len);
3771 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3772 " REGISTER_AND_MOVE\n");
3775 transport_kunmap_data_sg(cmd);
3777 core_scsi3_put_pr_reg(dest_pr_reg);
3781 transport_kunmap_data_sg(cmd);
3783 core_scsi3_lunacl_undepend_item(dest_se_deve);
3785 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3786 core_scsi3_tpg_undepend_item(dest_se_tpg);
3787 core_scsi3_put_pr_reg(pr_reg);
3791 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3793 unsigned int __v1, __v2;
3795 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3796 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3798 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3802 * See spc4r17 section 6.14 Table 170
3804 int target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3806 unsigned char *cdb = &cmd->t_task_cdb[0];
3808 u64 res_key, sa_res_key;
3809 int sa, scope, type, aptpl;
3810 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3814 * Following spc2r20 5.5.1 Reservations overview:
3816 * If a logical unit has been reserved by any RESERVE command and is
3817 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3818 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3819 * initiator or service action and shall terminate with a RESERVATION
3822 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3823 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3824 " SPC-2 reservation is held, returning"
3825 " RESERVATION_CONFLICT\n");
3826 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3832 * FIXME: A NULL struct se_session pointer means an this is not coming from
3833 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3835 if (!cmd->se_sess) {
3836 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3841 if (cmd->data_length < 24) {
3842 pr_warn("SPC-PR: Received PR OUT parameter list"
3843 " length too small: %u\n", cmd->data_length);
3844 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3849 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3851 sa = (cdb[1] & 0x1f);
3852 scope = (cdb[2] & 0xf0);
3853 type = (cdb[2] & 0x0f);
3855 buf = transport_kmap_data_sg(cmd);
3857 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3859 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3860 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3862 * REGISTER_AND_MOVE uses a different SA parameter list containing
3863 * SCSI TransportIDs.
3865 if (sa != PRO_REGISTER_AND_MOVE) {
3866 spec_i_pt = (buf[20] & 0x08);
3867 all_tg_pt = (buf[20] & 0x04);
3868 aptpl = (buf[20] & 0x01);
3870 aptpl = (buf[17] & 0x01);
3871 unreg = (buf[17] & 0x02);
3873 transport_kunmap_data_sg(cmd);
3877 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3879 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3880 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3886 * From spc4r17 section 6.14:
3888 * If the SPEC_I_PT bit is set to zero, the service action is not
3889 * REGISTER AND MOVE, and the parameter list length is not 24, then
3890 * the command shall be terminated with CHECK CONDITION status, with
3891 * the sense key set to ILLEGAL REQUEST, and the additional sense
3892 * code set to PARAMETER LIST LENGTH ERROR.
3894 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3895 (cmd->data_length != 24)) {
3896 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3897 " list length: %u\n", cmd->data_length);
3898 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3903 * (core_scsi3_emulate_pro_* function parameters
3904 * are defined by spc4r17 Table 174:
3905 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3909 ret = core_scsi3_emulate_pro_register(cmd,
3910 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3913 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3916 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3919 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3922 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3923 res_key, sa_res_key, 0);
3925 case PRO_PREEMPT_AND_ABORT:
3926 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3927 res_key, sa_res_key, 1);
3929 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3930 ret = core_scsi3_emulate_pro_register(cmd,
3931 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3933 case PRO_REGISTER_AND_MOVE:
3934 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3935 sa_res_key, aptpl, unreg);
3938 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3939 " action: 0x%02x\n", cdb[1] & 0x1f);
3940 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3947 target_complete_cmd(cmd, GOOD);
3952 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3954 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3956 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3958 struct se_device *dev = cmd->se_dev;
3959 struct t10_pr_registration *pr_reg;
3961 u32 add_len = 0, off = 8;
3963 if (cmd->data_length < 8) {
3964 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3965 " too small\n", cmd->data_length);
3966 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3970 buf = transport_kmap_data_sg(cmd);
3971 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3972 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3973 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3974 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3976 spin_lock(&dev->t10_pr.registration_lock);
3977 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3980 * Check for overflow of 8byte PRI READ_KEYS payload and
3981 * next reservation key list descriptor.
3983 if ((add_len + 8) > (cmd->data_length - 8))
3986 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3987 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3988 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3989 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3990 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3991 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3992 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3993 buf[off++] = (pr_reg->pr_res_key & 0xff);
3997 spin_unlock(&dev->t10_pr.registration_lock);
3999 buf[4] = ((add_len >> 24) & 0xff);
4000 buf[5] = ((add_len >> 16) & 0xff);
4001 buf[6] = ((add_len >> 8) & 0xff);
4002 buf[7] = (add_len & 0xff);
4004 transport_kunmap_data_sg(cmd);
4010 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4012 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4014 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4016 struct se_device *dev = cmd->se_dev;
4017 struct t10_pr_registration *pr_reg;
4020 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4022 if (cmd->data_length < 8) {
4023 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4024 " too small\n", cmd->data_length);
4025 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4029 buf = transport_kmap_data_sg(cmd);
4030 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
4031 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
4032 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
4033 buf[3] = (dev->t10_pr.pr_generation & 0xff);
4035 spin_lock(&dev->dev_reservation_lock);
4036 pr_reg = dev->dev_pr_res_holder;
4039 * Set the hardcoded Additional Length
4041 buf[4] = ((add_len >> 24) & 0xff);
4042 buf[5] = ((add_len >> 16) & 0xff);
4043 buf[6] = ((add_len >> 8) & 0xff);
4044 buf[7] = (add_len & 0xff);
4046 if (cmd->data_length < 22)
4050 * Set the Reservation key.
4052 * From spc4r17, section 5.7.10:
4053 * A persistent reservation holder has its reservation key
4054 * returned in the parameter data from a PERSISTENT
4055 * RESERVE IN command with READ RESERVATION service action as
4057 * a) For a persistent reservation of the type Write Exclusive
4058 * - All Registrants or Exclusive Access  All Regitrants,
4059 * the reservation key shall be set to zero; or
4060 * b) For all other persistent reservation types, the
4061 * reservation key shall be set to the registered
4062 * reservation key for the I_T nexus that holds the
4063 * persistent reservation.
4065 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4066 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4069 pr_res_key = pr_reg->pr_res_key;
4071 buf[8] = ((pr_res_key >> 56) & 0xff);
4072 buf[9] = ((pr_res_key >> 48) & 0xff);
4073 buf[10] = ((pr_res_key >> 40) & 0xff);
4074 buf[11] = ((pr_res_key >> 32) & 0xff);
4075 buf[12] = ((pr_res_key >> 24) & 0xff);
4076 buf[13] = ((pr_res_key >> 16) & 0xff);
4077 buf[14] = ((pr_res_key >> 8) & 0xff);
4078 buf[15] = (pr_res_key & 0xff);
4080 * Set the SCOPE and TYPE
4082 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4083 (pr_reg->pr_res_type & 0x0f);
4087 spin_unlock(&dev->dev_reservation_lock);
4088 transport_kunmap_data_sg(cmd);
4094 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4096 * See spc4r17 section 6.13.4 Table 165
4098 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4100 struct se_device *dev = cmd->se_dev;
4101 struct t10_reservation *pr_tmpl = &dev->t10_pr;
4103 u16 add_len = 8; /* Hardcoded to 8. */
4105 if (cmd->data_length < 6) {
4106 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4107 " %u too small\n", cmd->data_length);
4108 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4112 buf = transport_kmap_data_sg(cmd);
4114 buf[0] = ((add_len << 8) & 0xff);
4115 buf[1] = (add_len & 0xff);
4116 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4117 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4118 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4119 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4121 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4122 * set the TMV: Task Mask Valid bit.
4126 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4128 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4130 * PTPL_A: Persistence across Target Power Loss Active bit
4132 if (pr_tmpl->pr_aptpl_active)
4135 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4137 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4138 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4139 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4140 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4141 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4142 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4144 transport_kunmap_data_sg(cmd);
4150 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4152 * See spc4r17 section 6.13.5 Table 168 and 169
4154 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4156 struct se_device *dev = cmd->se_dev;
4157 struct se_node_acl *se_nacl;
4158 struct se_portal_group *se_tpg;
4159 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4160 struct t10_reservation *pr_tmpl = &dev->t10_pr;
4162 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4163 u32 off = 8; /* off into first Full Status descriptor */
4164 int format_code = 0;
4166 if (cmd->data_length < 8) {
4167 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4168 " too small\n", cmd->data_length);
4169 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4173 buf = transport_kmap_data_sg(cmd);
4175 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
4176 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
4177 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
4178 buf[3] = (dev->t10_pr.pr_generation & 0xff);
4180 spin_lock(&pr_tmpl->registration_lock);
4181 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4182 &pr_tmpl->registration_list, pr_reg_list) {
4184 se_nacl = pr_reg->pr_reg_nacl;
4185 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4188 atomic_inc(&pr_reg->pr_res_holders);
4189 smp_mb__after_atomic_inc();
4190 spin_unlock(&pr_tmpl->registration_lock);
4192 * Determine expected length of $FABRIC_MOD specific
4193 * TransportID full status descriptor..
4195 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4196 se_tpg, se_nacl, pr_reg, &format_code);
4198 if ((exp_desc_len + add_len) > cmd->data_length) {
4199 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4200 " out of buffer: %d\n", cmd->data_length);
4201 spin_lock(&pr_tmpl->registration_lock);
4202 atomic_dec(&pr_reg->pr_res_holders);
4203 smp_mb__after_atomic_dec();
4207 * Set RESERVATION KEY
4209 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4210 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4211 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4212 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4213 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4214 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4215 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4216 buf[off++] = (pr_reg->pr_res_key & 0xff);
4217 off += 4; /* Skip Over Reserved area */
4220 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4222 if (pr_reg->pr_reg_all_tg_pt)
4225 * The struct se_lun pointer will be present for the
4226 * reservation holder for PR_HOLDER bit.
4228 * Also, if this registration is the reservation
4229 * holder, fill in SCOPE and TYPE in the next byte.
4231 if (pr_reg->pr_res_holder) {
4233 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4234 (pr_reg->pr_res_type & 0x0f);
4238 off += 4; /* Skip over reserved area */
4240 * From spc4r17 6.3.15:
4242 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4243 * IDENTIFIER field contains the relative port identifier (see
4244 * 3.1.120) of the target port that is part of the I_T nexus
4245 * described by this full status descriptor. If the ALL_TG_PT
4246 * bit is set to one, the contents of the RELATIVE TARGET PORT
4247 * IDENTIFIER field are not defined by this standard.
4249 if (!pr_reg->pr_reg_all_tg_pt) {
4250 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4252 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4253 buf[off++] = (port->sep_rtpi & 0xff);
4255 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4258 * Now, have the $FABRIC_MOD fill in the protocol identifier
4260 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4261 se_nacl, pr_reg, &format_code, &buf[off+4]);
4263 spin_lock(&pr_tmpl->registration_lock);
4264 atomic_dec(&pr_reg->pr_res_holders);
4265 smp_mb__after_atomic_dec();
4267 * Set the ADDITIONAL DESCRIPTOR LENGTH
4269 buf[off++] = ((desc_len >> 24) & 0xff);
4270 buf[off++] = ((desc_len >> 16) & 0xff);
4271 buf[off++] = ((desc_len >> 8) & 0xff);
4272 buf[off++] = (desc_len & 0xff);
4274 * Size of full desctipor header minus TransportID
4275 * containing $FABRIC_MOD specific) initiator device/port
4278 * See spc4r17 Section 6.13.5 Table 169
4280 add_desc_len = (24 + desc_len);
4283 add_len += add_desc_len;
4285 spin_unlock(&pr_tmpl->registration_lock);
4287 * Set ADDITIONAL_LENGTH
4289 buf[4] = ((add_len >> 24) & 0xff);
4290 buf[5] = ((add_len >> 16) & 0xff);
4291 buf[6] = ((add_len >> 8) & 0xff);
4292 buf[7] = (add_len & 0xff);
4294 transport_kunmap_data_sg(cmd);
4299 int target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4304 * Following spc2r20 5.5.1 Reservations overview:
4306 * If a logical unit has been reserved by any RESERVE command and is
4307 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4308 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4309 * initiator or service action and shall terminate with a RESERVATION
4312 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4313 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4314 " SPC-2 reservation is held, returning"
4315 " RESERVATION_CONFLICT\n");
4316 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4320 switch (cmd->t_task_cdb[1] & 0x1f) {
4322 ret = core_scsi3_pri_read_keys(cmd);
4324 case PRI_READ_RESERVATION:
4325 ret = core_scsi3_pri_read_reservation(cmd);
4327 case PRI_REPORT_CAPABILITIES:
4328 ret = core_scsi3_pri_report_capabilities(cmd);
4330 case PRI_READ_FULL_STATUS:
4331 ret = core_scsi3_pri_read_full_status(cmd);
4334 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4335 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4336 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4342 target_complete_cmd(cmd, GOOD);
4346 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4351 static int core_pt_seq_non_holder(
4359 void core_setup_reservations(struct se_device *dev)
4361 struct t10_reservation *rest = &dev->t10_pr;
4364 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4365 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4366 * cause a problem because libata and some SATA RAID HBAs appear
4367 * under Linux/SCSI, but to emulate reservations themselves.
4369 if ((dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) ||
4370 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV &&
4371 !dev->dev_attrib.emulate_reservations)) {
4372 rest->res_type = SPC_PASSTHROUGH;
4373 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4374 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4375 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4376 " emulation\n", dev->transport->name);
4377 } else if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4378 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4379 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4380 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4381 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4382 " emulation\n", dev->transport->name);
4384 rest->res_type = SPC2_RESERVATIONS;
4385 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4386 rest->pr_ops.t10_seq_non_holder =
4387 &core_scsi2_reservation_seq_non_holder;
4388 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4389 dev->transport->name);