1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@kernel.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ******************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_pr.h"
42 #include "target_core_ua.h"
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 struct pr_transport_id_holder {
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
56 int core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
61 if (!pr_reg->isid_present_at_reg)
64 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
68 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69 struct t10_pr_registration *, int);
71 static int core_scsi2_reservation_seq_non_holder(
88 static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
90 struct se_device *dev = cmd->se_dev;
91 struct se_session *sess = cmd->se_sess;
97 spin_lock(&dev->dev_reservation_lock);
98 if (!dev->dev_reserved_node_acl || !sess) {
99 spin_unlock(&dev->dev_reservation_lock);
102 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103 spin_unlock(&dev->dev_reservation_lock);
106 if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
107 spin_unlock(&dev->dev_reservation_lock);
110 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
111 spin_unlock(&dev->dev_reservation_lock);
116 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117 struct se_node_acl *, struct se_session *);
118 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
120 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd, int *ret)
122 struct se_session *se_sess = cmd->se_sess;
123 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
124 struct t10_pr_registration *pr_reg;
125 struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
126 int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
132 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
156 if (pr_reg->pr_res_holder) {
157 core_scsi3_put_pr_reg(pr_reg);
161 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
162 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
163 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
164 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
165 core_scsi3_put_pr_reg(pr_reg);
169 core_scsi3_put_pr_reg(pr_reg);
173 * Following spc2r20 5.5.1 Reservations overview:
175 * If a logical unit has executed a PERSISTENT RESERVE OUT
176 * command with the REGISTER or the REGISTER AND IGNORE
177 * EXISTING KEY service action and is still registered by any
178 * initiator, all RESERVE commands and all RELEASE commands
179 * regardless of initiator shall conflict and shall terminate
180 * with a RESERVATION CONFLICT status.
182 spin_lock(&pr_tmpl->registration_lock);
183 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
184 spin_unlock(&pr_tmpl->registration_lock);
188 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
189 " while active SPC-3 registrations exist,"
190 " returning RESERVATION_CONFLICT\n");
191 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
198 int target_scsi2_reservation_release(struct se_task *task)
200 struct se_cmd *cmd = task->task_se_cmd;
201 struct se_device *dev = cmd->se_dev;
202 struct se_session *sess = cmd->se_sess;
203 struct se_portal_group *tpg = sess->se_tpg;
208 if (target_check_scsi2_reservation_conflict(cmd, &ret))
212 spin_lock(&dev->dev_reservation_lock);
213 if (!dev->dev_reserved_node_acl || !sess)
216 if (dev->dev_reserved_node_acl != sess->se_node_acl)
219 dev->dev_reserved_node_acl = NULL;
220 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
221 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
222 dev->dev_res_bin_isid = 0;
223 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
225 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
226 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
227 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
228 sess->se_node_acl->initiatorname);
231 spin_unlock(&dev->dev_reservation_lock);
234 task->task_scsi_status = GOOD;
235 transport_complete_task(task, 1);
240 int target_scsi2_reservation_reserve(struct se_task *task)
242 struct se_cmd *cmd = task->task_se_cmd;
243 struct se_device *dev = cmd->se_dev;
244 struct se_session *sess = cmd->se_sess;
245 struct se_portal_group *tpg = sess->se_tpg;
248 if ((cmd->t_task_cdb[1] & 0x01) &&
249 (cmd->t_task_cdb[1] & 0x02)) {
250 pr_err("LongIO and Obselete Bits set, returning"
251 " ILLEGAL_REQUEST\n");
252 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
257 * This is currently the case for target_core_mod passthrough struct se_cmd
262 if (target_check_scsi2_reservation_conflict(cmd, &ret))
266 spin_lock(&dev->dev_reservation_lock);
267 if (dev->dev_reserved_node_acl &&
268 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
269 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
270 tpg->se_tpg_tfo->get_fabric_name());
271 pr_err("Original reserver LUN: %u %s\n",
272 cmd->se_lun->unpacked_lun,
273 dev->dev_reserved_node_acl->initiatorname);
274 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
275 " from %s \n", cmd->se_lun->unpacked_lun,
276 cmd->se_deve->mapped_lun,
277 sess->se_node_acl->initiatorname);
278 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
283 dev->dev_reserved_node_acl = sess->se_node_acl;
284 dev->dev_flags |= DF_SPC2_RESERVATIONS;
285 if (sess->sess_bin_isid != 0) {
286 dev->dev_res_bin_isid = sess->sess_bin_isid;
287 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
289 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
290 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
291 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
292 sess->se_node_acl->initiatorname);
295 spin_unlock(&dev->dev_reservation_lock);
298 task->task_scsi_status = GOOD;
299 transport_complete_task(task, 1);
306 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
308 * This function is called by those initiator ports who are *NOT*
309 * the active PR reservation holder when a reservation is present.
311 static int core_scsi3_pr_seq_non_holder(
316 struct se_dev_entry *se_deve;
317 struct se_session *se_sess = cmd->se_sess;
318 int other_cdb = 0, ignore_reg;
319 int registered_nexus = 0, ret = 1; /* Conflict by default */
320 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
321 int we = 0; /* Write Exclusive */
322 int legacy = 0; /* Act like a legacy device and return
323 * RESERVATION CONFLICT on some CDBs */
325 * A legacy SPC-2 reservation is being held.
327 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
328 return core_scsi2_reservation_seq_non_holder(cmd,
331 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
333 * Determine if the registration should be ignored due to
334 * non-matching ISIDs in core_scsi3_pr_reservation_check().
336 ignore_reg = (pr_reg_type & 0x80000000);
338 pr_reg_type &= ~0x80000000;
340 switch (pr_reg_type) {
341 case PR_TYPE_WRITE_EXCLUSIVE:
343 case PR_TYPE_EXCLUSIVE_ACCESS:
345 * Some commands are only allowed for the persistent reservation
348 if ((se_deve->def_pr_registered) && !(ignore_reg))
349 registered_nexus = 1;
351 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
353 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
355 * Some commands are only allowed for registered I_T Nexuses.
358 if ((se_deve->def_pr_registered) && !(ignore_reg))
359 registered_nexus = 1;
361 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
363 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
365 * Each registered I_T Nexus is a reservation holder.
368 if ((se_deve->def_pr_registered) && !(ignore_reg))
369 registered_nexus = 1;
375 * Referenced from spc4r17 table 45 for *NON* PR holder access
378 case SECURITY_PROTOCOL_IN:
379 if (registered_nexus)
387 case RECEIVE_DIAGNOSTIC:
392 if (registered_nexus) {
396 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
398 case PERSISTENT_RESERVE_OUT:
400 * This follows PERSISTENT_RESERVE_OUT service actions that
401 * are allowed in the presence of various reservations.
402 * See spc4r17, table 46
404 switch (cdb[1] & 0x1f) {
407 case PRO_PREEMPT_AND_ABORT:
408 ret = (registered_nexus) ? 0 : 1;
411 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
414 case PRO_REGISTER_AND_MOVE:
419 ret = (registered_nexus) ? 0 : 1;
422 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
423 " action: 0x%02x\n", cdb[1] & 0x1f);
429 /* Handled by CRH=1 in target_scsi2_reservation_release() */
434 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
437 case TEST_UNIT_READY:
438 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
441 switch (cdb[1] & 0x1f) {
442 case MI_MANAGEMENT_PROTOCOL_IN:
443 if (registered_nexus) {
447 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
449 case MI_REPORT_SUPPORTED_OPERATION_CODES:
450 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
455 if (registered_nexus) {
459 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
461 case MI_REPORT_ALIASES:
462 case MI_REPORT_IDENTIFYING_INFORMATION:
463 case MI_REPORT_PRIORITY:
464 case MI_REPORT_TARGET_PGS:
465 case MI_REPORT_TIMESTAMP:
466 ret = 0; /* Allowed */
469 pr_err("Unknown MI Service Action: 0x%02x\n",
474 case ACCESS_CONTROL_IN:
475 case ACCESS_CONTROL_OUT:
478 case READ_MEDIA_SERIAL_NUMBER:
481 case PERSISTENT_RESERVE_IN:
482 ret = 0; /*/ Allowed CDBs */
489 * Case where the CDB is explicitly allowed in the above switch
492 if (!ret && !other_cdb) {
494 pr_debug("Allowing explict CDB: 0x%02x for %s"
495 " reservation holder\n", cdb[0],
496 core_scsi3_pr_dump_type(pr_reg_type));
501 * Check if write exclusive initiator ports *NOT* holding the
502 * WRITE_EXCLUSIVE_* reservation.
504 if ((we) && !(registered_nexus)) {
505 if (cmd->data_direction == DMA_TO_DEVICE) {
507 * Conflict for write exclusive
509 pr_debug("%s Conflict for unregistered nexus"
510 " %s CDB: 0x%02x to %s reservation\n",
511 transport_dump_cmd_direction(cmd),
512 se_sess->se_node_acl->initiatorname, cdb[0],
513 core_scsi3_pr_dump_type(pr_reg_type));
517 * Allow non WRITE CDBs for all Write Exclusive
518 * PR TYPEs to pass for registered and
519 * non-registered_nexuxes NOT holding the reservation.
521 * We only make noise for the unregisterd nexuses,
522 * as we expect registered non-reservation holding
523 * nexuses to issue CDBs.
526 if (!registered_nexus) {
527 pr_debug("Allowing implict CDB: 0x%02x"
528 " for %s reservation on unregistered"
530 core_scsi3_pr_dump_type(pr_reg_type));
535 } else if ((reg_only) || (all_reg)) {
536 if (registered_nexus) {
538 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
539 * allow commands from registered nexuses.
542 pr_debug("Allowing implict CDB: 0x%02x for %s"
543 " reservation\n", cdb[0],
544 core_scsi3_pr_dump_type(pr_reg_type));
549 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
550 " for %s reservation\n", transport_dump_cmd_direction(cmd),
551 (registered_nexus) ? "" : "un",
552 se_sess->se_node_acl->initiatorname, cdb[0],
553 core_scsi3_pr_dump_type(pr_reg_type));
555 return 1; /* Conflict by default */
558 static u32 core_scsi3_pr_generation(struct se_device *dev)
560 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
563 * PRGeneration field shall contain the value of a 32-bit wrapping
564 * counter mainted by the device server.
566 * Note that this is done regardless of Active Persist across
567 * Target PowerLoss (APTPL)
569 * See spc4r17 section 6.3.12 READ_KEYS service action
571 spin_lock(&dev->dev_reservation_lock);
572 prg = su_dev->t10_pr.pr_generation++;
573 spin_unlock(&dev->dev_reservation_lock);
578 static int core_scsi3_pr_reservation_check(
582 struct se_device *dev = cmd->se_dev;
583 struct se_session *sess = cmd->se_sess;
589 * A legacy SPC-2 reservation is being held.
591 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
592 return core_scsi2_reservation_check(cmd, pr_reg_type);
594 spin_lock(&dev->dev_reservation_lock);
595 if (!dev->dev_pr_res_holder) {
596 spin_unlock(&dev->dev_reservation_lock);
599 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
600 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
601 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
602 spin_unlock(&dev->dev_reservation_lock);
605 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
606 spin_unlock(&dev->dev_reservation_lock);
609 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
610 sess->sess_bin_isid) ? 0 : -EINVAL;
612 * Use bit in *pr_reg_type to notify ISID mismatch in
613 * core_scsi3_pr_seq_non_holder().
616 *pr_reg_type |= 0x80000000;
617 spin_unlock(&dev->dev_reservation_lock);
622 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
623 struct se_device *dev,
624 struct se_node_acl *nacl,
625 struct se_dev_entry *deve,
631 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
632 struct t10_pr_registration *pr_reg;
634 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
636 pr_err("Unable to allocate struct t10_pr_registration\n");
640 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
642 if (!pr_reg->pr_aptpl_buf) {
643 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
644 kmem_cache_free(t10_pr_reg_cache, pr_reg);
648 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
649 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
650 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
651 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
652 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
653 atomic_set(&pr_reg->pr_res_holders, 0);
654 pr_reg->pr_reg_nacl = nacl;
655 pr_reg->pr_reg_deve = deve;
656 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
657 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
658 pr_reg->pr_res_key = sa_res_key;
659 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
660 pr_reg->pr_reg_aptpl = aptpl;
661 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
663 * If an ISID value for this SCSI Initiator Port exists,
664 * save it to the registration now.
667 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
668 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
669 pr_reg->isid_present_at_reg = 1;
675 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
676 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
679 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
682 static struct t10_pr_registration *__core_scsi3_alloc_registration(
683 struct se_device *dev,
684 struct se_node_acl *nacl,
685 struct se_dev_entry *deve,
691 struct se_dev_entry *deve_tmp;
692 struct se_node_acl *nacl_tmp;
693 struct se_port *port, *port_tmp;
694 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
695 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
698 * Create a registration for the I_T Nexus upon which the
699 * PROUT REGISTER was received.
701 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
702 sa_res_key, all_tg_pt, aptpl);
706 * Return pointer to pr_reg for ALL_TG_PT=0
711 * Create list of matching SCSI Initiator Port registrations
714 spin_lock(&dev->se_port_lock);
715 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
716 atomic_inc(&port->sep_tg_pt_ref_cnt);
717 smp_mb__after_atomic_inc();
718 spin_unlock(&dev->se_port_lock);
720 spin_lock_bh(&port->sep_alua_lock);
721 list_for_each_entry(deve_tmp, &port->sep_alua_list,
724 * This pointer will be NULL for demo mode MappedLUNs
725 * that have not been make explict via a ConfigFS
726 * MappedLUN group for the SCSI Initiator Node ACL.
728 if (!deve_tmp->se_lun_acl)
731 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
733 * Skip the matching struct se_node_acl that is allocated
736 if (nacl == nacl_tmp)
739 * Only perform PR registrations for target ports on
740 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
743 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
746 * Look for a matching Initiator Node ACL in ASCII format
748 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
751 atomic_inc(&deve_tmp->pr_ref_count);
752 smp_mb__after_atomic_inc();
753 spin_unlock_bh(&port->sep_alua_lock);
755 * Grab a configfs group dependency that is released
756 * for the exception path at label out: below, or upon
757 * completion of adding ALL_TG_PT=1 registrations in
758 * __core_scsi3_add_registration()
760 ret = core_scsi3_lunacl_depend_item(deve_tmp);
762 pr_err("core_scsi3_lunacl_depend"
764 atomic_dec(&port->sep_tg_pt_ref_cnt);
765 smp_mb__after_atomic_dec();
766 atomic_dec(&deve_tmp->pr_ref_count);
767 smp_mb__after_atomic_dec();
771 * Located a matching SCSI Initiator Port on a different
772 * port, allocate the pr_reg_atp and attach it to the
773 * pr_reg->pr_reg_atp_list that will be processed once
774 * the original *pr_reg is processed in
775 * __core_scsi3_add_registration()
777 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
778 nacl_tmp, deve_tmp, NULL,
779 sa_res_key, all_tg_pt, aptpl);
781 atomic_dec(&port->sep_tg_pt_ref_cnt);
782 smp_mb__after_atomic_dec();
783 atomic_dec(&deve_tmp->pr_ref_count);
784 smp_mb__after_atomic_dec();
785 core_scsi3_lunacl_undepend_item(deve_tmp);
789 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
790 &pr_reg->pr_reg_atp_list);
791 spin_lock_bh(&port->sep_alua_lock);
793 spin_unlock_bh(&port->sep_alua_lock);
795 spin_lock(&dev->se_port_lock);
796 atomic_dec(&port->sep_tg_pt_ref_cnt);
797 smp_mb__after_atomic_dec();
799 spin_unlock(&dev->se_port_lock);
803 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
804 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
805 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
806 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
807 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
809 kmem_cache_free(t10_pr_reg_cache, pr_reg);
813 int core_scsi3_alloc_aptpl_registration(
814 struct t10_reservation *pr_tmpl,
816 unsigned char *i_port,
819 unsigned char *t_port,
826 struct t10_pr_registration *pr_reg;
828 if (!i_port || !t_port || !sa_res_key) {
829 pr_err("Illegal parameters for APTPL registration\n");
833 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
835 pr_err("Unable to allocate struct t10_pr_registration\n");
838 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
840 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
841 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
842 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
843 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
844 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
845 atomic_set(&pr_reg->pr_res_holders, 0);
846 pr_reg->pr_reg_nacl = NULL;
847 pr_reg->pr_reg_deve = NULL;
848 pr_reg->pr_res_mapped_lun = mapped_lun;
849 pr_reg->pr_aptpl_target_lun = target_lun;
850 pr_reg->pr_res_key = sa_res_key;
851 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
852 pr_reg->pr_reg_aptpl = 1;
853 pr_reg->pr_reg_tg_pt_lun = NULL;
854 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
855 pr_reg->pr_res_type = type;
857 * If an ISID value had been saved in APTPL metadata for this
858 * SCSI Initiator Port, restore it now.
861 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
862 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
863 pr_reg->isid_present_at_reg = 1;
866 * Copy the i_port and t_port information from caller.
868 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
869 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
870 pr_reg->pr_reg_tpgt = tpgt;
872 * Set pr_res_holder from caller, the pr_reg who is the reservation
873 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
874 * the Initiator Node LUN ACL from the fabric module is created for
877 pr_reg->pr_res_holder = res_holder;
879 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
880 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
881 " metadata\n", (res_holder) ? "+reservation" : "");
885 static void core_scsi3_aptpl_reserve(
886 struct se_device *dev,
887 struct se_portal_group *tpg,
888 struct se_node_acl *node_acl,
889 struct t10_pr_registration *pr_reg)
891 char i_buf[PR_REG_ISID_ID_LEN];
894 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
895 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
898 spin_lock(&dev->dev_reservation_lock);
899 dev->dev_pr_res_holder = pr_reg;
900 spin_unlock(&dev->dev_reservation_lock);
902 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
903 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
904 tpg->se_tpg_tfo->get_fabric_name(),
905 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
906 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
907 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
908 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
909 (prf_isid) ? &i_buf[0] : "");
912 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
913 struct t10_pr_registration *, int, int);
915 static int __core_scsi3_check_aptpl_registration(
916 struct se_device *dev,
917 struct se_portal_group *tpg,
920 struct se_node_acl *nacl,
921 struct se_dev_entry *deve)
923 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
924 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
925 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
926 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
929 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
930 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
932 * Copy Initiator Port information from struct se_node_acl
934 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
935 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
936 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
937 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
939 * Look for the matching registrations+reservation from those
940 * created from APTPL metadata. Note that multiple registrations
941 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
944 spin_lock(&pr_tmpl->aptpl_reg_lock);
945 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
947 if (!strcmp(pr_reg->pr_iport, i_port) &&
948 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
949 !(strcmp(pr_reg->pr_tport, t_port)) &&
950 (pr_reg->pr_reg_tpgt == tpgt) &&
951 (pr_reg->pr_aptpl_target_lun == target_lun)) {
953 pr_reg->pr_reg_nacl = nacl;
954 pr_reg->pr_reg_deve = deve;
955 pr_reg->pr_reg_tg_pt_lun = lun;
957 list_del(&pr_reg->pr_reg_aptpl_list);
958 spin_unlock(&pr_tmpl->aptpl_reg_lock);
960 * At this point all of the pointers in *pr_reg will
961 * be setup, so go ahead and add the registration.
964 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
966 * If this registration is the reservation holder,
967 * make that happen now..
969 if (pr_reg->pr_res_holder)
970 core_scsi3_aptpl_reserve(dev, tpg,
973 * Reenable pr_aptpl_active to accept new metadata
974 * updates once the SCSI device is active again..
976 spin_lock(&pr_tmpl->aptpl_reg_lock);
977 pr_tmpl->pr_aptpl_active = 1;
980 spin_unlock(&pr_tmpl->aptpl_reg_lock);
985 int core_scsi3_check_aptpl_registration(
986 struct se_device *dev,
987 struct se_portal_group *tpg,
989 struct se_lun_acl *lun_acl)
991 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
992 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
993 struct se_dev_entry *deve = &nacl->device_list[lun_acl->mapped_lun];
995 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
998 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
999 lun->unpacked_lun, nacl, deve);
1002 static void __core_scsi3_dump_registration(
1003 struct target_core_fabric_ops *tfo,
1004 struct se_device *dev,
1005 struct se_node_acl *nacl,
1006 struct t10_pr_registration *pr_reg,
1009 struct se_portal_group *se_tpg = nacl->se_tpg;
1010 char i_buf[PR_REG_ISID_ID_LEN];
1013 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1014 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1015 PR_REG_ISID_ID_LEN);
1017 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1018 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1019 "_AND_MOVE" : (register_type == 1) ?
1020 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1021 (prf_isid) ? i_buf : "");
1022 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1023 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1024 tfo->tpg_get_tag(se_tpg));
1025 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1026 " Port(s)\n", tfo->get_fabric_name(),
1027 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1028 dev->transport->name);
1029 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1030 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1031 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1032 pr_reg->pr_reg_aptpl);
1036 * this function can be called with struct se_device->dev_reservation_lock
1037 * when register_move = 1
1039 static void __core_scsi3_add_registration(
1040 struct se_device *dev,
1041 struct se_node_acl *nacl,
1042 struct t10_pr_registration *pr_reg,
1046 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1047 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1048 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1049 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1052 * Increment PRgeneration counter for struct se_device upon a successful
1053 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1055 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1056 * action, the struct se_device->dev_reservation_lock will already be held,
1057 * so we do not call core_scsi3_pr_generation() which grabs the lock
1060 pr_reg->pr_res_generation = (register_move) ?
1061 su_dev->t10_pr.pr_generation++ :
1062 core_scsi3_pr_generation(dev);
1064 spin_lock(&pr_tmpl->registration_lock);
1065 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1066 pr_reg->pr_reg_deve->def_pr_registered = 1;
1068 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1069 spin_unlock(&pr_tmpl->registration_lock);
1071 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1073 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1076 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1077 * allocated in __core_scsi3_alloc_registration()
1079 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1080 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1081 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1083 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1085 spin_lock(&pr_tmpl->registration_lock);
1086 list_add_tail(&pr_reg_tmp->pr_reg_list,
1087 &pr_tmpl->registration_list);
1088 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1090 __core_scsi3_dump_registration(tfo, dev,
1091 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1093 spin_unlock(&pr_tmpl->registration_lock);
1095 * Drop configfs group dependency reference from
1096 * __core_scsi3_alloc_registration()
1098 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1102 static int core_scsi3_alloc_registration(
1103 struct se_device *dev,
1104 struct se_node_acl *nacl,
1105 struct se_dev_entry *deve,
1106 unsigned char *isid,
1113 struct t10_pr_registration *pr_reg;
1115 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1116 sa_res_key, all_tg_pt, aptpl);
1120 __core_scsi3_add_registration(dev, nacl, pr_reg,
1121 register_type, register_move);
1125 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1126 struct se_device *dev,
1127 struct se_node_acl *nacl,
1128 unsigned char *isid)
1130 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1131 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1132 struct se_portal_group *tpg;
1134 spin_lock(&pr_tmpl->registration_lock);
1135 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1136 &pr_tmpl->registration_list, pr_reg_list) {
1138 * First look for a matching struct se_node_acl
1140 if (pr_reg->pr_reg_nacl != nacl)
1143 tpg = pr_reg->pr_reg_nacl->se_tpg;
1145 * If this registration does NOT contain a fabric provided
1146 * ISID, then we have found a match.
1148 if (!pr_reg->isid_present_at_reg) {
1150 * Determine if this SCSI device server requires that
1151 * SCSI Intiatior TransportID w/ ISIDs is enforced
1152 * for fabric modules (iSCSI) requiring them.
1154 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1155 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
1158 atomic_inc(&pr_reg->pr_res_holders);
1159 smp_mb__after_atomic_inc();
1160 spin_unlock(&pr_tmpl->registration_lock);
1164 * If the *pr_reg contains a fabric defined ISID for multi-value
1165 * SCSI Initiator Port TransportIDs, then we expect a valid
1166 * matching ISID to be provided by the local SCSI Initiator Port.
1170 if (strcmp(isid, pr_reg->pr_reg_isid))
1173 atomic_inc(&pr_reg->pr_res_holders);
1174 smp_mb__after_atomic_inc();
1175 spin_unlock(&pr_tmpl->registration_lock);
1178 spin_unlock(&pr_tmpl->registration_lock);
1183 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1184 struct se_device *dev,
1185 struct se_node_acl *nacl,
1186 struct se_session *sess)
1188 struct se_portal_group *tpg = nacl->se_tpg;
1189 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1191 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1192 memset(&buf[0], 0, PR_REG_ISID_LEN);
1193 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1198 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1201 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1203 atomic_dec(&pr_reg->pr_res_holders);
1204 smp_mb__after_atomic_dec();
1207 static int core_scsi3_check_implict_release(
1208 struct se_device *dev,
1209 struct t10_pr_registration *pr_reg)
1211 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1212 struct t10_pr_registration *pr_res_holder;
1215 spin_lock(&dev->dev_reservation_lock);
1216 pr_res_holder = dev->dev_pr_res_holder;
1217 if (!pr_res_holder) {
1218 spin_unlock(&dev->dev_reservation_lock);
1221 if (pr_res_holder == pr_reg) {
1223 * Perform an implict RELEASE if the registration that
1224 * is being released is holding the reservation.
1226 * From spc4r17, section 5.7.11.1:
1228 * e) If the I_T nexus is the persistent reservation holder
1229 * and the persistent reservation is not an all registrants
1230 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1231 * service action or REGISTER AND IGNORE EXISTING KEY
1232 * service action with the SERVICE ACTION RESERVATION KEY
1233 * field set to zero (see 5.7.11.3).
1235 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1238 * For 'All Registrants' reservation types, all existing
1239 * registrations are still processed as reservation holders
1240 * in core_scsi3_pr_seq_non_holder() after the initial
1241 * reservation holder is implictly released here.
1243 } else if (pr_reg->pr_reg_all_tg_pt &&
1244 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1245 pr_reg->pr_reg_nacl->initiatorname)) &&
1246 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1247 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1248 " UNREGISTER while existing reservation with matching"
1249 " key 0x%016Lx is present from another SCSI Initiator"
1250 " Port\n", pr_reg->pr_res_key);
1253 spin_unlock(&dev->dev_reservation_lock);
1259 * Called with struct t10_reservation->registration_lock held.
1261 static void __core_scsi3_free_registration(
1262 struct se_device *dev,
1263 struct t10_pr_registration *pr_reg,
1264 struct list_head *preempt_and_abort_list,
1267 struct target_core_fabric_ops *tfo =
1268 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1269 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1270 char i_buf[PR_REG_ISID_ID_LEN];
1273 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1274 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1275 PR_REG_ISID_ID_LEN);
1277 pr_reg->pr_reg_deve->def_pr_registered = 0;
1278 pr_reg->pr_reg_deve->pr_res_key = 0;
1279 list_del(&pr_reg->pr_reg_list);
1281 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1282 * so call core_scsi3_put_pr_reg() to decrement our reference.
1285 core_scsi3_put_pr_reg(pr_reg);
1287 * Wait until all reference from any other I_T nexuses for this
1288 * *pr_reg have been released. Because list_del() is called above,
1289 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1290 * count back to zero, and we release *pr_reg.
1292 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1293 spin_unlock(&pr_tmpl->registration_lock);
1294 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1295 tfo->get_fabric_name());
1297 spin_lock(&pr_tmpl->registration_lock);
1300 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1301 " Node: %s%s\n", tfo->get_fabric_name(),
1302 pr_reg->pr_reg_nacl->initiatorname,
1303 (prf_isid) ? &i_buf[0] : "");
1304 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1305 " Port(s)\n", tfo->get_fabric_name(),
1306 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1307 dev->transport->name);
1308 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1309 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1310 pr_reg->pr_res_generation);
1312 if (!preempt_and_abort_list) {
1313 pr_reg->pr_reg_deve = NULL;
1314 pr_reg->pr_reg_nacl = NULL;
1315 kfree(pr_reg->pr_aptpl_buf);
1316 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1320 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1321 * are released once the ABORT_TASK_SET has completed..
1323 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1326 void core_scsi3_free_pr_reg_from_nacl(
1327 struct se_device *dev,
1328 struct se_node_acl *nacl)
1330 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1331 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1333 * If the passed se_node_acl matches the reservation holder,
1334 * release the reservation.
1336 spin_lock(&dev->dev_reservation_lock);
1337 pr_res_holder = dev->dev_pr_res_holder;
1338 if ((pr_res_holder != NULL) &&
1339 (pr_res_holder->pr_reg_nacl == nacl))
1340 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1341 spin_unlock(&dev->dev_reservation_lock);
1343 * Release any registration associated with the struct se_node_acl.
1345 spin_lock(&pr_tmpl->registration_lock);
1346 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1347 &pr_tmpl->registration_list, pr_reg_list) {
1349 if (pr_reg->pr_reg_nacl != nacl)
1352 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1354 spin_unlock(&pr_tmpl->registration_lock);
1357 void core_scsi3_free_all_registrations(
1358 struct se_device *dev)
1360 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1361 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1363 spin_lock(&dev->dev_reservation_lock);
1364 pr_res_holder = dev->dev_pr_res_holder;
1365 if (pr_res_holder != NULL) {
1366 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1367 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1370 spin_unlock(&dev->dev_reservation_lock);
1372 spin_lock(&pr_tmpl->registration_lock);
1373 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1374 &pr_tmpl->registration_list, pr_reg_list) {
1376 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1378 spin_unlock(&pr_tmpl->registration_lock);
1380 spin_lock(&pr_tmpl->aptpl_reg_lock);
1381 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1382 pr_reg_aptpl_list) {
1383 list_del(&pr_reg->pr_reg_aptpl_list);
1384 kfree(pr_reg->pr_aptpl_buf);
1385 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1387 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1390 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1392 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1393 &tpg->tpg_group.cg_item);
1396 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1398 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1399 &tpg->tpg_group.cg_item);
1401 atomic_dec(&tpg->tpg_pr_ref_count);
1402 smp_mb__after_atomic_dec();
1405 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1407 struct se_portal_group *tpg = nacl->se_tpg;
1409 if (nacl->dynamic_node_acl)
1412 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1413 &nacl->acl_group.cg_item);
1416 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1418 struct se_portal_group *tpg = nacl->se_tpg;
1420 if (nacl->dynamic_node_acl) {
1421 atomic_dec(&nacl->acl_pr_ref_count);
1422 smp_mb__after_atomic_dec();
1426 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1427 &nacl->acl_group.cg_item);
1429 atomic_dec(&nacl->acl_pr_ref_count);
1430 smp_mb__after_atomic_dec();
1433 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1435 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1436 struct se_node_acl *nacl;
1437 struct se_portal_group *tpg;
1439 * For nacl->dynamic_node_acl=1
1444 nacl = lun_acl->se_lun_nacl;
1447 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1448 &lun_acl->se_lun_group.cg_item);
1451 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1453 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1454 struct se_node_acl *nacl;
1455 struct se_portal_group *tpg;
1457 * For nacl->dynamic_node_acl=1
1460 atomic_dec(&se_deve->pr_ref_count);
1461 smp_mb__after_atomic_dec();
1464 nacl = lun_acl->se_lun_nacl;
1467 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1468 &lun_acl->se_lun_group.cg_item);
1470 atomic_dec(&se_deve->pr_ref_count);
1471 smp_mb__after_atomic_dec();
1474 static int core_scsi3_decode_spec_i_port(
1476 struct se_portal_group *tpg,
1477 unsigned char *l_isid,
1482 struct se_device *dev = cmd->se_dev;
1483 struct se_port *tmp_port;
1484 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1485 struct se_session *se_sess = cmd->se_sess;
1486 struct se_node_acl *dest_node_acl = NULL;
1487 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1488 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1489 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1490 LIST_HEAD(tid_dest_list);
1491 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1492 struct target_core_fabric_ops *tmp_tf_ops;
1494 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1495 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1496 u32 tpdl, tid_len = 0;
1497 int ret, dest_local_nexus, prf_isid;
1500 memset(dest_iport, 0, 64);
1502 local_se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1504 * Allocate a struct pr_transport_id_holder and setup the
1505 * local_node_acl and local_se_deve pointers and add to
1506 * struct list_head tid_dest_list for add registration
1507 * processing in the loop of tid_dest_list below.
1509 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1511 pr_err("Unable to allocate tidh_new\n");
1512 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1515 INIT_LIST_HEAD(&tidh_new->dest_list);
1516 tidh_new->dest_tpg = tpg;
1517 tidh_new->dest_node_acl = se_sess->se_node_acl;
1518 tidh_new->dest_se_deve = local_se_deve;
1520 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1521 se_sess->se_node_acl, local_se_deve, l_isid,
1522 sa_res_key, all_tg_pt, aptpl);
1523 if (!local_pr_reg) {
1525 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1528 tidh_new->dest_pr_reg = local_pr_reg;
1530 * The local I_T nexus does not hold any configfs dependances,
1531 * so we set tid_h->dest_local_nexus=1 to prevent the
1532 * configfs_undepend_item() calls in the tid_dest_list loops below.
1534 tidh_new->dest_local_nexus = 1;
1535 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1537 buf = transport_kmap_data_sg(cmd);
1539 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1540 * first extract TransportID Parameter Data Length, and make sure
1541 * the value matches up to the SCSI expected data transfer length.
1543 tpdl = (buf[24] & 0xff) << 24;
1544 tpdl |= (buf[25] & 0xff) << 16;
1545 tpdl |= (buf[26] & 0xff) << 8;
1546 tpdl |= buf[27] & 0xff;
1548 if ((tpdl + 28) != cmd->data_length) {
1549 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1550 " does not equal CDB data_length: %u\n", tpdl,
1552 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1557 * Start processing the received transport IDs using the
1558 * receiving I_T Nexus portal's fabric dependent methods to
1559 * obtain the SCSI Initiator Port/Device Identifiers.
1564 proto_ident = (ptr[0] & 0x0f);
1567 spin_lock(&dev->se_port_lock);
1568 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1569 tmp_tpg = tmp_port->sep_tpg;
1572 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1575 if (!tmp_tf_ops->get_fabric_proto_ident ||
1576 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1579 * Look for the matching proto_ident provided by
1580 * the received TransportID
1582 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1583 if (tmp_proto_ident != proto_ident)
1585 dest_rtpi = tmp_port->sep_rtpi;
1587 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1588 tmp_tpg, (const char *)ptr, &tid_len,
1593 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1594 smp_mb__after_atomic_inc();
1595 spin_unlock(&dev->se_port_lock);
1597 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1599 pr_err(" core_scsi3_tpg_depend_item()"
1601 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1602 smp_mb__after_atomic_dec();
1603 cmd->scsi_sense_reason =
1604 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1609 * Locate the desination initiator ACL to be registered
1610 * from the decoded fabric module specific TransportID
1613 spin_lock_irq(&tmp_tpg->acl_node_lock);
1614 dest_node_acl = __core_tpg_get_initiator_node_acl(
1616 if (dest_node_acl) {
1617 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1618 smp_mb__after_atomic_inc();
1620 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1622 if (!dest_node_acl) {
1623 core_scsi3_tpg_undepend_item(tmp_tpg);
1624 spin_lock(&dev->se_port_lock);
1628 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1630 pr_err("configfs_depend_item() failed"
1631 " for dest_node_acl->acl_group\n");
1632 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1633 smp_mb__after_atomic_dec();
1634 core_scsi3_tpg_undepend_item(tmp_tpg);
1635 cmd->scsi_sense_reason =
1636 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1642 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1643 " %s Port RTPI: %hu\n",
1644 dest_tpg->se_tpg_tfo->get_fabric_name(),
1645 dest_node_acl->initiatorname, dest_rtpi);
1647 spin_lock(&dev->se_port_lock);
1650 spin_unlock(&dev->se_port_lock);
1653 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1655 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1660 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1661 " tid_len: %d for %s + %s\n",
1662 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1663 tpdl, tid_len, i_str, iport_ptr);
1665 if (tid_len > tpdl) {
1666 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1667 " %u for Transport ID: %s\n", tid_len, ptr);
1668 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1669 core_scsi3_tpg_undepend_item(dest_tpg);
1670 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1675 * Locate the desintation struct se_dev_entry pointer for matching
1676 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1679 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1681 if (!dest_se_deve) {
1682 pr_err("Unable to locate %s dest_se_deve"
1683 " from destination RTPI: %hu\n",
1684 dest_tpg->se_tpg_tfo->get_fabric_name(),
1687 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1688 core_scsi3_tpg_undepend_item(dest_tpg);
1689 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1694 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1696 pr_err("core_scsi3_lunacl_depend_item()"
1698 atomic_dec(&dest_se_deve->pr_ref_count);
1699 smp_mb__after_atomic_dec();
1700 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1701 core_scsi3_tpg_undepend_item(dest_tpg);
1702 cmd->scsi_sense_reason =
1703 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1708 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1709 " dest_se_deve mapped_lun: %u\n",
1710 dest_tpg->se_tpg_tfo->get_fabric_name(),
1711 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1714 * Skip any TransportIDs that already have a registration for
1717 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1720 core_scsi3_put_pr_reg(pr_reg_e);
1721 core_scsi3_lunacl_undepend_item(dest_se_deve);
1722 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1723 core_scsi3_tpg_undepend_item(dest_tpg);
1730 * Allocate a struct pr_transport_id_holder and setup
1731 * the dest_node_acl and dest_se_deve pointers for the
1734 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1737 pr_err("Unable to allocate tidh_new\n");
1738 core_scsi3_lunacl_undepend_item(dest_se_deve);
1739 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1740 core_scsi3_tpg_undepend_item(dest_tpg);
1741 cmd->scsi_sense_reason =
1742 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1746 INIT_LIST_HEAD(&tidh_new->dest_list);
1747 tidh_new->dest_tpg = dest_tpg;
1748 tidh_new->dest_node_acl = dest_node_acl;
1749 tidh_new->dest_se_deve = dest_se_deve;
1752 * Allocate, but do NOT add the registration for the
1753 * TransportID referenced SCSI Initiator port. This
1754 * done because of the following from spc4r17 in section
1755 * 6.14.3 wrt SPEC_I_PT:
1757 * "If a registration fails for any initiator port (e.g., if th
1758 * logical unit does not have enough resources available to
1759 * hold the registration information), no registrations shall be
1760 * made, and the command shall be terminated with
1761 * CHECK CONDITION status."
1763 * That means we call __core_scsi3_alloc_registration() here,
1764 * and then call __core_scsi3_add_registration() in the
1765 * 2nd loop which will never fail.
1767 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1768 dest_node_acl, dest_se_deve, iport_ptr,
1769 sa_res_key, all_tg_pt, aptpl);
1771 core_scsi3_lunacl_undepend_item(dest_se_deve);
1772 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1773 core_scsi3_tpg_undepend_item(dest_tpg);
1775 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1779 tidh_new->dest_pr_reg = dest_pr_reg;
1780 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1788 transport_kunmap_data_sg(cmd);
1791 * Go ahead and create a registrations from tid_dest_list for the
1792 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1795 * The SA Reservation Key from the PROUT is set for the
1796 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1797 * means that the TransportID Initiator port will be
1798 * registered on all of the target ports in the SCSI target device
1799 * ALL_TG_PT=0 means the registration will only be for the
1800 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1803 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1804 dest_tpg = tidh->dest_tpg;
1805 dest_node_acl = tidh->dest_node_acl;
1806 dest_se_deve = tidh->dest_se_deve;
1807 dest_pr_reg = tidh->dest_pr_reg;
1808 dest_local_nexus = tidh->dest_local_nexus;
1810 list_del(&tidh->dest_list);
1813 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1814 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1815 PR_REG_ISID_ID_LEN);
1817 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1820 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1821 " registered Transport ID for Node: %s%s Mapped LUN:"
1822 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1823 dest_node_acl->initiatorname, (prf_isid) ?
1824 &i_buf[0] : "", dest_se_deve->mapped_lun);
1826 if (dest_local_nexus)
1829 core_scsi3_lunacl_undepend_item(dest_se_deve);
1830 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1831 core_scsi3_tpg_undepend_item(dest_tpg);
1836 transport_kunmap_data_sg(cmd);
1838 * For the failure case, release everything from tid_dest_list
1839 * including *dest_pr_reg and the configfs dependances..
1841 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1842 dest_tpg = tidh->dest_tpg;
1843 dest_node_acl = tidh->dest_node_acl;
1844 dest_se_deve = tidh->dest_se_deve;
1845 dest_pr_reg = tidh->dest_pr_reg;
1846 dest_local_nexus = tidh->dest_local_nexus;
1848 list_del(&tidh->dest_list);
1851 * Release any extra ALL_TG_PT=1 registrations for
1852 * the SPEC_I_PT=1 case.
1854 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1855 &dest_pr_reg->pr_reg_atp_list,
1856 pr_reg_atp_mem_list) {
1857 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1858 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1859 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1862 kfree(dest_pr_reg->pr_aptpl_buf);
1863 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1865 if (dest_local_nexus)
1868 core_scsi3_lunacl_undepend_item(dest_se_deve);
1869 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1870 core_scsi3_tpg_undepend_item(dest_tpg);
1876 * Called with struct se_device->dev_reservation_lock held
1878 static int __core_scsi3_update_aptpl_buf(
1879 struct se_device *dev,
1881 u32 pr_aptpl_buf_len,
1882 int clear_aptpl_metadata)
1885 struct se_portal_group *tpg;
1886 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1887 struct t10_pr_registration *pr_reg;
1888 unsigned char tmp[512], isid_buf[32];
1892 memset(buf, 0, pr_aptpl_buf_len);
1894 * Called to clear metadata once APTPL has been deactivated.
1896 if (clear_aptpl_metadata) {
1897 snprintf(buf, pr_aptpl_buf_len,
1898 "No Registrations or Reservations\n");
1902 * Walk the registration list..
1904 spin_lock(&su_dev->t10_pr.registration_lock);
1905 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1910 tpg = pr_reg->pr_reg_nacl->se_tpg;
1911 lun = pr_reg->pr_reg_tg_pt_lun;
1913 * Write out any ISID value to APTPL metadata that was included
1914 * in the original registration.
1916 if (pr_reg->isid_present_at_reg)
1917 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1918 pr_reg->pr_reg_isid);
1920 * Include special metadata if the pr_reg matches the
1921 * reservation holder.
1923 if (dev->dev_pr_res_holder == pr_reg) {
1924 snprintf(tmp, 512, "PR_REG_START: %d"
1925 "\ninitiator_fabric=%s\n"
1926 "initiator_node=%s\n%s"
1928 "res_holder=1\nres_type=%02x\n"
1929 "res_scope=%02x\nres_all_tg_pt=%d\n"
1930 "mapped_lun=%u\n", reg_count,
1931 tpg->se_tpg_tfo->get_fabric_name(),
1932 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1933 pr_reg->pr_res_key, pr_reg->pr_res_type,
1934 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1935 pr_reg->pr_res_mapped_lun);
1937 snprintf(tmp, 512, "PR_REG_START: %d\n"
1938 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1939 "sa_res_key=%llu\nres_holder=0\n"
1940 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1941 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1942 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1943 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1944 pr_reg->pr_res_mapped_lun);
1947 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1948 pr_err("Unable to update renaming"
1949 " APTPL metadata\n");
1950 spin_unlock(&su_dev->t10_pr.registration_lock);
1953 len += sprintf(buf+len, "%s", tmp);
1956 * Include information about the associated SCSI target port.
1958 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1959 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1960 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1961 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1962 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1963 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1965 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1966 pr_err("Unable to update renaming"
1967 " APTPL metadata\n");
1968 spin_unlock(&su_dev->t10_pr.registration_lock);
1971 len += sprintf(buf+len, "%s", tmp);
1974 spin_unlock(&su_dev->t10_pr.registration_lock);
1977 len += sprintf(buf+len, "No Registrations or Reservations");
1982 static int core_scsi3_update_aptpl_buf(
1983 struct se_device *dev,
1985 u32 pr_aptpl_buf_len,
1986 int clear_aptpl_metadata)
1990 spin_lock(&dev->dev_reservation_lock);
1991 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
1992 clear_aptpl_metadata);
1993 spin_unlock(&dev->dev_reservation_lock);
1999 * Called with struct se_device->aptpl_file_mutex held
2001 static int __core_scsi3_write_aptpl_to_file(
2002 struct se_device *dev,
2004 u32 pr_aptpl_buf_len)
2006 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
2008 struct iovec iov[1];
2009 mm_segment_t old_fs;
2010 int flags = O_RDWR | O_CREAT | O_TRUNC;
2014 memset(iov, 0, sizeof(struct iovec));
2015 memset(path, 0, 512);
2017 if (strlen(&wwn->unit_serial[0]) >= 512) {
2018 pr_err("WWN value for struct se_device does not fit"
2019 " into path buffer\n");
2023 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2024 file = filp_open(path, flags, 0600);
2025 if (IS_ERR(file) || !file || !file->f_dentry) {
2026 pr_err("filp_open(%s) for APTPL metadata"
2028 return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
2031 iov[0].iov_base = &buf[0];
2032 if (!pr_aptpl_buf_len)
2033 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2035 iov[0].iov_len = pr_aptpl_buf_len;
2039 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2043 pr_debug("Error writing APTPL metadata file: %s\n", path);
2044 filp_close(file, NULL);
2047 filp_close(file, NULL);
2052 static int core_scsi3_update_and_write_aptpl(
2053 struct se_device *dev,
2054 unsigned char *in_buf,
2055 u32 in_pr_aptpl_buf_len)
2057 unsigned char null_buf[64], *buf;
2058 u32 pr_aptpl_buf_len;
2059 int ret, clear_aptpl_metadata = 0;
2061 * Can be called with a NULL pointer from PROUT service action CLEAR
2064 memset(null_buf, 0, 64);
2067 * This will clear the APTPL metadata to:
2068 * "No Registrations or Reservations" status
2070 pr_aptpl_buf_len = 64;
2071 clear_aptpl_metadata = 1;
2074 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2077 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2078 clear_aptpl_metadata);
2082 * __core_scsi3_write_aptpl_to_file() will call strlen()
2083 * on the passed buf to determine pr_aptpl_buf_len.
2085 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2092 static int core_scsi3_emulate_pro_register(
2101 struct se_session *se_sess = cmd->se_sess;
2102 struct se_device *dev = cmd->se_dev;
2103 struct se_dev_entry *se_deve;
2104 struct se_lun *se_lun = cmd->se_lun;
2105 struct se_portal_group *se_tpg;
2106 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2107 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2108 /* Used for APTPL metadata w/ UNREGISTER */
2109 unsigned char *pr_aptpl_buf = NULL;
2110 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2111 int pr_holder = 0, ret = 0, type;
2113 if (!se_sess || !se_lun) {
2114 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2115 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2118 se_tpg = se_sess->se_tpg;
2119 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2121 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2122 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2123 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2125 isid_ptr = &isid_buf[0];
2128 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2130 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2133 pr_warn("SPC-3 PR: Reservation Key non-zero"
2134 " for SA REGISTER, returning CONFLICT\n");
2135 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2139 * Do nothing but return GOOD status.
2146 * Perform the Service Action REGISTER on the Initiator
2147 * Port Endpoint that the PRO was received from on the
2148 * Logical Unit of the SCSI device server.
2150 ret = core_scsi3_alloc_registration(cmd->se_dev,
2151 se_sess->se_node_acl, se_deve, isid_ptr,
2152 sa_res_key, all_tg_pt, aptpl,
2155 pr_err("Unable to allocate"
2156 " struct t10_pr_registration\n");
2157 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2162 * Register both the Initiator port that received
2163 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2164 * TransportID from Parameter list and loop through
2165 * fabric dependent parameter list while calling
2166 * logic from of core_scsi3_alloc_registration() for
2167 * each TransportID provided SCSI Initiator Port/Device
2169 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2170 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2175 * Nothing left to do for the APTPL=0 case.
2178 pr_tmpl->pr_aptpl_active = 0;
2179 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2180 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2185 * Locate the newly allocated local I_T Nexus *pr_reg, and
2186 * update the APTPL metadata information using its
2187 * preallocated *pr_reg->pr_aptpl_buf.
2189 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2190 se_sess->se_node_acl, se_sess);
2192 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2193 &pr_reg->pr_aptpl_buf[0],
2194 pr_tmpl->pr_aptpl_buf_len);
2196 pr_tmpl->pr_aptpl_active = 1;
2197 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2200 core_scsi3_put_pr_reg(pr_reg);
2204 * Locate the existing *pr_reg via struct se_node_acl pointers
2207 type = pr_reg->pr_res_type;
2210 if (res_key != pr_reg->pr_res_key) {
2211 pr_err("SPC-3 PR REGISTER: Received"
2212 " res_key: 0x%016Lx does not match"
2213 " existing SA REGISTER res_key:"
2214 " 0x%016Lx\n", res_key,
2215 pr_reg->pr_res_key);
2216 core_scsi3_put_pr_reg(pr_reg);
2217 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2222 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2223 " set while sa_res_key=0\n");
2224 core_scsi3_put_pr_reg(pr_reg);
2225 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2229 * An existing ALL_TG_PT=1 registration being released
2230 * must also set ALL_TG_PT=1 in the incoming PROUT.
2232 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2233 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2234 " registration exists, but ALL_TG_PT=1 bit not"
2235 " present in received PROUT\n");
2236 core_scsi3_put_pr_reg(pr_reg);
2237 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2241 * Allocate APTPL metadata buffer used for UNREGISTER ops
2244 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2246 if (!pr_aptpl_buf) {
2247 pr_err("Unable to allocate"
2249 core_scsi3_put_pr_reg(pr_reg);
2250 cmd->scsi_sense_reason =
2251 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2256 * sa_res_key=0 Unregister Reservation Key for registered I_T
2257 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2261 pr_holder = core_scsi3_check_implict_release(
2262 cmd->se_dev, pr_reg);
2263 if (pr_holder < 0) {
2264 kfree(pr_aptpl_buf);
2265 core_scsi3_put_pr_reg(pr_reg);
2266 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2270 spin_lock(&pr_tmpl->registration_lock);
2272 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2273 * and matching pr_res_key.
2275 if (pr_reg->pr_reg_all_tg_pt) {
2276 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2277 &pr_tmpl->registration_list,
2280 if (!pr_reg_p->pr_reg_all_tg_pt)
2283 if (pr_reg_p->pr_res_key != res_key)
2286 if (pr_reg == pr_reg_p)
2289 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2290 pr_reg_p->pr_reg_nacl->initiatorname))
2293 __core_scsi3_free_registration(dev,
2298 * Release the calling I_T Nexus registration now..
2300 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2303 * From spc4r17, section 5.7.11.3 Unregistering
2305 * If the persistent reservation is a registrants only
2306 * type, the device server shall establish a unit
2307 * attention condition for the initiator port associated
2308 * with every registered I_T nexus except for the I_T
2309 * nexus on which the PERSISTENT RESERVE OUT command was
2310 * received, with the additional sense code set to
2311 * RESERVATIONS RELEASED.
2314 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2315 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2316 list_for_each_entry(pr_reg_p,
2317 &pr_tmpl->registration_list,
2320 core_scsi3_ua_allocate(
2321 pr_reg_p->pr_reg_nacl,
2322 pr_reg_p->pr_res_mapped_lun,
2324 ASCQ_2AH_RESERVATIONS_RELEASED);
2327 spin_unlock(&pr_tmpl->registration_lock);
2330 pr_tmpl->pr_aptpl_active = 0;
2331 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2332 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2333 " for UNREGISTER\n");
2337 ret = core_scsi3_update_and_write_aptpl(dev,
2339 pr_tmpl->pr_aptpl_buf_len);
2341 pr_tmpl->pr_aptpl_active = 1;
2342 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2343 " for UNREGISTER\n");
2346 kfree(pr_aptpl_buf);
2350 * Increment PRgeneration counter for struct se_device"
2351 * upon a successful REGISTER, see spc4r17 section 6.3.2
2352 * READ_KEYS service action.
2354 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2356 pr_reg->pr_res_key = sa_res_key;
2357 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2358 " Key for %s to: 0x%016Lx PRgeneration:"
2359 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2360 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2361 pr_reg->pr_reg_nacl->initiatorname,
2362 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2365 pr_tmpl->pr_aptpl_active = 0;
2366 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2367 core_scsi3_put_pr_reg(pr_reg);
2368 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2373 ret = core_scsi3_update_and_write_aptpl(dev,
2375 pr_tmpl->pr_aptpl_buf_len);
2377 pr_tmpl->pr_aptpl_active = 1;
2378 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2382 kfree(pr_aptpl_buf);
2383 core_scsi3_put_pr_reg(pr_reg);
2389 unsigned char *core_scsi3_pr_dump_type(int type)
2392 case PR_TYPE_WRITE_EXCLUSIVE:
2393 return "Write Exclusive Access";
2394 case PR_TYPE_EXCLUSIVE_ACCESS:
2395 return "Exclusive Access";
2396 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2397 return "Write Exclusive Access, Registrants Only";
2398 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2399 return "Exclusive Access, Registrants Only";
2400 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2401 return "Write Exclusive Access, All Registrants";
2402 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2403 return "Exclusive Access, All Registrants";
2408 return "Unknown SPC-3 PR Type";
2411 static int core_scsi3_pro_reserve(
2413 struct se_device *dev,
2418 struct se_session *se_sess = cmd->se_sess;
2419 struct se_dev_entry *se_deve;
2420 struct se_lun *se_lun = cmd->se_lun;
2421 struct se_portal_group *se_tpg;
2422 struct t10_pr_registration *pr_reg, *pr_res_holder;
2423 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2424 char i_buf[PR_REG_ISID_ID_LEN];
2427 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2429 if (!se_sess || !se_lun) {
2430 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2431 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2434 se_tpg = se_sess->se_tpg;
2435 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2437 * Locate the existing *pr_reg via struct se_node_acl pointers
2439 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2442 pr_err("SPC-3 PR: Unable to locate"
2443 " PR_REGISTERED *pr_reg for RESERVE\n");
2444 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2448 * From spc4r17 Section 5.7.9: Reserving:
2450 * An application client creates a persistent reservation by issuing
2451 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2452 * a registered I_T nexus with the following parameters:
2453 * a) RESERVATION KEY set to the value of the reservation key that is
2454 * registered with the logical unit for the I_T nexus; and
2456 if (res_key != pr_reg->pr_res_key) {
2457 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2458 " does not match existing SA REGISTER res_key:"
2459 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2460 core_scsi3_put_pr_reg(pr_reg);
2461 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2465 * From spc4r17 Section 5.7.9: Reserving:
2468 * b) TYPE field and SCOPE field set to the persistent reservation
2471 * Only one persistent reservation is allowed at a time per logical unit
2472 * and that persistent reservation has a scope of LU_SCOPE.
2474 if (scope != PR_SCOPE_LU_SCOPE) {
2475 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2476 core_scsi3_put_pr_reg(pr_reg);
2477 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2481 * See if we have an existing PR reservation holder pointer at
2482 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2485 spin_lock(&dev->dev_reservation_lock);
2486 pr_res_holder = dev->dev_pr_res_holder;
2487 if ((pr_res_holder)) {
2489 * From spc4r17 Section 5.7.9: Reserving:
2491 * If the device server receives a PERSISTENT RESERVE OUT
2492 * command from an I_T nexus other than a persistent reservation
2493 * holder (see 5.7.10) that attempts to create a persistent
2494 * reservation when a persistent reservation already exists for
2495 * the logical unit, then the command shall be completed with
2496 * RESERVATION CONFLICT status.
2498 if (pr_res_holder != pr_reg) {
2499 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2500 pr_err("SPC-3 PR: Attempted RESERVE from"
2501 " [%s]: %s while reservation already held by"
2502 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2503 cmd->se_tfo->get_fabric_name(),
2504 se_sess->se_node_acl->initiatorname,
2505 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2506 pr_res_holder->pr_reg_nacl->initiatorname);
2508 spin_unlock(&dev->dev_reservation_lock);
2509 core_scsi3_put_pr_reg(pr_reg);
2510 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2514 * From spc4r17 Section 5.7.9: Reserving:
2516 * If a persistent reservation holder attempts to modify the
2517 * type or scope of an existing persistent reservation, the
2518 * command shall be completed with RESERVATION CONFLICT status.
2520 if ((pr_res_holder->pr_res_type != type) ||
2521 (pr_res_holder->pr_res_scope != scope)) {
2522 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2523 pr_err("SPC-3 PR: Attempted RESERVE from"
2524 " [%s]: %s trying to change TYPE and/or SCOPE,"
2525 " while reservation already held by [%s]: %s,"
2526 " returning RESERVATION_CONFLICT\n",
2527 cmd->se_tfo->get_fabric_name(),
2528 se_sess->se_node_acl->initiatorname,
2529 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2530 pr_res_holder->pr_reg_nacl->initiatorname);
2532 spin_unlock(&dev->dev_reservation_lock);
2533 core_scsi3_put_pr_reg(pr_reg);
2534 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2538 * From spc4r17 Section 5.7.9: Reserving:
2540 * If the device server receives a PERSISTENT RESERVE OUT
2541 * command with RESERVE service action where the TYPE field and
2542 * the SCOPE field contain the same values as the existing type
2543 * and scope from a persistent reservation holder, it shall not
2544 * make any change to the existing persistent reservation and
2545 * shall completethe command with GOOD status.
2547 spin_unlock(&dev->dev_reservation_lock);
2548 core_scsi3_put_pr_reg(pr_reg);
2552 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2553 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2555 pr_reg->pr_res_scope = scope;
2556 pr_reg->pr_res_type = type;
2557 pr_reg->pr_res_holder = 1;
2558 dev->dev_pr_res_holder = pr_reg;
2559 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2560 PR_REG_ISID_ID_LEN);
2562 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2563 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2564 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2565 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2566 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2567 cmd->se_tfo->get_fabric_name(),
2568 se_sess->se_node_acl->initiatorname,
2569 (prf_isid) ? &i_buf[0] : "");
2570 spin_unlock(&dev->dev_reservation_lock);
2572 if (pr_tmpl->pr_aptpl_active) {
2573 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2574 &pr_reg->pr_aptpl_buf[0],
2575 pr_tmpl->pr_aptpl_buf_len);
2577 pr_debug("SPC-3 PR: Updated APTPL metadata"
2581 core_scsi3_put_pr_reg(pr_reg);
2585 static int core_scsi3_emulate_pro_reserve(
2591 struct se_device *dev = cmd->se_dev;
2595 case PR_TYPE_WRITE_EXCLUSIVE:
2596 case PR_TYPE_EXCLUSIVE_ACCESS:
2597 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2598 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2599 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2600 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2601 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2604 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2606 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2614 * Called with struct se_device->dev_reservation_lock held.
2616 static void __core_scsi3_complete_pro_release(
2617 struct se_device *dev,
2618 struct se_node_acl *se_nacl,
2619 struct t10_pr_registration *pr_reg,
2622 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2623 char i_buf[PR_REG_ISID_ID_LEN];
2626 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2627 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2628 PR_REG_ISID_ID_LEN);
2630 * Go ahead and release the current PR reservation holder.
2632 dev->dev_pr_res_holder = NULL;
2634 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2635 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2636 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2637 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2638 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2639 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2640 tfo->get_fabric_name(), se_nacl->initiatorname,
2641 (prf_isid) ? &i_buf[0] : "");
2643 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2645 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2648 static int core_scsi3_emulate_pro_release(
2654 struct se_device *dev = cmd->se_dev;
2655 struct se_session *se_sess = cmd->se_sess;
2656 struct se_lun *se_lun = cmd->se_lun;
2657 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2658 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2659 int ret, all_reg = 0;
2661 if (!se_sess || !se_lun) {
2662 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2663 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2667 * Locate the existing *pr_reg via struct se_node_acl pointers
2669 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2671 pr_err("SPC-3 PR: Unable to locate"
2672 " PR_REGISTERED *pr_reg for RELEASE\n");
2673 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2677 * From spc4r17 Section 5.7.11.2 Releasing:
2679 * If there is no persistent reservation or in response to a persistent
2680 * reservation release request from a registered I_T nexus that is not a
2681 * persistent reservation holder (see 5.7.10), the device server shall
2684 * a) Not release the persistent reservation, if any;
2685 * b) Not remove any registrations; and
2686 * c) Complete the command with GOOD status.
2688 spin_lock(&dev->dev_reservation_lock);
2689 pr_res_holder = dev->dev_pr_res_holder;
2690 if (!pr_res_holder) {
2692 * No persistent reservation, return GOOD status.
2694 spin_unlock(&dev->dev_reservation_lock);
2695 core_scsi3_put_pr_reg(pr_reg);
2698 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2699 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2702 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2704 * Non 'All Registrants' PR Type cases..
2705 * Release request from a registered I_T nexus that is not a
2706 * persistent reservation holder. return GOOD status.
2708 spin_unlock(&dev->dev_reservation_lock);
2709 core_scsi3_put_pr_reg(pr_reg);
2713 * From spc4r17 Section 5.7.11.2 Releasing:
2715 * Only the persistent reservation holder (see 5.7.10) is allowed to
2716 * release a persistent reservation.
2718 * An application client releases the persistent reservation by issuing
2719 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2720 * an I_T nexus that is a persistent reservation holder with the
2721 * following parameters:
2723 * a) RESERVATION KEY field set to the value of the reservation key
2724 * that is registered with the logical unit for the I_T nexus;
2726 if (res_key != pr_reg->pr_res_key) {
2727 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2728 " does not match existing SA REGISTER res_key:"
2729 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2730 spin_unlock(&dev->dev_reservation_lock);
2731 core_scsi3_put_pr_reg(pr_reg);
2732 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2736 * From spc4r17 Section 5.7.11.2 Releasing and above:
2738 * b) TYPE field and SCOPE field set to match the persistent
2739 * reservation being released.
2741 if ((pr_res_holder->pr_res_type != type) ||
2742 (pr_res_holder->pr_res_scope != scope)) {
2743 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2744 pr_err("SPC-3 PR RELEASE: Attempted to release"
2745 " reservation from [%s]: %s with different TYPE "
2746 "and/or SCOPE while reservation already held by"
2747 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2748 cmd->se_tfo->get_fabric_name(),
2749 se_sess->se_node_acl->initiatorname,
2750 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2751 pr_res_holder->pr_reg_nacl->initiatorname);
2753 spin_unlock(&dev->dev_reservation_lock);
2754 core_scsi3_put_pr_reg(pr_reg);
2755 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2759 * In response to a persistent reservation release request from the
2760 * persistent reservation holder the device server shall perform a
2761 * release by doing the following as an uninterrupted series of actions:
2762 * a) Release the persistent reservation;
2763 * b) Not remove any registration(s);
2764 * c) If the released persistent reservation is a registrants only type
2765 * or all registrants type persistent reservation,
2766 * the device server shall establish a unit attention condition for
2767 * the initiator port associated with every regis-
2768 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2769 * RESERVE OUT command with RELEASE service action was received,
2770 * with the additional sense code set to RESERVATIONS RELEASED; and
2771 * d) If the persistent reservation is of any other type, the device
2772 * server shall not establish a unit attention condition.
2774 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2777 spin_unlock(&dev->dev_reservation_lock);
2779 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2780 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2781 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2782 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2784 * If no UNIT ATTENTION conditions will be established for
2785 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2786 * go ahead and check for APTPL=1 update+write below
2791 spin_lock(&pr_tmpl->registration_lock);
2792 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2795 * Do not establish a UNIT ATTENTION condition
2796 * for the calling I_T Nexus
2798 if (pr_reg_p == pr_reg)
2801 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2802 pr_reg_p->pr_res_mapped_lun,
2803 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2805 spin_unlock(&pr_tmpl->registration_lock);
2808 if (pr_tmpl->pr_aptpl_active) {
2809 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2810 &pr_reg->pr_aptpl_buf[0],
2811 pr_tmpl->pr_aptpl_buf_len);
2813 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2816 core_scsi3_put_pr_reg(pr_reg);
2820 static int core_scsi3_emulate_pro_clear(
2824 struct se_device *dev = cmd->se_dev;
2825 struct se_node_acl *pr_reg_nacl;
2826 struct se_session *se_sess = cmd->se_sess;
2827 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2828 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2829 u32 pr_res_mapped_lun = 0;
2830 int calling_it_nexus = 0;
2832 * Locate the existing *pr_reg via struct se_node_acl pointers
2834 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2835 se_sess->se_node_acl, se_sess);
2837 pr_err("SPC-3 PR: Unable to locate"
2838 " PR_REGISTERED *pr_reg for CLEAR\n");
2839 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2843 * From spc4r17 section 5.7.11.6, Clearing:
2845 * Any application client may release the persistent reservation and
2846 * remove all registrations from a device server by issuing a
2847 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2848 * registered I_T nexus with the following parameter:
2850 * a) RESERVATION KEY field set to the value of the reservation key
2851 * that is registered with the logical unit for the I_T nexus.
2853 if (res_key != pr_reg_n->pr_res_key) {
2854 pr_err("SPC-3 PR REGISTER: Received"
2855 " res_key: 0x%016Lx does not match"
2856 " existing SA REGISTER res_key:"
2857 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2858 core_scsi3_put_pr_reg(pr_reg_n);
2859 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2863 * a) Release the persistent reservation, if any;
2865 spin_lock(&dev->dev_reservation_lock);
2866 pr_res_holder = dev->dev_pr_res_holder;
2867 if (pr_res_holder) {
2868 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2869 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2872 spin_unlock(&dev->dev_reservation_lock);
2874 * b) Remove all registration(s) (see spc4r17 5.7.7);
2876 spin_lock(&pr_tmpl->registration_lock);
2877 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2878 &pr_tmpl->registration_list, pr_reg_list) {
2880 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2881 pr_reg_nacl = pr_reg->pr_reg_nacl;
2882 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2883 __core_scsi3_free_registration(dev, pr_reg, NULL,
2886 * e) Establish a unit attention condition for the initiator
2887 * port associated with every registered I_T nexus other
2888 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2889 * command with CLEAR service action was received, with the
2890 * additional sense code set to RESERVATIONS PREEMPTED.
2892 if (!calling_it_nexus)
2893 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2894 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2896 spin_unlock(&pr_tmpl->registration_lock);
2898 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2899 cmd->se_tfo->get_fabric_name());
2901 if (pr_tmpl->pr_aptpl_active) {
2902 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2903 pr_debug("SPC-3 PR: Updated APTPL metadata"
2907 core_scsi3_pr_generation(dev);
2912 * Called with struct se_device->dev_reservation_lock held.
2914 static void __core_scsi3_complete_pro_preempt(
2915 struct se_device *dev,
2916 struct t10_pr_registration *pr_reg,
2917 struct list_head *preempt_and_abort_list,
2922 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2923 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2924 char i_buf[PR_REG_ISID_ID_LEN];
2927 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2928 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2929 PR_REG_ISID_ID_LEN);
2931 * Do an implict RELEASE of the existing reservation.
2933 if (dev->dev_pr_res_holder)
2934 __core_scsi3_complete_pro_release(dev, nacl,
2935 dev->dev_pr_res_holder, 0);
2937 dev->dev_pr_res_holder = pr_reg;
2938 pr_reg->pr_res_holder = 1;
2939 pr_reg->pr_res_type = type;
2940 pr_reg->pr_res_scope = scope;
2942 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2943 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2944 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2945 core_scsi3_pr_dump_type(type),
2946 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2947 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2948 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2949 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2951 * For PREEMPT_AND_ABORT, add the preempting reservation's
2952 * struct t10_pr_registration to the list that will be compared
2953 * against received CDBs..
2955 if (preempt_and_abort_list)
2956 list_add_tail(&pr_reg->pr_reg_abort_list,
2957 preempt_and_abort_list);
2960 static void core_scsi3_release_preempt_and_abort(
2961 struct list_head *preempt_and_abort_list,
2962 struct t10_pr_registration *pr_reg_holder)
2964 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2966 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2967 pr_reg_abort_list) {
2969 list_del(&pr_reg->pr_reg_abort_list);
2970 if (pr_reg_holder == pr_reg)
2972 if (pr_reg->pr_res_holder) {
2973 pr_warn("pr_reg->pr_res_holder still set\n");
2977 pr_reg->pr_reg_deve = NULL;
2978 pr_reg->pr_reg_nacl = NULL;
2979 kfree(pr_reg->pr_aptpl_buf);
2980 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2984 static int core_scsi3_pro_preempt(
2992 struct se_device *dev = cmd->se_dev;
2993 struct se_dev_entry *se_deve;
2994 struct se_node_acl *pr_reg_nacl;
2995 struct se_session *se_sess = cmd->se_sess;
2996 LIST_HEAD(preempt_and_abort_list);
2997 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2998 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2999 u32 pr_res_mapped_lun = 0;
3000 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3001 int prh_type = 0, prh_scope = 0, ret;
3004 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3008 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3009 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3012 pr_err("SPC-3 PR: Unable to locate"
3013 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3014 (abort) ? "_AND_ABORT" : "");
3015 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3018 if (pr_reg_n->pr_res_key != res_key) {
3019 core_scsi3_put_pr_reg(pr_reg_n);
3020 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3023 if (scope != PR_SCOPE_LU_SCOPE) {
3024 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3025 core_scsi3_put_pr_reg(pr_reg_n);
3026 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3030 spin_lock(&dev->dev_reservation_lock);
3031 pr_res_holder = dev->dev_pr_res_holder;
3032 if (pr_res_holder &&
3033 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3034 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3037 if (!all_reg && !sa_res_key) {
3038 spin_unlock(&dev->dev_reservation_lock);
3039 core_scsi3_put_pr_reg(pr_reg_n);
3040 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3044 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3046 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3047 * persistent reservation holder or there is no persistent reservation
3048 * holder (i.e., there is no persistent reservation), then the device
3049 * server shall perform a preempt by doing the following in an
3050 * uninterrupted series of actions. (See below..)
3052 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3054 * No existing or SA Reservation Key matching reservations..
3056 * PROUT SA PREEMPT with All Registrant type reservations are
3057 * allowed to be processed without a matching SA Reservation Key
3059 spin_lock(&pr_tmpl->registration_lock);
3060 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3061 &pr_tmpl->registration_list, pr_reg_list) {
3063 * Removing of registrations in non all registrants
3064 * type reservations without a matching SA reservation
3067 * a) Remove the registrations for all I_T nexuses
3068 * specified by the SERVICE ACTION RESERVATION KEY
3070 * b) Ignore the contents of the SCOPE and TYPE fields;
3071 * c) Process tasks as defined in 5.7.1; and
3072 * d) Establish a unit attention condition for the
3073 * initiator port associated with every I_T nexus
3074 * that lost its registration other than the I_T
3075 * nexus on which the PERSISTENT RESERVE OUT command
3076 * was received, with the additional sense code set
3077 * to REGISTRATIONS PREEMPTED.
3080 if (pr_reg->pr_res_key != sa_res_key)
3083 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3084 pr_reg_nacl = pr_reg->pr_reg_nacl;
3085 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3086 __core_scsi3_free_registration(dev, pr_reg,
3087 (abort) ? &preempt_and_abort_list :
3088 NULL, calling_it_nexus);
3092 * Case for any existing all registrants type
3093 * reservation, follow logic in spc4r17 section
3094 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3096 * For a ZERO SA Reservation key, release
3097 * all other registrations and do an implict
3098 * release of active persistent reservation.
3100 * For a non-ZERO SA Reservation key, only
3101 * release the matching reservation key from
3105 (pr_reg->pr_res_key != sa_res_key))
3108 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3109 if (calling_it_nexus)
3112 pr_reg_nacl = pr_reg->pr_reg_nacl;
3113 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3114 __core_scsi3_free_registration(dev, pr_reg,
3115 (abort) ? &preempt_and_abort_list :
3119 if (!calling_it_nexus)
3120 core_scsi3_ua_allocate(pr_reg_nacl,
3121 pr_res_mapped_lun, 0x2A,
3122 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3124 spin_unlock(&pr_tmpl->registration_lock);
3126 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3127 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3128 * RESERVATION KEY field to a value that does not match any
3129 * registered reservation key, then the device server shall
3130 * complete the command with RESERVATION CONFLICT status.
3132 if (!released_regs) {
3133 spin_unlock(&dev->dev_reservation_lock);
3134 core_scsi3_put_pr_reg(pr_reg_n);
3135 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3139 * For an existing all registrants type reservation
3140 * with a zero SA rservation key, preempt the existing
3141 * reservation with the new PR type and scope.
3143 if (pr_res_holder && all_reg && !(sa_res_key)) {
3144 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3145 (abort) ? &preempt_and_abort_list : NULL,
3146 type, scope, abort);
3149 core_scsi3_release_preempt_and_abort(
3150 &preempt_and_abort_list, pr_reg_n);
3152 spin_unlock(&dev->dev_reservation_lock);
3154 if (pr_tmpl->pr_aptpl_active) {
3155 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3156 &pr_reg_n->pr_aptpl_buf[0],
3157 pr_tmpl->pr_aptpl_buf_len);
3159 pr_debug("SPC-3 PR: Updated APTPL"
3160 " metadata for PREEMPT%s\n", (abort) ?
3164 core_scsi3_put_pr_reg(pr_reg_n);
3165 core_scsi3_pr_generation(cmd->se_dev);
3169 * The PREEMPTing SA reservation key matches that of the
3170 * existing persistent reservation, first, we check if
3171 * we are preempting our own reservation.
3172 * From spc4r17, section 5.7.11.4.3 Preempting
3173 * persistent reservations and registration handling
3175 * If an all registrants persistent reservation is not
3176 * present, it is not an error for the persistent
3177 * reservation holder to preempt itself (i.e., a
3178 * PERSISTENT RESERVE OUT with a PREEMPT service action
3179 * or a PREEMPT AND ABORT service action with the
3180 * SERVICE ACTION RESERVATION KEY value equal to the
3181 * persistent reservation holder's reservation key that
3182 * is received from the persistent reservation holder).
3183 * In that case, the device server shall establish the
3184 * new persistent reservation and maintain the
3187 prh_type = pr_res_holder->pr_res_type;
3188 prh_scope = pr_res_holder->pr_res_scope;
3190 * If the SERVICE ACTION RESERVATION KEY field identifies a
3191 * persistent reservation holder (see 5.7.10), the device
3192 * server shall perform a preempt by doing the following as
3193 * an uninterrupted series of actions:
3195 * a) Release the persistent reservation for the holder
3196 * identified by the SERVICE ACTION RESERVATION KEY field;
3198 if (pr_reg_n != pr_res_holder)
3199 __core_scsi3_complete_pro_release(dev,
3200 pr_res_holder->pr_reg_nacl,
3201 dev->dev_pr_res_holder, 0);
3203 * b) Remove the registrations for all I_T nexuses identified
3204 * by the SERVICE ACTION RESERVATION KEY field, except the
3205 * I_T nexus that is being used for the PERSISTENT RESERVE
3206 * OUT command. If an all registrants persistent reservation
3207 * is present and the SERVICE ACTION RESERVATION KEY field
3208 * is set to zero, then all registrations shall be removed
3209 * except for that of the I_T nexus that is being used for
3210 * the PERSISTENT RESERVE OUT command;
3212 spin_lock(&pr_tmpl->registration_lock);
3213 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3214 &pr_tmpl->registration_list, pr_reg_list) {
3216 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3217 if (calling_it_nexus)
3220 if (pr_reg->pr_res_key != sa_res_key)
3223 pr_reg_nacl = pr_reg->pr_reg_nacl;
3224 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3225 __core_scsi3_free_registration(dev, pr_reg,
3226 (abort) ? &preempt_and_abort_list : NULL,
3229 * e) Establish a unit attention condition for the initiator
3230 * port associated with every I_T nexus that lost its
3231 * persistent reservation and/or registration, with the
3232 * additional sense code set to REGISTRATIONS PREEMPTED;
3234 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3235 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3237 spin_unlock(&pr_tmpl->registration_lock);
3239 * c) Establish a persistent reservation for the preempting
3240 * I_T nexus using the contents of the SCOPE and TYPE fields;
3242 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3243 (abort) ? &preempt_and_abort_list : NULL,
3244 type, scope, abort);
3246 * d) Process tasks as defined in 5.7.1;
3248 * f) If the type or scope has changed, then for every I_T nexus
3249 * whose reservation key was not removed, except for the I_T
3250 * nexus on which the PERSISTENT RESERVE OUT command was
3251 * received, the device server shall establish a unit
3252 * attention condition for the initiator port associated with
3253 * that I_T nexus, with the additional sense code set to
3254 * RESERVATIONS RELEASED. If the type or scope have not
3255 * changed, then no unit attention condition(s) shall be
3256 * established for this reason.
3258 if ((prh_type != type) || (prh_scope != scope)) {
3259 spin_lock(&pr_tmpl->registration_lock);
3260 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3261 &pr_tmpl->registration_list, pr_reg_list) {
3263 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3264 if (calling_it_nexus)
3267 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3268 pr_reg->pr_res_mapped_lun, 0x2A,
3269 ASCQ_2AH_RESERVATIONS_RELEASED);
3271 spin_unlock(&pr_tmpl->registration_lock);
3273 spin_unlock(&dev->dev_reservation_lock);
3275 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3276 * All received CDBs for the matching existing reservation and
3277 * registrations undergo ABORT_TASK logic.
3279 * From there, core_scsi3_release_preempt_and_abort() will
3280 * release every registration in the list (which have already
3281 * been removed from the primary pr_reg list), except the
3282 * new persistent reservation holder, the calling Initiator Port.
3285 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3286 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3290 if (pr_tmpl->pr_aptpl_active) {
3291 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3292 &pr_reg_n->pr_aptpl_buf[0],
3293 pr_tmpl->pr_aptpl_buf_len);
3295 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3296 "%s\n", (abort) ? "_AND_ABORT" : "");
3299 core_scsi3_put_pr_reg(pr_reg_n);
3300 core_scsi3_pr_generation(cmd->se_dev);
3304 static int core_scsi3_emulate_pro_preempt(
3315 case PR_TYPE_WRITE_EXCLUSIVE:
3316 case PR_TYPE_EXCLUSIVE_ACCESS:
3317 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3318 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3319 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3320 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3321 ret = core_scsi3_pro_preempt(cmd, type, scope,
3322 res_key, sa_res_key, abort);
3325 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3326 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3327 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3335 static int core_scsi3_emulate_pro_register_and_move(
3342 struct se_session *se_sess = cmd->se_sess;
3343 struct se_device *dev = cmd->se_dev;
3344 struct se_dev_entry *se_deve, *dest_se_deve = NULL;
3345 struct se_lun *se_lun = cmd->se_lun;
3346 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3347 struct se_port *se_port;
3348 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3349 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3350 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3351 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3353 unsigned char *initiator_str;
3354 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3355 u32 tid_len, tmp_tid_len;
3356 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3357 unsigned short rtpi;
3358 unsigned char proto_ident;
3360 if (!se_sess || !se_lun) {
3361 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3362 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3365 memset(dest_iport, 0, 64);
3366 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3367 se_tpg = se_sess->se_tpg;
3368 tf_ops = se_tpg->se_tpg_tfo;
3369 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3371 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3372 * Register behaviors for a REGISTER AND MOVE service action
3374 * Locate the existing *pr_reg via struct se_node_acl pointers
3376 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3379 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3380 " *pr_reg for REGISTER_AND_MOVE\n");
3381 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3385 * The provided reservation key much match the existing reservation key
3386 * provided during this initiator's I_T nexus registration.
3388 if (res_key != pr_reg->pr_res_key) {
3389 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3390 " res_key: 0x%016Lx does not match existing SA REGISTER"
3391 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3392 core_scsi3_put_pr_reg(pr_reg);
3393 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3397 * The service active reservation key needs to be non zero
3400 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3402 core_scsi3_put_pr_reg(pr_reg);
3403 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3408 * Determine the Relative Target Port Identifier where the reservation
3409 * will be moved to for the TransportID containing SCSI initiator WWN
3412 buf = transport_kmap_data_sg(cmd);
3413 rtpi = (buf[18] & 0xff) << 8;
3414 rtpi |= buf[19] & 0xff;
3415 tid_len = (buf[20] & 0xff) << 24;
3416 tid_len |= (buf[21] & 0xff) << 16;
3417 tid_len |= (buf[22] & 0xff) << 8;
3418 tid_len |= buf[23] & 0xff;
3419 transport_kunmap_data_sg(cmd);
3422 if ((tid_len + 24) != cmd->data_length) {
3423 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3424 " does not equal CDB data_length: %u\n", tid_len,
3426 core_scsi3_put_pr_reg(pr_reg);
3427 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3431 spin_lock(&dev->se_port_lock);
3432 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3433 if (se_port->sep_rtpi != rtpi)
3435 dest_se_tpg = se_port->sep_tpg;
3438 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3442 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3443 smp_mb__after_atomic_inc();
3444 spin_unlock(&dev->se_port_lock);
3446 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3448 pr_err("core_scsi3_tpg_depend_item() failed"
3449 " for dest_se_tpg\n");
3450 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3451 smp_mb__after_atomic_dec();
3452 core_scsi3_put_pr_reg(pr_reg);
3453 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3457 spin_lock(&dev->se_port_lock);
3460 spin_unlock(&dev->se_port_lock);
3462 if (!dest_se_tpg || !dest_tf_ops) {
3463 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3464 " fabric ops from Relative Target Port Identifier:"
3466 core_scsi3_put_pr_reg(pr_reg);
3467 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3471 buf = transport_kmap_data_sg(cmd);
3472 proto_ident = (buf[24] & 0x0f);
3474 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3475 " 0x%02x\n", proto_ident);
3477 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3478 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3479 " proto_ident: 0x%02x does not match ident: 0x%02x"
3480 " from fabric: %s\n", proto_ident,
3481 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3482 dest_tf_ops->get_fabric_name());
3483 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3487 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3488 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3489 " containg a valid tpg_parse_pr_out_transport_id"
3490 " function pointer\n");
3491 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3495 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3496 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3497 if (!initiator_str) {
3498 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3499 " initiator_str from Transport ID\n");
3500 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3505 transport_kunmap_data_sg(cmd);
3508 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3509 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3510 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3513 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3514 * action specifies a TransportID that is the same as the initiator port
3515 * of the I_T nexus for the command received, then the command shall
3516 * be terminated with CHECK CONDITION status, with the sense key set to
3517 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3518 * IN PARAMETER LIST.
3520 pr_reg_nacl = pr_reg->pr_reg_nacl;
3521 matching_iname = (!strcmp(initiator_str,
3522 pr_reg_nacl->initiatorname)) ? 1 : 0;
3523 if (!matching_iname)
3524 goto after_iport_check;
3526 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3527 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3528 " matches: %s on received I_T Nexus\n", initiator_str,
3529 pr_reg_nacl->initiatorname);
3530 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3534 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3535 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3536 " matches: %s %s on received I_T Nexus\n",
3537 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3538 pr_reg->pr_reg_isid);
3539 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3545 * Locate the destination struct se_node_acl from the received Transport ID
3547 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3548 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3550 if (dest_node_acl) {
3551 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3552 smp_mb__after_atomic_inc();
3554 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3556 if (!dest_node_acl) {
3557 pr_err("Unable to locate %s dest_node_acl for"
3558 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3560 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3564 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3566 pr_err("core_scsi3_nodeacl_depend_item() for"
3567 " dest_node_acl\n");
3568 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3569 smp_mb__after_atomic_dec();
3570 dest_node_acl = NULL;
3571 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3576 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3577 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3578 dest_node_acl->initiatorname);
3581 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3584 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3585 if (!dest_se_deve) {
3586 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3587 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3588 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3593 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3595 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3596 atomic_dec(&dest_se_deve->pr_ref_count);
3597 smp_mb__after_atomic_dec();
3598 dest_se_deve = NULL;
3599 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3604 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3605 " ACL for dest_se_deve->mapped_lun: %u\n",
3606 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3607 dest_se_deve->mapped_lun);
3610 * A persistent reservation needs to already existing in order to
3611 * successfully complete the REGISTER_AND_MOVE service action..
3613 spin_lock(&dev->dev_reservation_lock);
3614 pr_res_holder = dev->dev_pr_res_holder;
3615 if (!pr_res_holder) {
3616 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3617 " currently held\n");
3618 spin_unlock(&dev->dev_reservation_lock);
3619 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3624 * The received on I_T Nexus must be the reservation holder.
3626 * From spc4r17 section 5.7.8 Table 50 --
3627 * Register behaviors for a REGISTER AND MOVE service action
3629 if (pr_res_holder != pr_reg) {
3630 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3631 " Nexus is not reservation holder\n");
3632 spin_unlock(&dev->dev_reservation_lock);
3633 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3638 * From spc4r17 section 5.7.8: registering and moving reservation
3640 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3641 * action is received and the established persistent reservation is a
3642 * Write Exclusive - All Registrants type or Exclusive Access -
3643 * All Registrants type reservation, then the command shall be completed
3644 * with RESERVATION CONFLICT status.
3646 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3647 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3648 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3649 " reservation for type: %s\n",
3650 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3651 spin_unlock(&dev->dev_reservation_lock);
3652 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3656 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3658 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3660 type = pr_res_holder->pr_res_type;
3661 scope = pr_res_holder->pr_res_type;
3663 * c) Associate the reservation key specified in the SERVICE ACTION
3664 * RESERVATION KEY field with the I_T nexus specified as the
3665 * destination of the register and move, where:
3666 * A) The I_T nexus is specified by the TransportID and the
3667 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3668 * B) Regardless of the TransportID format used, the association for
3669 * the initiator port is based on either the initiator port name
3670 * (see 3.1.71) on SCSI transport protocols where port names are
3671 * required or the initiator port identifier (see 3.1.70) on SCSI
3672 * transport protocols where port names are not required;
3673 * d) Register the reservation key specified in the SERVICE ACTION
3674 * RESERVATION KEY field;
3675 * e) Retain the reservation key specified in the SERVICE ACTION
3676 * RESERVATION KEY field and associated information;
3678 * Also, It is not an error for a REGISTER AND MOVE service action to
3679 * register an I_T nexus that is already registered with the same
3680 * reservation key or a different reservation key.
3682 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3685 ret = core_scsi3_alloc_registration(cmd->se_dev,
3686 dest_node_acl, dest_se_deve, iport_ptr,
3687 sa_res_key, 0, aptpl, 2, 1);
3689 spin_unlock(&dev->dev_reservation_lock);
3690 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3694 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3699 * f) Release the persistent reservation for the persistent reservation
3700 * holder (i.e., the I_T nexus on which the
3702 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3703 dev->dev_pr_res_holder, 0);
3705 * g) Move the persistent reservation to the specified I_T nexus using
3706 * the same scope and type as the persistent reservation released in
3709 dev->dev_pr_res_holder = dest_pr_reg;
3710 dest_pr_reg->pr_res_holder = 1;
3711 dest_pr_reg->pr_res_type = type;
3712 pr_reg->pr_res_scope = scope;
3713 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3714 PR_REG_ISID_ID_LEN);
3716 * Increment PRGeneration for existing registrations..
3719 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3720 spin_unlock(&dev->dev_reservation_lock);
3722 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3723 " created new reservation holder TYPE: %s on object RTPI:"
3724 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3725 core_scsi3_pr_dump_type(type), rtpi,
3726 dest_pr_reg->pr_res_generation);
3727 pr_debug("SPC-3 PR Successfully moved reservation from"
3728 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3729 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3730 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3731 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3734 * It is now safe to release configfs group dependencies for destination
3735 * of Transport ID Initiator Device/Port Identifier
3737 core_scsi3_lunacl_undepend_item(dest_se_deve);
3738 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3739 core_scsi3_tpg_undepend_item(dest_se_tpg);
3741 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3742 * nexus on which PERSISTENT RESERVE OUT command was received.
3745 spin_lock(&pr_tmpl->registration_lock);
3746 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3747 spin_unlock(&pr_tmpl->registration_lock);
3749 core_scsi3_put_pr_reg(pr_reg);
3752 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3753 * write out the updated metadata to struct file for this SCSI device.
3756 pr_tmpl->pr_aptpl_active = 0;
3757 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3758 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3759 " REGISTER_AND_MOVE\n");
3761 pr_tmpl->pr_aptpl_active = 1;
3762 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3763 &dest_pr_reg->pr_aptpl_buf[0],
3764 pr_tmpl->pr_aptpl_buf_len);
3766 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3767 " REGISTER_AND_MOVE\n");
3770 transport_kunmap_data_sg(cmd);
3772 core_scsi3_put_pr_reg(dest_pr_reg);
3776 transport_kunmap_data_sg(cmd);
3778 core_scsi3_lunacl_undepend_item(dest_se_deve);
3780 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3781 core_scsi3_tpg_undepend_item(dest_se_tpg);
3782 core_scsi3_put_pr_reg(pr_reg);
3786 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3788 unsigned int __v1, __v2;
3790 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3791 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3793 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3797 * See spc4r17 section 6.14 Table 170
3799 int target_scsi3_emulate_pr_out(struct se_task *task)
3801 struct se_cmd *cmd = task->task_se_cmd;
3802 unsigned char *cdb = &cmd->t_task_cdb[0];
3804 u64 res_key, sa_res_key;
3805 int sa, scope, type, aptpl;
3806 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3810 * Following spc2r20 5.5.1 Reservations overview:
3812 * If a logical unit has been reserved by any RESERVE command and is
3813 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3814 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3815 * initiator or service action and shall terminate with a RESERVATION
3818 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3819 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3820 " SPC-2 reservation is held, returning"
3821 " RESERVATION_CONFLICT\n");
3822 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3828 * FIXME: A NULL struct se_session pointer means an this is not coming from
3829 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3831 if (!cmd->se_sess) {
3832 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3836 if (cmd->data_length < 24) {
3837 pr_warn("SPC-PR: Received PR OUT parameter list"
3838 " length too small: %u\n", cmd->data_length);
3839 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3844 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3846 sa = (cdb[1] & 0x1f);
3847 scope = (cdb[2] & 0xf0);
3848 type = (cdb[2] & 0x0f);
3850 buf = transport_kmap_data_sg(cmd);
3852 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3854 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3855 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3857 * REGISTER_AND_MOVE uses a different SA parameter list containing
3858 * SCSI TransportIDs.
3860 if (sa != PRO_REGISTER_AND_MOVE) {
3861 spec_i_pt = (buf[20] & 0x08);
3862 all_tg_pt = (buf[20] & 0x04);
3863 aptpl = (buf[20] & 0x01);
3865 aptpl = (buf[17] & 0x01);
3866 unreg = (buf[17] & 0x02);
3868 transport_kunmap_data_sg(cmd);
3872 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3874 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3875 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3881 * From spc4r17 section 6.14:
3883 * If the SPEC_I_PT bit is set to zero, the service action is not
3884 * REGISTER AND MOVE, and the parameter list length is not 24, then
3885 * the command shall be terminated with CHECK CONDITION status, with
3886 * the sense key set to ILLEGAL REQUEST, and the additional sense
3887 * code set to PARAMETER LIST LENGTH ERROR.
3889 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3890 (cmd->data_length != 24)) {
3891 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3892 " list length: %u\n", cmd->data_length);
3893 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3898 * (core_scsi3_emulate_pro_* function parameters
3899 * are defined by spc4r17 Table 174:
3900 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3904 ret = core_scsi3_emulate_pro_register(cmd,
3905 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3908 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3911 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3914 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3917 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3918 res_key, sa_res_key, 0);
3920 case PRO_PREEMPT_AND_ABORT:
3921 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3922 res_key, sa_res_key, 1);
3924 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3925 ret = core_scsi3_emulate_pro_register(cmd,
3926 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3928 case PRO_REGISTER_AND_MOVE:
3929 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3930 sa_res_key, aptpl, unreg);
3933 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3934 " action: 0x%02x\n", cdb[1] & 0x1f);
3935 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3942 task->task_scsi_status = GOOD;
3943 transport_complete_task(task, 1);
3949 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3951 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3953 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3955 struct se_device *se_dev = cmd->se_dev;
3956 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
3957 struct t10_pr_registration *pr_reg;
3959 u32 add_len = 0, off = 8;
3961 if (cmd->data_length < 8) {
3962 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3963 " too small\n", cmd->data_length);
3964 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3968 buf = transport_kmap_data_sg(cmd);
3969 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3970 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3971 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3972 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
3974 spin_lock(&su_dev->t10_pr.registration_lock);
3975 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
3978 * Check for overflow of 8byte PRI READ_KEYS payload and
3979 * next reservation key list descriptor.
3981 if ((add_len + 8) > (cmd->data_length - 8))
3984 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3985 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3986 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3987 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3988 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3989 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3990 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3991 buf[off++] = (pr_reg->pr_res_key & 0xff);
3995 spin_unlock(&su_dev->t10_pr.registration_lock);
3997 buf[4] = ((add_len >> 24) & 0xff);
3998 buf[5] = ((add_len >> 16) & 0xff);
3999 buf[6] = ((add_len >> 8) & 0xff);
4000 buf[7] = (add_len & 0xff);
4002 transport_kunmap_data_sg(cmd);
4008 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4010 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4012 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4014 struct se_device *se_dev = cmd->se_dev;
4015 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4016 struct t10_pr_registration *pr_reg;
4019 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4021 if (cmd->data_length < 8) {
4022 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4023 " too small\n", cmd->data_length);
4024 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4028 buf = transport_kmap_data_sg(cmd);
4029 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4030 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4031 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4032 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4034 spin_lock(&se_dev->dev_reservation_lock);
4035 pr_reg = se_dev->dev_pr_res_holder;
4038 * Set the hardcoded Additional Length
4040 buf[4] = ((add_len >> 24) & 0xff);
4041 buf[5] = ((add_len >> 16) & 0xff);
4042 buf[6] = ((add_len >> 8) & 0xff);
4043 buf[7] = (add_len & 0xff);
4045 if (cmd->data_length < 22)
4049 * Set the Reservation key.
4051 * From spc4r17, section 5.7.10:
4052 * A persistent reservation holder has its reservation key
4053 * returned in the parameter data from a PERSISTENT
4054 * RESERVE IN command with READ RESERVATION service action as
4056 * a) For a persistent reservation of the type Write Exclusive
4057 * - All Registrants or Exclusive Access  All Regitrants,
4058 * the reservation key shall be set to zero; or
4059 * b) For all other persistent reservation types, the
4060 * reservation key shall be set to the registered
4061 * reservation key for the I_T nexus that holds the
4062 * persistent reservation.
4064 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4065 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4068 pr_res_key = pr_reg->pr_res_key;
4070 buf[8] = ((pr_res_key >> 56) & 0xff);
4071 buf[9] = ((pr_res_key >> 48) & 0xff);
4072 buf[10] = ((pr_res_key >> 40) & 0xff);
4073 buf[11] = ((pr_res_key >> 32) & 0xff);
4074 buf[12] = ((pr_res_key >> 24) & 0xff);
4075 buf[13] = ((pr_res_key >> 16) & 0xff);
4076 buf[14] = ((pr_res_key >> 8) & 0xff);
4077 buf[15] = (pr_res_key & 0xff);
4079 * Set the SCOPE and TYPE
4081 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4082 (pr_reg->pr_res_type & 0x0f);
4086 spin_unlock(&se_dev->dev_reservation_lock);
4087 transport_kunmap_data_sg(cmd);
4093 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4095 * See spc4r17 section 6.13.4 Table 165
4097 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4099 struct se_device *dev = cmd->se_dev;
4100 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
4102 u16 add_len = 8; /* Hardcoded to 8. */
4104 if (cmd->data_length < 6) {
4105 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4106 " %u too small\n", cmd->data_length);
4107 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4111 buf = transport_kmap_data_sg(cmd);
4113 buf[0] = ((add_len << 8) & 0xff);
4114 buf[1] = (add_len & 0xff);
4115 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4116 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4117 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4118 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4120 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4121 * set the TMV: Task Mask Valid bit.
4125 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4127 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4129 * PTPL_A: Persistence across Target Power Loss Active bit
4131 if (pr_tmpl->pr_aptpl_active)
4134 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4136 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4137 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4138 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4139 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4140 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4141 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4143 transport_kunmap_data_sg(cmd);
4149 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4151 * See spc4r17 section 6.13.5 Table 168 and 169
4153 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4155 struct se_device *se_dev = cmd->se_dev;
4156 struct se_node_acl *se_nacl;
4157 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4158 struct se_portal_group *se_tpg;
4159 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4160 struct t10_reservation *pr_tmpl = &se_dev->se_sub_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] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4176 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4177 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4178 buf[3] = (su_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 IDENTIFER */
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_task *task)
4301 struct se_cmd *cmd = task->task_se_cmd;
4305 * Following spc2r20 5.5.1 Reservations overview:
4307 * If a logical unit has been reserved by any RESERVE command and is
4308 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4309 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4310 * initiator or service action and shall terminate with a RESERVATION
4313 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4314 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4315 " SPC-2 reservation is held, returning"
4316 " RESERVATION_CONFLICT\n");
4317 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4321 switch (cmd->t_task_cdb[1] & 0x1f) {
4323 ret = core_scsi3_pri_read_keys(cmd);
4325 case PRI_READ_RESERVATION:
4326 ret = core_scsi3_pri_read_reservation(cmd);
4328 case PRI_REPORT_CAPABILITIES:
4329 ret = core_scsi3_pri_report_capabilities(cmd);
4331 case PRI_READ_FULL_STATUS:
4332 ret = core_scsi3_pri_read_full_status(cmd);
4335 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4336 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4337 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4343 task->task_scsi_status = GOOD;
4344 transport_complete_task(task, 1);
4349 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4354 static int core_pt_seq_non_holder(
4362 int core_setup_reservations(struct se_device *dev, int force_pt)
4364 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
4365 struct t10_reservation *rest = &su_dev->t10_pr;
4367 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4368 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4369 * cause a problem because libata and some SATA RAID HBAs appear
4370 * under Linux/SCSI, but to emulate reservations themselves.
4372 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4373 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
4374 rest->res_type = SPC_PASSTHROUGH;
4375 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4376 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4377 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4378 " emulation\n", dev->transport->name);
4382 * If SPC-3 or above is reported by real or emulated struct se_device,
4383 * use emulated Persistent Reservations.
4385 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4386 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4387 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4388 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4389 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4390 " emulation\n", dev->transport->name);
4392 rest->res_type = SPC2_RESERVATIONS;
4393 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4394 rest->pr_ops.t10_seq_non_holder =
4395 &core_scsi2_reservation_seq_non_holder;
4396 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4397 dev->transport->name);