1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * (c) Copyright 2009-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/vmalloc.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
34 #include <scsi/scsi_proto.h>
35 #include <asm/unaligned.h>
37 #include <target/target_core_base.h>
38 #include <target/target_core_backend.h>
39 #include <target/target_core_fabric.h>
41 #include "target_core_internal.h"
42 #include "target_core_pr.h"
43 #include "target_core_ua.h"
46 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
48 struct pr_transport_id_holder {
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
56 void core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
61 if (!pr_reg->isid_present_at_reg) {
66 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
71 REGISTER_AND_IGNORE_EXISTING_KEY,
80 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
81 struct t10_pr_registration *, int, int);
83 static int is_reservation_holder(
84 struct t10_pr_registration *pr_res_holder,
85 struct t10_pr_registration *pr_reg)
90 pr_res_type = pr_res_holder->pr_res_type;
92 return pr_res_holder == pr_reg ||
93 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
94 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
100 target_scsi2_reservation_check(struct se_cmd *cmd)
102 struct se_device *dev = cmd->se_dev;
103 struct se_session *sess = cmd->se_sess;
105 switch (cmd->t_task_cdb[0]) {
114 if (!dev->reservation_holder || !sess)
117 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
118 return TCM_RESERVATION_CONFLICT;
120 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
121 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
122 return TCM_RESERVATION_CONFLICT;
128 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
129 struct se_node_acl *, struct se_session *);
130 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
132 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
134 struct se_session *se_sess = cmd->se_sess;
135 struct se_device *dev = cmd->se_dev;
136 struct t10_pr_registration *pr_reg;
137 struct t10_reservation *pr_tmpl = &dev->t10_pr;
140 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
144 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
147 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
148 * status, but no reservation shall be established and the
149 * persistent reservation shall not be changed, if the command
150 * is received from a) and b) below.
152 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
153 * status, but the persistent reservation shall not be released,
154 * if the command is received from a) and b)
156 * a) An I_T nexus that is a persistent reservation holder; or
157 * b) An I_T nexus that is registered if a registrants only or
158 * all registrants type persistent reservation is present.
160 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
161 * RELEASE(6) command, or RELEASE(10) command shall be processed
162 * as defined in SPC-2.
164 if (pr_reg->pr_res_holder) {
165 core_scsi3_put_pr_reg(pr_reg);
168 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
169 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
170 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
171 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
172 core_scsi3_put_pr_reg(pr_reg);
175 core_scsi3_put_pr_reg(pr_reg);
179 * Following spc2r20 5.5.1 Reservations overview:
181 * If a logical unit has executed a PERSISTENT RESERVE OUT
182 * command with the REGISTER or the REGISTER AND IGNORE
183 * EXISTING KEY service action and is still registered by any
184 * initiator, all RESERVE commands and all RELEASE commands
185 * regardless of initiator shall conflict and shall terminate
186 * with a RESERVATION CONFLICT status.
188 spin_lock(&pr_tmpl->registration_lock);
189 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
190 spin_unlock(&pr_tmpl->registration_lock);
194 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
195 " while active SPC-3 registrations exist,"
196 " returning RESERVATION_CONFLICT\n");
203 void target_release_reservation(struct se_device *dev)
205 dev->reservation_holder = NULL;
206 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
207 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
208 dev->dev_res_bin_isid = 0;
209 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
214 target_scsi2_reservation_release(struct se_cmd *cmd)
216 struct se_device *dev = cmd->se_dev;
217 struct se_session *sess = cmd->se_sess;
218 struct se_portal_group *tpg;
221 if (!sess || !sess->se_tpg)
223 rc = target_check_scsi2_reservation_conflict(cmd);
227 return TCM_RESERVATION_CONFLICT;
229 spin_lock(&dev->dev_reservation_lock);
230 if (!dev->reservation_holder || !sess)
233 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
236 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
239 target_release_reservation(dev);
241 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
242 " MAPPED LUN: %llu for %s\n",
243 tpg->se_tpg_tfo->fabric_name,
244 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
245 sess->se_node_acl->initiatorname);
248 spin_unlock(&dev->dev_reservation_lock);
250 target_complete_cmd(cmd, GOOD);
255 target_scsi2_reservation_reserve(struct se_cmd *cmd)
257 struct se_device *dev = cmd->se_dev;
258 struct se_session *sess = cmd->se_sess;
259 struct se_portal_group *tpg;
260 sense_reason_t ret = 0;
263 if ((cmd->t_task_cdb[1] & 0x01) &&
264 (cmd->t_task_cdb[1] & 0x02)) {
265 pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
266 return TCM_UNSUPPORTED_SCSI_OPCODE;
269 * This is currently the case for target_core_mod passthrough struct se_cmd
272 if (!sess || !sess->se_tpg)
274 rc = target_check_scsi2_reservation_conflict(cmd);
279 return TCM_RESERVATION_CONFLICT;
282 spin_lock(&dev->dev_reservation_lock);
283 if (dev->reservation_holder &&
284 dev->reservation_holder->se_node_acl != sess->se_node_acl) {
285 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
286 tpg->se_tpg_tfo->fabric_name);
287 pr_err("Original reserver LUN: %llu %s\n",
288 cmd->se_lun->unpacked_lun,
289 dev->reservation_holder->se_node_acl->initiatorname);
290 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
291 " from %s \n", cmd->se_lun->unpacked_lun,
293 sess->se_node_acl->initiatorname);
294 ret = TCM_RESERVATION_CONFLICT;
298 dev->reservation_holder = sess;
299 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
300 if (sess->sess_bin_isid != 0) {
301 dev->dev_res_bin_isid = sess->sess_bin_isid;
302 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
304 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
305 " for %s\n", tpg->se_tpg_tfo->fabric_name,
306 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
307 sess->se_node_acl->initiatorname);
310 spin_unlock(&dev->dev_reservation_lock);
313 target_complete_cmd(cmd, GOOD);
319 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
321 * This function is called by those initiator ports who are *NOT*
322 * the active PR reservation holder when a reservation is present.
324 static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
327 unsigned char *cdb = cmd->t_task_cdb;
328 struct se_session *se_sess = cmd->se_sess;
329 struct se_node_acl *nacl = se_sess->se_node_acl;
331 int registered_nexus = 0, ret = 1; /* Conflict by default */
332 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
333 int we = 0; /* Write Exclusive */
334 int legacy = 0; /* Act like a legacy device and return
335 * RESERVATION CONFLICT on some CDBs */
338 registered_nexus = 0;
340 struct se_dev_entry *se_deve;
343 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
345 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
346 &se_deve->deve_flags);
350 switch (pr_reg_type) {
351 case PR_TYPE_WRITE_EXCLUSIVE:
353 case PR_TYPE_EXCLUSIVE_ACCESS:
355 * Some commands are only allowed for the persistent reservation
359 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
362 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
364 * Some commands are only allowed for registered I_T Nexuses.
368 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
371 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
373 * Each registered I_T Nexus is a reservation holder.
381 * Referenced from spc4r17 table 45 for *NON* PR holder access
384 case SECURITY_PROTOCOL_IN:
385 if (registered_nexus)
393 case RECEIVE_DIAGNOSTIC:
398 if (registered_nexus) {
402 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
404 case PERSISTENT_RESERVE_OUT:
406 * This follows PERSISTENT_RESERVE_OUT service actions that
407 * are allowed in the presence of various reservations.
408 * See spc4r17, table 46
410 switch (cdb[1] & 0x1f) {
413 case PRO_PREEMPT_AND_ABORT:
414 ret = (registered_nexus) ? 0 : 1;
417 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
420 case PRO_REGISTER_AND_MOVE:
425 ret = (registered_nexus) ? 0 : 1;
428 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
429 " action: 0x%02x\n", cdb[1] & 0x1f);
435 /* Handled by CRH=1 in target_scsi2_reservation_release() */
440 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
443 case TEST_UNIT_READY:
444 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
447 switch (cdb[1] & 0x1f) {
448 case MI_MANAGEMENT_PROTOCOL_IN:
449 if (registered_nexus) {
453 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
455 case MI_REPORT_SUPPORTED_OPERATION_CODES:
456 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
461 if (registered_nexus) {
465 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
467 case MI_REPORT_ALIASES:
468 case MI_REPORT_IDENTIFYING_INFORMATION:
469 case MI_REPORT_PRIORITY:
470 case MI_REPORT_TARGET_PGS:
471 case MI_REPORT_TIMESTAMP:
472 ret = 0; /* Allowed */
475 pr_err("Unknown MI Service Action: 0x%02x\n",
480 case ACCESS_CONTROL_IN:
481 case ACCESS_CONTROL_OUT:
484 case SERVICE_ACTION_IN_12:
487 case PERSISTENT_RESERVE_IN:
488 ret = 0; /*/ Allowed CDBs */
495 * Case where the CDB is explicitly allowed in the above switch
498 if (!ret && !other_cdb) {
499 pr_debug("Allowing explicit CDB: 0x%02x for %s"
500 " reservation holder\n", cdb[0],
501 core_scsi3_pr_dump_type(pr_reg_type));
506 * Check if write exclusive initiator ports *NOT* holding the
507 * WRITE_EXCLUSIVE_* reservation.
509 if (we && !registered_nexus) {
510 if (cmd->data_direction == DMA_TO_DEVICE) {
512 * Conflict for write exclusive
514 pr_debug("%s Conflict for unregistered nexus"
515 " %s CDB: 0x%02x to %s reservation\n",
516 transport_dump_cmd_direction(cmd),
517 se_sess->se_node_acl->initiatorname, cdb[0],
518 core_scsi3_pr_dump_type(pr_reg_type));
522 * Allow non WRITE CDBs for all Write Exclusive
523 * PR TYPEs to pass for registered and
524 * non-registered_nexuxes NOT holding the reservation.
526 * We only make noise for the unregisterd nexuses,
527 * as we expect registered non-reservation holding
528 * nexuses to issue CDBs.
531 if (!registered_nexus) {
532 pr_debug("Allowing implicit CDB: 0x%02x"
533 " for %s reservation on unregistered"
535 core_scsi3_pr_dump_type(pr_reg_type));
540 } else if ((reg_only) || (all_reg)) {
541 if (registered_nexus) {
543 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
544 * allow commands from registered nexuses.
547 pr_debug("Allowing implicit CDB: 0x%02x for %s"
548 " reservation\n", cdb[0],
549 core_scsi3_pr_dump_type(pr_reg_type));
553 } else if (we && registered_nexus) {
555 * Reads are allowed for Write Exclusive locks
556 * from all registrants.
558 if (cmd->data_direction == DMA_FROM_DEVICE) {
559 pr_debug("Allowing READ CDB: 0x%02x for %s"
560 " reservation\n", cdb[0],
561 core_scsi3_pr_dump_type(pr_reg_type));
566 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
567 " for %s reservation\n", transport_dump_cmd_direction(cmd),
568 (registered_nexus) ? "" : "un",
569 se_sess->se_node_acl->initiatorname, cdb[0],
570 core_scsi3_pr_dump_type(pr_reg_type));
572 return 1; /* Conflict by default */
575 static sense_reason_t
576 target_scsi3_pr_reservation_check(struct se_cmd *cmd)
578 struct se_device *dev = cmd->se_dev;
579 struct se_session *sess = cmd->se_sess;
581 bool isid_mismatch = false;
583 if (!dev->dev_pr_res_holder)
586 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
587 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
588 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
589 goto check_nonholder;
591 if (dev->dev_pr_res_holder->isid_present_at_reg) {
592 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
593 sess->sess_bin_isid) {
594 isid_mismatch = true;
595 goto check_nonholder;
602 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
603 return TCM_RESERVATION_CONFLICT;
607 static u32 core_scsi3_pr_generation(struct se_device *dev)
612 * PRGeneration field shall contain the value of a 32-bit wrapping
613 * counter mainted by the device server.
615 * Note that this is done regardless of Active Persist across
616 * Target PowerLoss (APTPL)
618 * See spc4r17 section 6.3.12 READ_KEYS service action
620 spin_lock(&dev->dev_reservation_lock);
621 prg = dev->t10_pr.pr_generation++;
622 spin_unlock(&dev->dev_reservation_lock);
627 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
628 struct se_device *dev,
629 struct se_node_acl *nacl,
631 struct se_dev_entry *dest_deve,
638 struct t10_pr_registration *pr_reg;
640 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
642 pr_err("Unable to allocate struct t10_pr_registration\n");
646 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
647 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
648 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
649 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
650 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
651 atomic_set(&pr_reg->pr_res_holders, 0);
652 pr_reg->pr_reg_nacl = nacl;
654 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
655 * the se_dev_entry->pr_ref will have been already obtained by
656 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
658 * Otherwise, locate se_dev_entry now and obtain a reference until
659 * registration completes in __core_scsi3_add_registration().
662 pr_reg->pr_reg_deve = dest_deve;
665 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
666 if (!pr_reg->pr_reg_deve) {
668 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
669 nacl->initiatorname, mapped_lun);
670 kmem_cache_free(t10_pr_reg_cache, pr_reg);
673 kref_get(&pr_reg->pr_reg_deve->pr_kref);
676 pr_reg->pr_res_mapped_lun = mapped_lun;
677 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
678 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
679 pr_reg->pr_res_key = sa_res_key;
680 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
681 pr_reg->pr_reg_aptpl = aptpl;
683 * If an ISID value for this SCSI Initiator Port exists,
684 * save it to the registration now.
687 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
688 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
689 pr_reg->isid_present_at_reg = 1;
695 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
696 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
699 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
702 static struct t10_pr_registration *__core_scsi3_alloc_registration(
703 struct se_device *dev,
704 struct se_node_acl *nacl,
706 struct se_dev_entry *deve,
713 struct se_dev_entry *deve_tmp;
714 struct se_node_acl *nacl_tmp;
715 struct se_lun_acl *lacl_tmp;
716 struct se_lun *lun_tmp, *next, *dest_lun;
717 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
718 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
721 * Create a registration for the I_T Nexus upon which the
722 * PROUT REGISTER was received.
724 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
725 isid, sa_res_key, all_tg_pt,
730 * Return pointer to pr_reg for ALL_TG_PT=0
735 * Create list of matching SCSI Initiator Port registrations
738 spin_lock(&dev->se_port_lock);
739 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
740 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
742 spin_unlock(&dev->se_port_lock);
744 spin_lock(&lun_tmp->lun_deve_lock);
745 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
747 * This pointer will be NULL for demo mode MappedLUNs
748 * that have not been make explicit via a ConfigFS
749 * MappedLUN group for the SCSI Initiator Node ACL.
751 if (!deve_tmp->se_lun_acl)
754 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
755 lockdep_is_held(&lun_tmp->lun_deve_lock));
756 nacl_tmp = lacl_tmp->se_lun_nacl;
758 * Skip the matching struct se_node_acl that is allocated
761 if (nacl == nacl_tmp)
764 * Only perform PR registrations for target ports on
765 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
768 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
771 * Look for a matching Initiator Node ACL in ASCII format
773 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
776 kref_get(&deve_tmp->pr_kref);
777 spin_unlock(&lun_tmp->lun_deve_lock);
779 * Grab a configfs group dependency that is released
780 * for the exception path at label out: below, or upon
781 * completion of adding ALL_TG_PT=1 registrations in
782 * __core_scsi3_add_registration()
784 ret = core_scsi3_lunacl_depend_item(deve_tmp);
786 pr_err("core_scsi3_lunacl_depend"
788 percpu_ref_put(&lun_tmp->lun_ref);
789 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
793 * Located a matching SCSI Initiator Port on a different
794 * port, allocate the pr_reg_atp and attach it to the
795 * pr_reg->pr_reg_atp_list that will be processed once
796 * the original *pr_reg is processed in
797 * __core_scsi3_add_registration()
799 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
800 kref_read(&deve_tmp->pr_kref) != 0);
802 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
803 nacl_tmp, dest_lun, deve_tmp,
804 deve_tmp->mapped_lun, NULL,
805 sa_res_key, all_tg_pt, aptpl);
807 percpu_ref_put(&lun_tmp->lun_ref);
808 core_scsi3_lunacl_undepend_item(deve_tmp);
812 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
813 &pr_reg->pr_reg_atp_list);
814 spin_lock(&lun_tmp->lun_deve_lock);
816 spin_unlock(&lun_tmp->lun_deve_lock);
818 spin_lock(&dev->se_port_lock);
819 percpu_ref_put(&lun_tmp->lun_ref);
821 spin_unlock(&dev->se_port_lock);
825 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
826 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
827 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
828 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
829 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
831 kmem_cache_free(t10_pr_reg_cache, pr_reg);
835 int core_scsi3_alloc_aptpl_registration(
836 struct t10_reservation *pr_tmpl,
838 unsigned char *i_port,
841 unsigned char *t_port,
848 struct t10_pr_registration *pr_reg;
850 if (!i_port || !t_port || !sa_res_key) {
851 pr_err("Illegal parameters for APTPL registration\n");
855 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
857 pr_err("Unable to allocate struct t10_pr_registration\n");
861 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
862 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
863 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
864 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
865 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
866 atomic_set(&pr_reg->pr_res_holders, 0);
867 pr_reg->pr_reg_nacl = NULL;
868 pr_reg->pr_reg_deve = NULL;
869 pr_reg->pr_res_mapped_lun = mapped_lun;
870 pr_reg->pr_aptpl_target_lun = target_lun;
871 pr_reg->pr_res_key = sa_res_key;
872 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
873 pr_reg->pr_reg_aptpl = 1;
874 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
875 pr_reg->pr_res_type = type;
877 * If an ISID value had been saved in APTPL metadata for this
878 * SCSI Initiator Port, restore it now.
881 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
882 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
883 pr_reg->isid_present_at_reg = 1;
886 * Copy the i_port and t_port information from caller.
888 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
889 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
890 pr_reg->pr_reg_tpgt = tpgt;
892 * Set pr_res_holder from caller, the pr_reg who is the reservation
893 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
894 * the Initiator Node LUN ACL from the fabric module is created for
897 pr_reg->pr_res_holder = res_holder;
899 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
900 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
901 " metadata\n", (res_holder) ? "+reservation" : "");
905 static void core_scsi3_aptpl_reserve(
906 struct se_device *dev,
907 struct se_portal_group *tpg,
908 struct se_node_acl *node_acl,
909 struct t10_pr_registration *pr_reg)
911 char i_buf[PR_REG_ISID_ID_LEN];
913 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
914 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
916 spin_lock(&dev->dev_reservation_lock);
917 dev->dev_pr_res_holder = pr_reg;
918 spin_unlock(&dev->dev_reservation_lock);
920 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
921 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
922 tpg->se_tpg_tfo->fabric_name,
923 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
924 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
925 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
926 tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
930 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
931 struct t10_pr_registration *, enum register_type, int);
933 static int __core_scsi3_check_aptpl_registration(
934 struct se_device *dev,
935 struct se_portal_group *tpg,
938 struct se_node_acl *nacl,
941 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
942 struct t10_reservation *pr_tmpl = &dev->t10_pr;
943 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
944 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
947 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
948 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
950 * Copy Initiator Port information from struct se_node_acl
952 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
953 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
954 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
955 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
957 * Look for the matching registrations+reservation from those
958 * created from APTPL metadata. Note that multiple registrations
959 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
962 spin_lock(&pr_tmpl->aptpl_reg_lock);
963 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
966 if (!strcmp(pr_reg->pr_iport, i_port) &&
967 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
968 !(strcmp(pr_reg->pr_tport, t_port)) &&
969 (pr_reg->pr_reg_tpgt == tpgt) &&
970 (pr_reg->pr_aptpl_target_lun == target_lun)) {
972 * Obtain the ->pr_reg_deve pointer + reference, that
973 * is released by __core_scsi3_add_registration() below.
976 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
977 if (!pr_reg->pr_reg_deve) {
978 pr_err("Unable to locate PR APTPL %s mapped_lun:"
979 " %llu\n", nacl->initiatorname, mapped_lun);
983 kref_get(&pr_reg->pr_reg_deve->pr_kref);
986 pr_reg->pr_reg_nacl = nacl;
987 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
988 list_del(&pr_reg->pr_reg_aptpl_list);
989 spin_unlock(&pr_tmpl->aptpl_reg_lock);
991 * At this point all of the pointers in *pr_reg will
992 * be setup, so go ahead and add the registration.
994 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
996 * If this registration is the reservation holder,
997 * make that happen now..
999 if (pr_reg->pr_res_holder)
1000 core_scsi3_aptpl_reserve(dev, tpg,
1003 * Reenable pr_aptpl_active to accept new metadata
1004 * updates once the SCSI device is active again..
1006 spin_lock(&pr_tmpl->aptpl_reg_lock);
1007 pr_tmpl->pr_aptpl_active = 1;
1010 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1015 int core_scsi3_check_aptpl_registration(
1016 struct se_device *dev,
1017 struct se_portal_group *tpg,
1019 struct se_node_acl *nacl,
1022 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1025 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1026 lun->unpacked_lun, nacl,
1030 static void __core_scsi3_dump_registration(
1031 const struct target_core_fabric_ops *tfo,
1032 struct se_device *dev,
1033 struct se_node_acl *nacl,
1034 struct t10_pr_registration *pr_reg,
1035 enum register_type register_type)
1037 struct se_portal_group *se_tpg = nacl->se_tpg;
1038 char i_buf[PR_REG_ISID_ID_LEN];
1040 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1041 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1043 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1044 " Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
1045 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1046 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1048 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1049 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
1050 tfo->tpg_get_tag(se_tpg));
1051 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1052 " Port(s)\n", tfo->fabric_name,
1053 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1054 dev->transport->name);
1055 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1056 " 0x%08x APTPL: %d\n", tfo->fabric_name,
1057 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1058 pr_reg->pr_reg_aptpl);
1061 static void __core_scsi3_add_registration(
1062 struct se_device *dev,
1063 struct se_node_acl *nacl,
1064 struct t10_pr_registration *pr_reg,
1065 enum register_type register_type,
1068 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1069 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1070 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1071 struct se_dev_entry *deve;
1074 * Increment PRgeneration counter for struct se_device upon a successful
1075 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1077 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1078 * action, the struct se_device->dev_reservation_lock will already be held,
1079 * so we do not call core_scsi3_pr_generation() which grabs the lock
1082 pr_reg->pr_res_generation = (register_move) ?
1083 dev->t10_pr.pr_generation++ :
1084 core_scsi3_pr_generation(dev);
1086 spin_lock(&pr_tmpl->registration_lock);
1087 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1089 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1090 spin_unlock(&pr_tmpl->registration_lock);
1092 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1094 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1097 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1098 * allocated in __core_scsi3_alloc_registration()
1100 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1101 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1102 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1104 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1106 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1108 spin_lock(&pr_tmpl->registration_lock);
1109 list_add_tail(&pr_reg_tmp->pr_reg_list,
1110 &pr_tmpl->registration_list);
1112 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1114 spin_unlock(&pr_tmpl->registration_lock);
1116 * Drop configfs group dependency reference and deve->pr_kref
1117 * obtained from __core_scsi3_alloc_registration() code.
1120 deve = pr_reg_tmp->pr_reg_deve;
1122 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1123 core_scsi3_lunacl_undepend_item(deve);
1124 pr_reg_tmp->pr_reg_deve = NULL;
1130 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1133 deve = pr_reg->pr_reg_deve;
1135 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1136 kref_put(&deve->pr_kref, target_pr_kref_release);
1137 pr_reg->pr_reg_deve = NULL;
1142 static int core_scsi3_alloc_registration(
1143 struct se_device *dev,
1144 struct se_node_acl *nacl,
1146 struct se_dev_entry *deve,
1148 unsigned char *isid,
1152 enum register_type register_type,
1155 struct t10_pr_registration *pr_reg;
1157 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1158 isid, sa_res_key, all_tg_pt,
1163 __core_scsi3_add_registration(dev, nacl, pr_reg,
1164 register_type, register_move);
1168 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1169 struct se_device *dev,
1170 struct se_node_acl *nacl,
1171 unsigned char *isid)
1173 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1174 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1175 struct se_portal_group *tpg;
1177 spin_lock(&pr_tmpl->registration_lock);
1178 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1179 &pr_tmpl->registration_list, pr_reg_list) {
1181 * First look for a matching struct se_node_acl
1183 if (pr_reg->pr_reg_nacl != nacl)
1186 tpg = pr_reg->pr_reg_nacl->se_tpg;
1188 * If this registration does NOT contain a fabric provided
1189 * ISID, then we have found a match.
1191 if (!pr_reg->isid_present_at_reg) {
1193 * Determine if this SCSI device server requires that
1194 * SCSI Intiatior TransportID w/ ISIDs is enforced
1195 * for fabric modules (iSCSI) requiring them.
1197 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1198 if (dev->dev_attrib.enforce_pr_isids)
1201 atomic_inc_mb(&pr_reg->pr_res_holders);
1202 spin_unlock(&pr_tmpl->registration_lock);
1206 * If the *pr_reg contains a fabric defined ISID for multi-value
1207 * SCSI Initiator Port TransportIDs, then we expect a valid
1208 * matching ISID to be provided by the local SCSI Initiator Port.
1212 if (strcmp(isid, pr_reg->pr_reg_isid))
1215 atomic_inc_mb(&pr_reg->pr_res_holders);
1216 spin_unlock(&pr_tmpl->registration_lock);
1219 spin_unlock(&pr_tmpl->registration_lock);
1224 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1225 struct se_device *dev,
1226 struct se_node_acl *nacl,
1227 struct se_session *sess)
1229 struct se_portal_group *tpg = nacl->se_tpg;
1230 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1232 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1233 memset(&buf[0], 0, PR_REG_ISID_LEN);
1234 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1239 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1242 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1244 atomic_dec_mb(&pr_reg->pr_res_holders);
1247 static int core_scsi3_check_implicit_release(
1248 struct se_device *dev,
1249 struct t10_pr_registration *pr_reg)
1251 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1252 struct t10_pr_registration *pr_res_holder;
1255 spin_lock(&dev->dev_reservation_lock);
1256 pr_res_holder = dev->dev_pr_res_holder;
1257 if (!pr_res_holder) {
1258 spin_unlock(&dev->dev_reservation_lock);
1261 if (pr_res_holder == pr_reg) {
1263 * Perform an implicit RELEASE if the registration that
1264 * is being released is holding the reservation.
1266 * From spc4r17, section 5.7.11.1:
1268 * e) If the I_T nexus is the persistent reservation holder
1269 * and the persistent reservation is not an all registrants
1270 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1271 * service action or REGISTER AND IGNORE EXISTING KEY
1272 * service action with the SERVICE ACTION RESERVATION KEY
1273 * field set to zero (see 5.7.11.3).
1275 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1278 * For 'All Registrants' reservation types, all existing
1279 * registrations are still processed as reservation holders
1280 * in core_scsi3_pr_seq_non_holder() after the initial
1281 * reservation holder is implicitly released here.
1283 } else if (pr_reg->pr_reg_all_tg_pt &&
1284 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1285 pr_reg->pr_reg_nacl->initiatorname)) &&
1286 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1287 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1288 " UNREGISTER while existing reservation with matching"
1289 " key 0x%016Lx is present from another SCSI Initiator"
1290 " Port\n", pr_reg->pr_res_key);
1293 spin_unlock(&dev->dev_reservation_lock);
1298 static void __core_scsi3_free_registration(
1299 struct se_device *dev,
1300 struct t10_pr_registration *pr_reg,
1301 struct list_head *preempt_and_abort_list,
1303 __releases(&pr_tmpl->registration_lock)
1304 __acquires(&pr_tmpl->registration_lock)
1306 const struct target_core_fabric_ops *tfo =
1307 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1308 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1309 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1310 struct se_dev_entry *deve;
1311 char i_buf[PR_REG_ISID_ID_LEN];
1313 lockdep_assert_held(&pr_tmpl->registration_lock);
1315 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1316 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1318 if (!list_empty(&pr_reg->pr_reg_list))
1319 list_del(&pr_reg->pr_reg_list);
1321 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1322 * so call core_scsi3_put_pr_reg() to decrement our reference.
1325 core_scsi3_put_pr_reg(pr_reg);
1327 spin_unlock(&pr_tmpl->registration_lock);
1329 * Wait until all reference from any other I_T nexuses for this
1330 * *pr_reg have been released. Because list_del() is called above,
1331 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1332 * count back to zero, and we release *pr_reg.
1334 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1335 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1341 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1343 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1346 spin_lock(&pr_tmpl->registration_lock);
1347 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1348 " Node: %s%s\n", tfo->fabric_name,
1349 pr_reg->pr_reg_nacl->initiatorname,
1351 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1352 " Port(s)\n", tfo->fabric_name,
1353 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1354 dev->transport->name);
1355 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1356 " 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
1357 pr_reg->pr_res_generation);
1359 if (!preempt_and_abort_list) {
1360 pr_reg->pr_reg_deve = NULL;
1361 pr_reg->pr_reg_nacl = NULL;
1362 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1366 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1367 * are released once the ABORT_TASK_SET has completed..
1369 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1372 void core_scsi3_free_pr_reg_from_nacl(
1373 struct se_device *dev,
1374 struct se_node_acl *nacl)
1376 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1377 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1378 bool free_reg = false;
1380 * If the passed se_node_acl matches the reservation holder,
1381 * release the reservation.
1383 spin_lock(&dev->dev_reservation_lock);
1384 pr_res_holder = dev->dev_pr_res_holder;
1385 if ((pr_res_holder != NULL) &&
1386 (pr_res_holder->pr_reg_nacl == nacl)) {
1387 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1390 spin_unlock(&dev->dev_reservation_lock);
1392 * Release any registration associated with the struct se_node_acl.
1394 spin_lock(&pr_tmpl->registration_lock);
1395 if (pr_res_holder && free_reg)
1396 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1398 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1399 &pr_tmpl->registration_list, pr_reg_list) {
1401 if (pr_reg->pr_reg_nacl != nacl)
1404 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1406 spin_unlock(&pr_tmpl->registration_lock);
1409 void core_scsi3_free_all_registrations(
1410 struct se_device *dev)
1412 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1413 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1415 spin_lock(&dev->dev_reservation_lock);
1416 pr_res_holder = dev->dev_pr_res_holder;
1417 if (pr_res_holder != NULL) {
1418 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1419 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1420 pr_res_holder, 0, 0);
1422 spin_unlock(&dev->dev_reservation_lock);
1424 spin_lock(&pr_tmpl->registration_lock);
1425 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1426 &pr_tmpl->registration_list, pr_reg_list) {
1428 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1430 spin_unlock(&pr_tmpl->registration_lock);
1432 spin_lock(&pr_tmpl->aptpl_reg_lock);
1433 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1434 pr_reg_aptpl_list) {
1435 list_del(&pr_reg->pr_reg_aptpl_list);
1436 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1438 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1441 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1443 return target_depend_item(&tpg->tpg_group.cg_item);
1446 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1448 target_undepend_item(&tpg->tpg_group.cg_item);
1449 atomic_dec_mb(&tpg->tpg_pr_ref_count);
1452 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1454 if (nacl->dynamic_node_acl)
1456 return target_depend_item(&nacl->acl_group.cg_item);
1459 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1461 if (!nacl->dynamic_node_acl)
1462 target_undepend_item(&nacl->acl_group.cg_item);
1463 atomic_dec_mb(&nacl->acl_pr_ref_count);
1466 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1468 struct se_lun_acl *lun_acl;
1471 * For nacl->dynamic_node_acl=1
1473 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1474 kref_read(&se_deve->pr_kref) != 0);
1478 return target_depend_item(&lun_acl->se_lun_group.cg_item);
1481 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1483 struct se_lun_acl *lun_acl;
1486 * For nacl->dynamic_node_acl=1
1488 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1489 kref_read(&se_deve->pr_kref) != 0);
1491 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1495 target_undepend_item(&lun_acl->se_lun_group.cg_item);
1496 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1499 static sense_reason_t
1500 core_scsi3_decode_spec_i_port(
1502 struct se_portal_group *tpg,
1503 unsigned char *l_isid,
1508 struct se_device *dev = cmd->se_dev;
1509 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1510 struct se_session *se_sess = cmd->se_sess;
1511 struct se_node_acl *dest_node_acl = NULL;
1512 struct se_dev_entry *dest_se_deve = NULL;
1513 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1514 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1515 LIST_HEAD(tid_dest_list);
1516 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1517 unsigned char *buf, *ptr, proto_ident;
1518 const unsigned char *i_str = NULL;
1519 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1521 u32 tpdl, tid_len = 0;
1525 * Allocate a struct pr_transport_id_holder and setup the
1526 * local_node_acl pointer and add to struct list_head tid_dest_list
1527 * for add registration processing in the loop of tid_dest_list below.
1529 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1531 pr_err("Unable to allocate tidh_new\n");
1532 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1534 INIT_LIST_HEAD(&tidh_new->dest_list);
1535 tidh_new->dest_tpg = tpg;
1536 tidh_new->dest_node_acl = se_sess->se_node_acl;
1538 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1539 se_sess->se_node_acl, cmd->se_lun,
1540 NULL, cmd->orig_fe_lun, l_isid,
1541 sa_res_key, all_tg_pt, aptpl);
1542 if (!local_pr_reg) {
1544 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1546 tidh_new->dest_pr_reg = local_pr_reg;
1548 * The local I_T nexus does not hold any configfs dependances,
1549 * so we set tidh_new->dest_se_deve to NULL to prevent the
1550 * configfs_undepend_item() calls in the tid_dest_list loops below.
1552 tidh_new->dest_se_deve = NULL;
1553 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1555 if (cmd->data_length < 28) {
1556 pr_warn("SPC-PR: Received PR OUT parameter list"
1557 " length too small: %u\n", cmd->data_length);
1558 ret = TCM_INVALID_PARAMETER_LIST;
1562 buf = transport_kmap_data_sg(cmd);
1564 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1569 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1570 * first extract TransportID Parameter Data Length, and make sure
1571 * the value matches up to the SCSI expected data transfer length.
1573 tpdl = get_unaligned_be32(&buf[24]);
1575 if ((tpdl + 28) != cmd->data_length) {
1576 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1577 " does not equal CDB data_length: %u\n", tpdl,
1579 ret = TCM_INVALID_PARAMETER_LIST;
1583 * Start processing the received transport IDs using the
1584 * receiving I_T Nexus portal's fabric dependent methods to
1585 * obtain the SCSI Initiator Port/Device Identifiers.
1590 struct se_lun *dest_lun, *tmp_lun;
1592 proto_ident = (ptr[0] & 0x0f);
1595 spin_lock(&dev->se_port_lock);
1596 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1597 tmp_tpg = tmp_lun->lun_tpg;
1600 * Look for the matching proto_ident provided by
1601 * the received TransportID
1603 if (tmp_tpg->proto_id != proto_ident)
1605 dest_rtpi = tmp_lun->lun_rtpi;
1607 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1608 ptr, &tid_len, &iport_ptr);
1612 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1613 spin_unlock(&dev->se_port_lock);
1615 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1616 pr_err(" core_scsi3_tpg_depend_item()"
1618 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1619 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1623 * Locate the destination initiator ACL to be registered
1624 * from the decoded fabric module specific TransportID
1627 mutex_lock(&tmp_tpg->acl_node_mutex);
1628 dest_node_acl = __core_tpg_get_initiator_node_acl(
1631 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1632 mutex_unlock(&tmp_tpg->acl_node_mutex);
1634 if (!dest_node_acl) {
1635 core_scsi3_tpg_undepend_item(tmp_tpg);
1636 spin_lock(&dev->se_port_lock);
1640 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1641 pr_err("configfs_depend_item() failed"
1642 " for dest_node_acl->acl_group\n");
1643 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1644 core_scsi3_tpg_undepend_item(tmp_tpg);
1645 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1650 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1651 " %s Port RTPI: %hu\n",
1652 dest_tpg->se_tpg_tfo->fabric_name,
1653 dest_node_acl->initiatorname, dest_rtpi);
1655 spin_lock(&dev->se_port_lock);
1658 spin_unlock(&dev->se_port_lock);
1661 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1663 ret = TCM_INVALID_PARAMETER_LIST;
1667 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1668 " tid_len: %d for %s + %s\n",
1669 dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
1670 tpdl, tid_len, i_str, iport_ptr);
1672 if (tid_len > tpdl) {
1673 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1674 " %u for Transport ID: %s\n", tid_len, ptr);
1675 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1676 core_scsi3_tpg_undepend_item(dest_tpg);
1677 ret = TCM_INVALID_PARAMETER_LIST;
1681 * Locate the desintation struct se_dev_entry pointer for matching
1682 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1685 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1687 if (!dest_se_deve) {
1688 pr_err("Unable to locate %s dest_se_deve"
1689 " from destination RTPI: %hu\n",
1690 dest_tpg->se_tpg_tfo->fabric_name,
1693 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1694 core_scsi3_tpg_undepend_item(dest_tpg);
1695 ret = TCM_INVALID_PARAMETER_LIST;
1699 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1700 pr_err("core_scsi3_lunacl_depend_item()"
1702 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1703 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1704 core_scsi3_tpg_undepend_item(dest_tpg);
1705 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1709 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1710 " dest_se_deve mapped_lun: %llu\n",
1711 dest_tpg->se_tpg_tfo->fabric_name,
1712 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1715 * Skip any TransportIDs that already have a registration for
1718 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1721 core_scsi3_put_pr_reg(pr_reg_e);
1722 core_scsi3_lunacl_undepend_item(dest_se_deve);
1723 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1724 core_scsi3_tpg_undepend_item(dest_tpg);
1731 * Allocate a struct pr_transport_id_holder and setup
1732 * the dest_node_acl and dest_se_deve pointers for the
1735 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1738 pr_err("Unable to allocate tidh_new\n");
1739 core_scsi3_lunacl_undepend_item(dest_se_deve);
1740 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1741 core_scsi3_tpg_undepend_item(dest_tpg);
1742 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1745 INIT_LIST_HEAD(&tidh_new->dest_list);
1746 tidh_new->dest_tpg = dest_tpg;
1747 tidh_new->dest_node_acl = dest_node_acl;
1748 tidh_new->dest_se_deve = dest_se_deve;
1751 * Allocate, but do NOT add the registration for the
1752 * TransportID referenced SCSI Initiator port. This
1753 * done because of the following from spc4r17 in section
1754 * 6.14.3 wrt SPEC_I_PT:
1756 * "If a registration fails for any initiator port (e.g., if th
1757 * logical unit does not have enough resources available to
1758 * hold the registration information), no registrations shall be
1759 * made, and the command shall be terminated with
1760 * CHECK CONDITION status."
1762 * That means we call __core_scsi3_alloc_registration() here,
1763 * and then call __core_scsi3_add_registration() in the
1764 * 2nd loop which will never fail.
1766 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
1767 kref_read(&dest_se_deve->pr_kref) != 0);
1769 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1770 dest_node_acl, dest_lun, dest_se_deve,
1771 dest_se_deve->mapped_lun, iport_ptr,
1772 sa_res_key, all_tg_pt, aptpl);
1774 core_scsi3_lunacl_undepend_item(dest_se_deve);
1775 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1776 core_scsi3_tpg_undepend_item(dest_tpg);
1778 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1781 tidh_new->dest_pr_reg = dest_pr_reg;
1782 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1790 transport_kunmap_data_sg(cmd);
1793 * Go ahead and create a registrations from tid_dest_list for the
1794 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1797 * The SA Reservation Key from the PROUT is set for the
1798 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1799 * means that the TransportID Initiator port will be
1800 * registered on all of the target ports in the SCSI target device
1801 * ALL_TG_PT=0 means the registration will only be for the
1802 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1805 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1806 dest_tpg = tidh->dest_tpg;
1807 dest_node_acl = tidh->dest_node_acl;
1808 dest_se_deve = tidh->dest_se_deve;
1809 dest_pr_reg = tidh->dest_pr_reg;
1811 list_del(&tidh->dest_list);
1814 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1815 core_pr_dump_initiator_port(dest_pr_reg, i_buf, 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 " %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
1823 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1824 dest_se_deve->mapped_lun : 0);
1826 if (!dest_se_deve) {
1827 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1828 target_pr_kref_release);
1831 core_scsi3_lunacl_undepend_item(dest_se_deve);
1832 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1833 core_scsi3_tpg_undepend_item(dest_tpg);
1838 transport_kunmap_data_sg(cmd);
1841 * For the failure case, release everything from tid_dest_list
1842 * including *dest_pr_reg and the configfs dependances..
1844 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1845 dest_tpg = tidh->dest_tpg;
1846 dest_node_acl = tidh->dest_node_acl;
1847 dest_se_deve = tidh->dest_se_deve;
1848 dest_pr_reg = tidh->dest_pr_reg;
1850 list_del(&tidh->dest_list);
1853 * Release any extra ALL_TG_PT=1 registrations for
1854 * the SPEC_I_PT=1 case.
1856 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1857 &dest_pr_reg->pr_reg_atp_list,
1858 pr_reg_atp_mem_list) {
1859 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1860 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1861 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1864 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1866 if (!dest_se_deve) {
1867 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1868 target_pr_kref_release);
1871 core_scsi3_lunacl_undepend_item(dest_se_deve);
1872 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1873 core_scsi3_tpg_undepend_item(dest_tpg);
1878 static int core_scsi3_update_aptpl_buf(
1879 struct se_device *dev,
1881 u32 pr_aptpl_buf_len)
1883 struct se_portal_group *tpg;
1884 struct t10_pr_registration *pr_reg;
1885 unsigned char tmp[512], isid_buf[32];
1890 spin_lock(&dev->dev_reservation_lock);
1891 spin_lock(&dev->t10_pr.registration_lock);
1893 * Walk the registration list..
1895 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1900 tpg = pr_reg->pr_reg_nacl->se_tpg;
1902 * Write out any ISID value to APTPL metadata that was included
1903 * in the original registration.
1905 if (pr_reg->isid_present_at_reg)
1906 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1907 pr_reg->pr_reg_isid);
1909 * Include special metadata if the pr_reg matches the
1910 * reservation holder.
1912 if (dev->dev_pr_res_holder == pr_reg) {
1913 snprintf(tmp, 512, "PR_REG_START: %d"
1914 "\ninitiator_fabric=%s\n"
1915 "initiator_node=%s\n%s"
1917 "res_holder=1\nres_type=%02x\n"
1918 "res_scope=%02x\nres_all_tg_pt=%d\n"
1919 "mapped_lun=%llu\n", reg_count,
1920 tpg->se_tpg_tfo->fabric_name,
1921 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1922 pr_reg->pr_res_key, pr_reg->pr_res_type,
1923 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1924 pr_reg->pr_res_mapped_lun);
1926 snprintf(tmp, 512, "PR_REG_START: %d\n"
1927 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1928 "sa_res_key=%llu\nres_holder=0\n"
1929 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
1930 reg_count, tpg->se_tpg_tfo->fabric_name,
1931 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1932 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1933 pr_reg->pr_res_mapped_lun);
1936 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1937 pr_err("Unable to update renaming APTPL metadata,"
1938 " reallocating larger buffer\n");
1942 len += sprintf(buf+len, "%s", tmp);
1945 * Include information about the associated SCSI target port.
1947 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1948 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
1949 " %d\n", tpg->se_tpg_tfo->fabric_name,
1950 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1951 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1952 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1955 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1956 pr_err("Unable to update renaming APTPL metadata,"
1957 " reallocating larger buffer\n");
1961 len += sprintf(buf+len, "%s", tmp);
1966 len += sprintf(buf+len, "No Registrations or Reservations");
1969 spin_unlock(&dev->t10_pr.registration_lock);
1970 spin_unlock(&dev->dev_reservation_lock);
1975 static int __core_scsi3_write_aptpl_to_file(
1976 struct se_device *dev,
1979 struct t10_wwn *wwn = &dev->t10_wwn;
1981 int flags = O_RDWR | O_CREAT | O_TRUNC;
1983 u32 pr_aptpl_buf_len;
1987 path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
1988 &wwn->unit_serial[0]);
1992 file = filp_open(path, flags, 0600);
1994 pr_err("filp_open(%s) for APTPL metadata"
1997 return PTR_ERR(file);
2000 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
2002 ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
2005 pr_debug("Error writing APTPL metadata file: %s\n", path);
2009 return (ret < 0) ? -EIO : 0;
2013 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2014 * write out the updated metadata to struct file for this SCSI device.
2016 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
2019 int rc, len = PR_APTPL_BUF_LEN;
2022 char *null_buf = "No Registrations or Reservations\n";
2024 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2025 dev->t10_pr.pr_aptpl_active = 0;
2026 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
2029 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2036 return TCM_OUT_OF_RESOURCES;
2038 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
2045 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2047 pr_err("SPC-3 PR: Could not update APTPL\n");
2049 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2051 dev->t10_pr.pr_aptpl_active = 1;
2053 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2057 static sense_reason_t
2058 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2059 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2061 struct se_session *se_sess = cmd->se_sess;
2062 struct se_device *dev = cmd->se_dev;
2063 struct se_lun *se_lun = cmd->se_lun;
2064 struct se_portal_group *se_tpg;
2065 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2066 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2067 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2068 sense_reason_t ret = TCM_NO_SENSE;
2069 int pr_holder = 0, type;
2071 if (!se_sess || !se_lun) {
2072 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2073 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2075 se_tpg = se_sess->se_tpg;
2077 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2078 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2079 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2081 isid_ptr = &isid_buf[0];
2084 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2086 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2089 pr_warn("SPC-3 PR: Reservation Key non-zero"
2090 " for SA REGISTER, returning CONFLICT\n");
2091 return TCM_RESERVATION_CONFLICT;
2094 * Do nothing but return GOOD status.
2101 * Perform the Service Action REGISTER on the Initiator
2102 * Port Endpoint that the PRO was received from on the
2103 * Logical Unit of the SCSI device server.
2105 if (core_scsi3_alloc_registration(cmd->se_dev,
2106 se_sess->se_node_acl, cmd->se_lun,
2107 NULL, cmd->orig_fe_lun, isid_ptr,
2108 sa_res_key, all_tg_pt, aptpl,
2109 register_type, 0)) {
2110 pr_err("Unable to allocate"
2111 " struct t10_pr_registration\n");
2112 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
2116 * Register both the Initiator port that received
2117 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2118 * TransportID from Parameter list and loop through
2119 * fabric dependent parameter list while calling
2120 * logic from of core_scsi3_alloc_registration() for
2121 * each TransportID provided SCSI Initiator Port/Device
2123 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2124 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2128 return core_scsi3_update_and_write_aptpl(dev, aptpl);
2131 /* ok, existing registration */
2133 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2134 pr_err("SPC-3 PR REGISTER: Received"
2135 " res_key: 0x%016Lx does not match"
2136 " existing SA REGISTER res_key:"
2137 " 0x%016Lx\n", res_key,
2138 pr_reg->pr_res_key);
2139 ret = TCM_RESERVATION_CONFLICT;
2144 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2145 " set on a registered nexus\n");
2146 ret = TCM_INVALID_PARAMETER_LIST;
2151 * An existing ALL_TG_PT=1 registration being released
2152 * must also set ALL_TG_PT=1 in the incoming PROUT.
2154 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2155 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2156 " registration exists, but ALL_TG_PT=1 bit not"
2157 " present in received PROUT\n");
2158 ret = TCM_INVALID_CDB_FIELD;
2163 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2167 * Increment PRgeneration counter for struct se_device"
2168 * upon a successful REGISTER, see spc4r17 section 6.3.2
2169 * READ_KEYS service action.
2171 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2172 pr_reg->pr_res_key = sa_res_key;
2173 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2174 " Key for %s to: 0x%016Lx PRgeneration:"
2175 " 0x%08x\n", cmd->se_tfo->fabric_name,
2176 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2177 pr_reg->pr_reg_nacl->initiatorname,
2178 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2182 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2184 type = pr_reg->pr_res_type;
2185 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2187 if (pr_holder < 0) {
2188 ret = TCM_RESERVATION_CONFLICT;
2192 spin_lock(&pr_tmpl->registration_lock);
2194 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2195 * and matching pr_res_key.
2197 if (pr_reg->pr_reg_all_tg_pt) {
2198 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2199 &pr_tmpl->registration_list,
2202 if (!pr_reg_p->pr_reg_all_tg_pt)
2204 if (pr_reg_p->pr_res_key != res_key)
2206 if (pr_reg == pr_reg_p)
2208 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2209 pr_reg_p->pr_reg_nacl->initiatorname))
2212 __core_scsi3_free_registration(dev,
2218 * Release the calling I_T Nexus registration now..
2220 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2224 * From spc4r17, section 5.7.11.3 Unregistering
2226 * If the persistent reservation is a registrants only
2227 * type, the device server shall establish a unit
2228 * attention condition for the initiator port associated
2229 * with every registered I_T nexus except for the I_T
2230 * nexus on which the PERSISTENT RESERVE OUT command was
2231 * received, with the additional sense code set to
2232 * RESERVATIONS RELEASED.
2235 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2236 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2237 list_for_each_entry(pr_reg_p,
2238 &pr_tmpl->registration_list,
2241 target_ua_allocate_lun(
2242 pr_reg_p->pr_reg_nacl,
2243 pr_reg_p->pr_res_mapped_lun,
2245 ASCQ_2AH_RESERVATIONS_RELEASED);
2249 spin_unlock(&pr_tmpl->registration_lock);
2252 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2256 core_scsi3_put_pr_reg(pr_reg);
2260 unsigned char *core_scsi3_pr_dump_type(int type)
2263 case PR_TYPE_WRITE_EXCLUSIVE:
2264 return "Write Exclusive Access";
2265 case PR_TYPE_EXCLUSIVE_ACCESS:
2266 return "Exclusive Access";
2267 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2268 return "Write Exclusive Access, Registrants Only";
2269 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2270 return "Exclusive Access, Registrants Only";
2271 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2272 return "Write Exclusive Access, All Registrants";
2273 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2274 return "Exclusive Access, All Registrants";
2279 return "Unknown SPC-3 PR Type";
2282 static sense_reason_t
2283 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2285 struct se_device *dev = cmd->se_dev;
2286 struct se_session *se_sess = cmd->se_sess;
2287 struct se_lun *se_lun = cmd->se_lun;
2288 struct t10_pr_registration *pr_reg, *pr_res_holder;
2289 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2290 char i_buf[PR_REG_ISID_ID_LEN];
2293 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2295 if (!se_sess || !se_lun) {
2296 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2297 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2300 * Locate the existing *pr_reg via struct se_node_acl pointers
2302 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2305 pr_err("SPC-3 PR: Unable to locate"
2306 " PR_REGISTERED *pr_reg for RESERVE\n");
2307 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2310 * From spc4r17 Section 5.7.9: Reserving:
2312 * An application client creates a persistent reservation by issuing
2313 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2314 * a registered I_T nexus with the following parameters:
2315 * a) RESERVATION KEY set to the value of the reservation key that is
2316 * registered with the logical unit for the I_T nexus; and
2318 if (res_key != pr_reg->pr_res_key) {
2319 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2320 " does not match existing SA REGISTER res_key:"
2321 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2322 ret = TCM_RESERVATION_CONFLICT;
2323 goto out_put_pr_reg;
2326 * From spc4r17 Section 5.7.9: Reserving:
2329 * b) TYPE field and SCOPE field set to the persistent reservation
2332 * Only one persistent reservation is allowed at a time per logical unit
2333 * and that persistent reservation has a scope of LU_SCOPE.
2335 if (scope != PR_SCOPE_LU_SCOPE) {
2336 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2337 ret = TCM_INVALID_PARAMETER_LIST;
2338 goto out_put_pr_reg;
2341 * See if we have an existing PR reservation holder pointer at
2342 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2345 spin_lock(&dev->dev_reservation_lock);
2346 pr_res_holder = dev->dev_pr_res_holder;
2347 if (pr_res_holder) {
2349 * From spc4r17 Section 5.7.9: Reserving:
2351 * If the device server receives a PERSISTENT RESERVE OUT
2352 * command from an I_T nexus other than a persistent reservation
2353 * holder (see 5.7.10) that attempts to create a persistent
2354 * reservation when a persistent reservation already exists for
2355 * the logical unit, then the command shall be completed with
2356 * RESERVATION CONFLICT status.
2358 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2359 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2360 pr_err("SPC-3 PR: Attempted RESERVE from"
2361 " [%s]: %s while reservation already held by"
2362 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2363 cmd->se_tfo->fabric_name,
2364 se_sess->se_node_acl->initiatorname,
2365 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2366 pr_res_holder->pr_reg_nacl->initiatorname);
2368 spin_unlock(&dev->dev_reservation_lock);
2369 ret = TCM_RESERVATION_CONFLICT;
2370 goto out_put_pr_reg;
2373 * From spc4r17 Section 5.7.9: Reserving:
2375 * If a persistent reservation holder attempts to modify the
2376 * type or scope of an existing persistent reservation, the
2377 * command shall be completed with RESERVATION CONFLICT status.
2379 if ((pr_res_holder->pr_res_type != type) ||
2380 (pr_res_holder->pr_res_scope != scope)) {
2381 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2382 pr_err("SPC-3 PR: Attempted RESERVE from"
2383 " [%s]: %s trying to change TYPE and/or SCOPE,"
2384 " while reservation already held by [%s]: %s,"
2385 " returning RESERVATION_CONFLICT\n",
2386 cmd->se_tfo->fabric_name,
2387 se_sess->se_node_acl->initiatorname,
2388 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2389 pr_res_holder->pr_reg_nacl->initiatorname);
2391 spin_unlock(&dev->dev_reservation_lock);
2392 ret = TCM_RESERVATION_CONFLICT;
2393 goto out_put_pr_reg;
2396 * From spc4r17 Section 5.7.9: Reserving:
2398 * If the device server receives a PERSISTENT RESERVE OUT
2399 * command with RESERVE service action where the TYPE field and
2400 * the SCOPE field contain the same values as the existing type
2401 * and scope from a persistent reservation holder, it shall not
2402 * make any change to the existing persistent reservation and
2403 * shall completethe command with GOOD status.
2405 spin_unlock(&dev->dev_reservation_lock);
2407 goto out_put_pr_reg;
2410 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2411 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2413 pr_reg->pr_res_scope = scope;
2414 pr_reg->pr_res_type = type;
2415 pr_reg->pr_res_holder = 1;
2416 dev->dev_pr_res_holder = pr_reg;
2417 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2419 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2420 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2421 cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
2422 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2423 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2424 cmd->se_tfo->fabric_name,
2425 se_sess->se_node_acl->initiatorname,
2427 spin_unlock(&dev->dev_reservation_lock);
2429 if (pr_tmpl->pr_aptpl_active)
2430 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2434 core_scsi3_put_pr_reg(pr_reg);
2438 static sense_reason_t
2439 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2443 case PR_TYPE_WRITE_EXCLUSIVE:
2444 case PR_TYPE_EXCLUSIVE_ACCESS:
2445 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2446 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2447 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2448 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2449 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2451 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2453 return TCM_INVALID_CDB_FIELD;
2457 static void __core_scsi3_complete_pro_release(
2458 struct se_device *dev,
2459 struct se_node_acl *se_nacl,
2460 struct t10_pr_registration *pr_reg,
2464 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2465 char i_buf[PR_REG_ISID_ID_LEN];
2466 int pr_res_type = 0, pr_res_scope = 0;
2468 lockdep_assert_held(&dev->dev_reservation_lock);
2470 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2471 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2473 * Go ahead and release the current PR reservation holder.
2474 * If an All Registrants reservation is currently active and
2475 * a unregister operation is requested, replace the current
2476 * dev_pr_res_holder with another active registration.
2478 if (dev->dev_pr_res_holder) {
2479 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2480 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2481 dev->dev_pr_res_holder->pr_res_type = 0;
2482 dev->dev_pr_res_holder->pr_res_scope = 0;
2483 dev->dev_pr_res_holder->pr_res_holder = 0;
2484 dev->dev_pr_res_holder = NULL;
2489 spin_lock(&dev->t10_pr.registration_lock);
2490 list_del_init(&pr_reg->pr_reg_list);
2492 * If the I_T nexus is a reservation holder, the persistent reservation
2493 * is of an all registrants type, and the I_T nexus is the last remaining
2494 * registered I_T nexus, then the device server shall also release the
2495 * persistent reservation.
2497 if (!list_empty(&dev->t10_pr.registration_list) &&
2498 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2499 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2500 dev->dev_pr_res_holder =
2501 list_entry(dev->t10_pr.registration_list.next,
2502 struct t10_pr_registration, pr_reg_list);
2503 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2504 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2505 dev->dev_pr_res_holder->pr_res_holder = 1;
2507 spin_unlock(&dev->t10_pr.registration_lock);
2509 if (!dev->dev_pr_res_holder) {
2510 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2511 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2512 tfo->fabric_name, (explicit) ? "explicit" :
2513 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2514 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2516 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2517 tfo->fabric_name, se_nacl->initiatorname,
2520 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2522 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2525 static sense_reason_t
2526 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2529 struct se_device *dev = cmd->se_dev;
2530 struct se_session *se_sess = cmd->se_sess;
2531 struct se_lun *se_lun = cmd->se_lun;
2532 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2533 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2534 sense_reason_t ret = 0;
2536 if (!se_sess || !se_lun) {
2537 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2538 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2541 * Locate the existing *pr_reg via struct se_node_acl pointers
2543 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2545 pr_err("SPC-3 PR: Unable to locate"
2546 " PR_REGISTERED *pr_reg for RELEASE\n");
2547 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2550 * From spc4r17 Section 5.7.11.2 Releasing:
2552 * If there is no persistent reservation or in response to a persistent
2553 * reservation release request from a registered I_T nexus that is not a
2554 * persistent reservation holder (see 5.7.10), the device server shall
2557 * a) Not release the persistent reservation, if any;
2558 * b) Not remove any registrations; and
2559 * c) Complete the command with GOOD status.
2561 spin_lock(&dev->dev_reservation_lock);
2562 pr_res_holder = dev->dev_pr_res_holder;
2563 if (!pr_res_holder) {
2565 * No persistent reservation, return GOOD status.
2567 spin_unlock(&dev->dev_reservation_lock);
2568 goto out_put_pr_reg;
2571 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2573 * Release request from a registered I_T nexus that is not a
2574 * persistent reservation holder. return GOOD status.
2576 spin_unlock(&dev->dev_reservation_lock);
2577 goto out_put_pr_reg;
2581 * From spc4r17 Section 5.7.11.2 Releasing:
2583 * Only the persistent reservation holder (see 5.7.10) is allowed to
2584 * release a persistent reservation.
2586 * An application client releases the persistent reservation by issuing
2587 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2588 * an I_T nexus that is a persistent reservation holder with the
2589 * following parameters:
2591 * a) RESERVATION KEY field set to the value of the reservation key
2592 * that is registered with the logical unit for the I_T nexus;
2594 if (res_key != pr_reg->pr_res_key) {
2595 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2596 " does not match existing SA REGISTER res_key:"
2597 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2598 spin_unlock(&dev->dev_reservation_lock);
2599 ret = TCM_RESERVATION_CONFLICT;
2600 goto out_put_pr_reg;
2603 * From spc4r17 Section 5.7.11.2 Releasing and above:
2605 * b) TYPE field and SCOPE field set to match the persistent
2606 * reservation being released.
2608 if ((pr_res_holder->pr_res_type != type) ||
2609 (pr_res_holder->pr_res_scope != scope)) {
2610 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2611 pr_err("SPC-3 PR RELEASE: Attempted to release"
2612 " reservation from [%s]: %s with different TYPE "
2613 "and/or SCOPE while reservation already held by"
2614 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2615 cmd->se_tfo->fabric_name,
2616 se_sess->se_node_acl->initiatorname,
2617 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2618 pr_res_holder->pr_reg_nacl->initiatorname);
2620 spin_unlock(&dev->dev_reservation_lock);
2621 ret = TCM_RESERVATION_CONFLICT;
2622 goto out_put_pr_reg;
2625 * In response to a persistent reservation release request from the
2626 * persistent reservation holder the device server shall perform a
2627 * release by doing the following as an uninterrupted series of actions:
2628 * a) Release the persistent reservation;
2629 * b) Not remove any registration(s);
2630 * c) If the released persistent reservation is a registrants only type
2631 * or all registrants type persistent reservation,
2632 * the device server shall establish a unit attention condition for
2633 * the initiator port associated with every regis-
2634 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2635 * RESERVE OUT command with RELEASE service action was received,
2636 * with the additional sense code set to RESERVATIONS RELEASED; and
2637 * d) If the persistent reservation is of any other type, the device
2638 * server shall not establish a unit attention condition.
2640 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2643 spin_unlock(&dev->dev_reservation_lock);
2645 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2646 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2647 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2648 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2650 * If no UNIT ATTENTION conditions will be established for
2651 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2652 * go ahead and check for APTPL=1 update+write below
2657 spin_lock(&pr_tmpl->registration_lock);
2658 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2661 * Do not establish a UNIT ATTENTION condition
2662 * for the calling I_T Nexus
2664 if (pr_reg_p == pr_reg)
2667 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2668 pr_reg_p->pr_res_mapped_lun,
2669 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2671 spin_unlock(&pr_tmpl->registration_lock);
2674 if (pr_tmpl->pr_aptpl_active)
2675 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2678 core_scsi3_put_pr_reg(pr_reg);
2682 static sense_reason_t
2683 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2685 struct se_device *dev = cmd->se_dev;
2686 struct se_node_acl *pr_reg_nacl;
2687 struct se_session *se_sess = cmd->se_sess;
2688 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2689 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2690 u64 pr_res_mapped_lun = 0;
2691 int calling_it_nexus = 0;
2693 * Locate the existing *pr_reg via struct se_node_acl pointers
2695 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2696 se_sess->se_node_acl, se_sess);
2698 pr_err("SPC-3 PR: Unable to locate"
2699 " PR_REGISTERED *pr_reg for CLEAR\n");
2700 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2703 * From spc4r17 section 5.7.11.6, Clearing:
2705 * Any application client may release the persistent reservation and
2706 * remove all registrations from a device server by issuing a
2707 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2708 * registered I_T nexus with the following parameter:
2710 * a) RESERVATION KEY field set to the value of the reservation key
2711 * that is registered with the logical unit for the I_T nexus.
2713 if (res_key != pr_reg_n->pr_res_key) {
2714 pr_err("SPC-3 PR REGISTER: Received"
2715 " res_key: 0x%016Lx does not match"
2716 " existing SA REGISTER res_key:"
2717 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2718 core_scsi3_put_pr_reg(pr_reg_n);
2719 return TCM_RESERVATION_CONFLICT;
2722 * a) Release the persistent reservation, if any;
2724 spin_lock(&dev->dev_reservation_lock);
2725 pr_res_holder = dev->dev_pr_res_holder;
2726 if (pr_res_holder) {
2727 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2728 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2729 pr_res_holder, 0, 0);
2731 spin_unlock(&dev->dev_reservation_lock);
2733 * b) Remove all registration(s) (see spc4r17 5.7.7);
2735 spin_lock(&pr_tmpl->registration_lock);
2736 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2737 &pr_tmpl->registration_list, pr_reg_list) {
2739 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2740 pr_reg_nacl = pr_reg->pr_reg_nacl;
2741 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2742 __core_scsi3_free_registration(dev, pr_reg, NULL,
2745 * e) Establish a unit attention condition for the initiator
2746 * port associated with every registered I_T nexus other
2747 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2748 * command with CLEAR service action was received, with the
2749 * additional sense code set to RESERVATIONS PREEMPTED.
2751 if (!calling_it_nexus)
2752 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2753 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2755 spin_unlock(&pr_tmpl->registration_lock);
2757 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2758 cmd->se_tfo->fabric_name);
2760 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2762 core_scsi3_pr_generation(dev);
2766 static void __core_scsi3_complete_pro_preempt(
2767 struct se_device *dev,
2768 struct t10_pr_registration *pr_reg,
2769 struct list_head *preempt_and_abort_list,
2772 enum preempt_type preempt_type)
2774 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2775 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2776 char i_buf[PR_REG_ISID_ID_LEN];
2778 lockdep_assert_held(&dev->dev_reservation_lock);
2780 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2781 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2783 * Do an implicit RELEASE of the existing reservation.
2785 if (dev->dev_pr_res_holder)
2786 __core_scsi3_complete_pro_release(dev, nacl,
2787 dev->dev_pr_res_holder, 0, 0);
2789 dev->dev_pr_res_holder = pr_reg;
2790 pr_reg->pr_res_holder = 1;
2791 pr_reg->pr_res_type = type;
2792 pr_reg->pr_res_scope = scope;
2794 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2795 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2796 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2797 core_scsi3_pr_dump_type(type),
2798 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2799 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2800 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2801 nacl->initiatorname, i_buf);
2803 * For PREEMPT_AND_ABORT, add the preempting reservation's
2804 * struct t10_pr_registration to the list that will be compared
2805 * against received CDBs..
2807 if (preempt_and_abort_list)
2808 list_add_tail(&pr_reg->pr_reg_abort_list,
2809 preempt_and_abort_list);
2812 static void core_scsi3_release_preempt_and_abort(
2813 struct list_head *preempt_and_abort_list,
2814 struct t10_pr_registration *pr_reg_holder)
2816 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2818 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2819 pr_reg_abort_list) {
2821 list_del(&pr_reg->pr_reg_abort_list);
2822 if (pr_reg_holder == pr_reg)
2824 if (pr_reg->pr_res_holder) {
2825 pr_warn("pr_reg->pr_res_holder still set\n");
2829 pr_reg->pr_reg_deve = NULL;
2830 pr_reg->pr_reg_nacl = NULL;
2831 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2835 static sense_reason_t
2836 core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2837 u64 sa_res_key, enum preempt_type preempt_type)
2839 struct se_device *dev = cmd->se_dev;
2840 struct se_node_acl *pr_reg_nacl;
2841 struct se_session *se_sess = cmd->se_sess;
2842 LIST_HEAD(preempt_and_abort_list);
2843 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2844 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2845 u64 pr_res_mapped_lun = 0;
2846 int all_reg = 0, calling_it_nexus = 0;
2847 bool sa_res_key_unmatched = sa_res_key != 0;
2848 int prh_type = 0, prh_scope = 0;
2851 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2853 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2856 pr_err("SPC-3 PR: Unable to locate"
2857 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2858 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2859 return TCM_RESERVATION_CONFLICT;
2861 if (pr_reg_n->pr_res_key != res_key) {
2862 core_scsi3_put_pr_reg(pr_reg_n);
2863 return TCM_RESERVATION_CONFLICT;
2865 if (scope != PR_SCOPE_LU_SCOPE) {
2866 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2867 core_scsi3_put_pr_reg(pr_reg_n);
2868 return TCM_INVALID_PARAMETER_LIST;
2871 spin_lock(&dev->dev_reservation_lock);
2872 pr_res_holder = dev->dev_pr_res_holder;
2873 if (pr_res_holder &&
2874 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2875 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2878 if (!all_reg && !sa_res_key) {
2879 spin_unlock(&dev->dev_reservation_lock);
2880 core_scsi3_put_pr_reg(pr_reg_n);
2881 return TCM_INVALID_PARAMETER_LIST;
2884 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2886 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2887 * persistent reservation holder or there is no persistent reservation
2888 * holder (i.e., there is no persistent reservation), then the device
2889 * server shall perform a preempt by doing the following in an
2890 * uninterrupted series of actions. (See below..)
2892 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2894 * No existing or SA Reservation Key matching reservations..
2896 * PROUT SA PREEMPT with All Registrant type reservations are
2897 * allowed to be processed without a matching SA Reservation Key
2899 spin_lock(&pr_tmpl->registration_lock);
2900 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2901 &pr_tmpl->registration_list, pr_reg_list) {
2903 * Removing of registrations in non all registrants
2904 * type reservations without a matching SA reservation
2907 * a) Remove the registrations for all I_T nexuses
2908 * specified by the SERVICE ACTION RESERVATION KEY
2910 * b) Ignore the contents of the SCOPE and TYPE fields;
2911 * c) Process tasks as defined in 5.7.1; and
2912 * d) Establish a unit attention condition for the
2913 * initiator port associated with every I_T nexus
2914 * that lost its registration other than the I_T
2915 * nexus on which the PERSISTENT RESERVE OUT command
2916 * was received, with the additional sense code set
2917 * to REGISTRATIONS PREEMPTED.
2920 if (pr_reg->pr_res_key != sa_res_key)
2922 sa_res_key_unmatched = false;
2924 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2925 pr_reg_nacl = pr_reg->pr_reg_nacl;
2926 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2927 __core_scsi3_free_registration(dev, pr_reg,
2928 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2929 NULL, calling_it_nexus);
2932 * Case for any existing all registrants type
2933 * reservation, follow logic in spc4r17 section
2934 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2936 * For a ZERO SA Reservation key, release
2937 * all other registrations and do an implicit
2938 * release of active persistent reservation.
2940 * For a non-ZERO SA Reservation key, only
2941 * release the matching reservation key from
2945 (pr_reg->pr_res_key != sa_res_key))
2947 sa_res_key_unmatched = false;
2949 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2950 if (calling_it_nexus)
2953 pr_reg_nacl = pr_reg->pr_reg_nacl;
2954 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2955 __core_scsi3_free_registration(dev, pr_reg,
2956 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2959 if (!calling_it_nexus)
2960 target_ua_allocate_lun(pr_reg_nacl,
2961 pr_res_mapped_lun, 0x2A,
2962 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2964 spin_unlock(&pr_tmpl->registration_lock);
2966 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2967 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2968 * RESERVATION KEY field to a value that does not match any
2969 * registered reservation key, then the device server shall
2970 * complete the command with RESERVATION CONFLICT status.
2972 if (sa_res_key_unmatched) {
2973 spin_unlock(&dev->dev_reservation_lock);
2974 core_scsi3_put_pr_reg(pr_reg_n);
2975 return TCM_RESERVATION_CONFLICT;
2978 * For an existing all registrants type reservation
2979 * with a zero SA rservation key, preempt the existing
2980 * reservation with the new PR type and scope.
2982 if (pr_res_holder && all_reg && !(sa_res_key)) {
2983 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
2984 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2985 type, scope, preempt_type);
2987 if (preempt_type == PREEMPT_AND_ABORT)
2988 core_scsi3_release_preempt_and_abort(
2989 &preempt_and_abort_list, pr_reg_n);
2991 spin_unlock(&dev->dev_reservation_lock);
2993 if (pr_tmpl->pr_aptpl_active)
2994 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2996 core_scsi3_put_pr_reg(pr_reg_n);
2997 core_scsi3_pr_generation(cmd->se_dev);
3001 * The PREEMPTing SA reservation key matches that of the
3002 * existing persistent reservation, first, we check if
3003 * we are preempting our own reservation.
3004 * From spc4r17, section 5.7.11.4.3 Preempting
3005 * persistent reservations and registration handling
3007 * If an all registrants persistent reservation is not
3008 * present, it is not an error for the persistent
3009 * reservation holder to preempt itself (i.e., a
3010 * PERSISTENT RESERVE OUT with a PREEMPT service action
3011 * or a PREEMPT AND ABORT service action with the
3012 * SERVICE ACTION RESERVATION KEY value equal to the
3013 * persistent reservation holder's reservation key that
3014 * is received from the persistent reservation holder).
3015 * In that case, the device server shall establish the
3016 * new persistent reservation and maintain the
3019 prh_type = pr_res_holder->pr_res_type;
3020 prh_scope = pr_res_holder->pr_res_scope;
3022 * If the SERVICE ACTION RESERVATION KEY field identifies a
3023 * persistent reservation holder (see 5.7.10), the device
3024 * server shall perform a preempt by doing the following as
3025 * an uninterrupted series of actions:
3027 * a) Release the persistent reservation for the holder
3028 * identified by the SERVICE ACTION RESERVATION KEY field;
3030 if (pr_reg_n != pr_res_holder)
3031 __core_scsi3_complete_pro_release(dev,
3032 pr_res_holder->pr_reg_nacl,
3033 dev->dev_pr_res_holder, 0, 0);
3035 * b) Remove the registrations for all I_T nexuses identified
3036 * by the SERVICE ACTION RESERVATION KEY field, except the
3037 * I_T nexus that is being used for the PERSISTENT RESERVE
3038 * OUT command. If an all registrants persistent reservation
3039 * is present and the SERVICE ACTION RESERVATION KEY field
3040 * is set to zero, then all registrations shall be removed
3041 * except for that of the I_T nexus that is being used for
3042 * the PERSISTENT RESERVE OUT command;
3044 spin_lock(&pr_tmpl->registration_lock);
3045 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3046 &pr_tmpl->registration_list, pr_reg_list) {
3048 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3049 if (calling_it_nexus)
3052 if (pr_reg->pr_res_key != sa_res_key)
3055 pr_reg_nacl = pr_reg->pr_reg_nacl;
3056 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3057 __core_scsi3_free_registration(dev, pr_reg,
3058 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3061 * e) Establish a unit attention condition for the initiator
3062 * port associated with every I_T nexus that lost its
3063 * persistent reservation and/or registration, with the
3064 * additional sense code set to REGISTRATIONS PREEMPTED;
3066 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3067 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3069 spin_unlock(&pr_tmpl->registration_lock);
3071 * c) Establish a persistent reservation for the preempting
3072 * I_T nexus using the contents of the SCOPE and TYPE fields;
3074 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3075 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3076 type, scope, preempt_type);
3078 * d) Process tasks as defined in 5.7.1;
3080 * f) If the type or scope has changed, then for every I_T nexus
3081 * whose reservation key was not removed, except for the I_T
3082 * nexus on which the PERSISTENT RESERVE OUT command was
3083 * received, the device server shall establish a unit
3084 * attention condition for the initiator port associated with
3085 * that I_T nexus, with the additional sense code set to
3086 * RESERVATIONS RELEASED. If the type or scope have not
3087 * changed, then no unit attention condition(s) shall be
3088 * established for this reason.
3090 if ((prh_type != type) || (prh_scope != scope)) {
3091 spin_lock(&pr_tmpl->registration_lock);
3092 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3093 &pr_tmpl->registration_list, pr_reg_list) {
3095 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3096 if (calling_it_nexus)
3099 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3100 pr_reg->pr_res_mapped_lun, 0x2A,
3101 ASCQ_2AH_RESERVATIONS_RELEASED);
3103 spin_unlock(&pr_tmpl->registration_lock);
3105 spin_unlock(&dev->dev_reservation_lock);
3107 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3108 * All received CDBs for the matching existing reservation and
3109 * registrations undergo ABORT_TASK logic.
3111 * From there, core_scsi3_release_preempt_and_abort() will
3112 * release every registration in the list (which have already
3113 * been removed from the primary pr_reg list), except the
3114 * new persistent reservation holder, the calling Initiator Port.
3116 if (preempt_type == PREEMPT_AND_ABORT) {
3117 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3118 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3122 if (pr_tmpl->pr_aptpl_active)
3123 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3125 core_scsi3_put_pr_reg(pr_reg_n);
3126 core_scsi3_pr_generation(cmd->se_dev);
3130 static sense_reason_t
3131 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3132 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3135 case PR_TYPE_WRITE_EXCLUSIVE:
3136 case PR_TYPE_EXCLUSIVE_ACCESS:
3137 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3138 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3139 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3140 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3141 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3142 sa_res_key, preempt_type);
3144 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3145 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3146 return TCM_INVALID_CDB_FIELD;
3151 static sense_reason_t
3152 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3153 u64 sa_res_key, int aptpl, int unreg)
3155 struct se_session *se_sess = cmd->se_sess;
3156 struct se_device *dev = cmd->se_dev;
3157 struct se_dev_entry *dest_se_deve = NULL;
3158 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3159 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3160 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3161 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3162 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3163 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3165 const unsigned char *initiator_str;
3166 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
3167 u32 tid_len, tmp_tid_len;
3168 int new_reg = 0, type, scope, matching_iname;
3170 unsigned short rtpi;
3171 unsigned char proto_ident;
3173 if (!se_sess || !se_lun) {
3174 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3175 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3178 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3179 se_tpg = se_sess->se_tpg;
3180 tf_ops = se_tpg->se_tpg_tfo;
3182 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3183 * Register behaviors for a REGISTER AND MOVE service action
3185 * Locate the existing *pr_reg via struct se_node_acl pointers
3187 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3190 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3191 " *pr_reg for REGISTER_AND_MOVE\n");
3192 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3195 * The provided reservation key much match the existing reservation key
3196 * provided during this initiator's I_T nexus registration.
3198 if (res_key != pr_reg->pr_res_key) {
3199 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3200 " res_key: 0x%016Lx does not match existing SA REGISTER"
3201 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3202 ret = TCM_RESERVATION_CONFLICT;
3203 goto out_put_pr_reg;
3206 * The service active reservation key needs to be non zero
3209 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3211 ret = TCM_INVALID_PARAMETER_LIST;
3212 goto out_put_pr_reg;
3216 * Determine the Relative Target Port Identifier where the reservation
3217 * will be moved to for the TransportID containing SCSI initiator WWN
3220 buf = transport_kmap_data_sg(cmd);
3222 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3223 goto out_put_pr_reg;
3226 rtpi = get_unaligned_be16(&buf[18]);
3227 tid_len = get_unaligned_be32(&buf[20]);
3228 transport_kunmap_data_sg(cmd);
3231 if ((tid_len + 24) != cmd->data_length) {
3232 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3233 " does not equal CDB data_length: %u\n", tid_len,
3235 ret = TCM_INVALID_PARAMETER_LIST;
3236 goto out_put_pr_reg;
3239 spin_lock(&dev->se_port_lock);
3240 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3241 if (tmp_lun->lun_rtpi != rtpi)
3243 dest_se_tpg = tmp_lun->lun_tpg;
3244 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3248 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3249 spin_unlock(&dev->se_port_lock);
3251 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3252 pr_err("core_scsi3_tpg_depend_item() failed"
3253 " for dest_se_tpg\n");
3254 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3255 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3256 goto out_put_pr_reg;
3259 spin_lock(&dev->se_port_lock);
3262 spin_unlock(&dev->se_port_lock);
3264 if (!dest_se_tpg || !dest_tf_ops) {
3265 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3266 " fabric ops from Relative Target Port Identifier:"
3268 ret = TCM_INVALID_PARAMETER_LIST;
3269 goto out_put_pr_reg;
3272 buf = transport_kmap_data_sg(cmd);
3274 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3275 goto out_put_pr_reg;
3277 proto_ident = (buf[24] & 0x0f);
3279 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3280 " 0x%02x\n", proto_ident);
3282 if (proto_ident != dest_se_tpg->proto_id) {
3283 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3284 " proto_ident: 0x%02x does not match ident: 0x%02x"
3285 " from fabric: %s\n", proto_ident,
3286 dest_se_tpg->proto_id,
3287 dest_tf_ops->fabric_name);
3288 ret = TCM_INVALID_PARAMETER_LIST;
3291 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
3292 &buf[24], &tmp_tid_len, &iport_ptr);
3293 if (!initiator_str) {
3294 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3295 " initiator_str from Transport ID\n");
3296 ret = TCM_INVALID_PARAMETER_LIST;
3300 transport_kunmap_data_sg(cmd);
3303 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3304 " %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
3305 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3308 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3309 * action specifies a TransportID that is the same as the initiator port
3310 * of the I_T nexus for the command received, then the command shall
3311 * be terminated with CHECK CONDITION status, with the sense key set to
3312 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3313 * IN PARAMETER LIST.
3315 pr_reg_nacl = pr_reg->pr_reg_nacl;
3316 matching_iname = (!strcmp(initiator_str,
3317 pr_reg_nacl->initiatorname)) ? 1 : 0;
3318 if (!matching_iname)
3319 goto after_iport_check;
3321 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3322 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3323 " matches: %s on received I_T Nexus\n", initiator_str,
3324 pr_reg_nacl->initiatorname);
3325 ret = TCM_INVALID_PARAMETER_LIST;
3328 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3329 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3330 " matches: %s %s on received I_T Nexus\n",
3331 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3332 pr_reg->pr_reg_isid);
3333 ret = TCM_INVALID_PARAMETER_LIST;
3338 * Locate the destination struct se_node_acl from the received Transport ID
3340 mutex_lock(&dest_se_tpg->acl_node_mutex);
3341 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3344 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3345 mutex_unlock(&dest_se_tpg->acl_node_mutex);
3347 if (!dest_node_acl) {
3348 pr_err("Unable to locate %s dest_node_acl for"
3349 " TransportID%s\n", dest_tf_ops->fabric_name,
3351 ret = TCM_INVALID_PARAMETER_LIST;
3355 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3356 pr_err("core_scsi3_nodeacl_depend_item() for"
3357 " dest_node_acl\n");
3358 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3359 dest_node_acl = NULL;
3360 ret = TCM_INVALID_PARAMETER_LIST;
3364 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3365 " %s from TransportID\n", dest_tf_ops->fabric_name,
3366 dest_node_acl->initiatorname);
3369 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3372 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3373 if (!dest_se_deve) {
3374 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3375 " %hu\n", dest_tf_ops->fabric_name, rtpi);
3376 ret = TCM_INVALID_PARAMETER_LIST;
3380 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3381 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3382 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3383 dest_se_deve = NULL;
3384 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3388 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3389 " ACL for dest_se_deve->mapped_lun: %llu\n",
3390 dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
3391 dest_se_deve->mapped_lun);
3394 * A persistent reservation needs to already existing in order to
3395 * successfully complete the REGISTER_AND_MOVE service action..
3397 spin_lock(&dev->dev_reservation_lock);
3398 pr_res_holder = dev->dev_pr_res_holder;
3399 if (!pr_res_holder) {
3400 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3401 " currently held\n");
3402 spin_unlock(&dev->dev_reservation_lock);
3403 ret = TCM_INVALID_CDB_FIELD;
3407 * The received on I_T Nexus must be the reservation holder.
3409 * From spc4r17 section 5.7.8 Table 50 --
3410 * Register behaviors for a REGISTER AND MOVE service action
3412 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3413 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3414 " Nexus is not reservation holder\n");
3415 spin_unlock(&dev->dev_reservation_lock);
3416 ret = TCM_RESERVATION_CONFLICT;
3420 * From spc4r17 section 5.7.8: registering and moving reservation
3422 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3423 * action is received and the established persistent reservation is a
3424 * Write Exclusive - All Registrants type or Exclusive Access -
3425 * All Registrants type reservation, then the command shall be completed
3426 * with RESERVATION CONFLICT status.
3428 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3429 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3430 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3431 " reservation for type: %s\n",
3432 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3433 spin_unlock(&dev->dev_reservation_lock);
3434 ret = TCM_RESERVATION_CONFLICT;
3437 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3439 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3441 type = pr_res_holder->pr_res_type;
3442 scope = pr_res_holder->pr_res_type;
3444 * c) Associate the reservation key specified in the SERVICE ACTION
3445 * RESERVATION KEY field with the I_T nexus specified as the
3446 * destination of the register and move, where:
3447 * A) The I_T nexus is specified by the TransportID and the
3448 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3449 * B) Regardless of the TransportID format used, the association for
3450 * the initiator port is based on either the initiator port name
3451 * (see 3.1.71) on SCSI transport protocols where port names are
3452 * required or the initiator port identifier (see 3.1.70) on SCSI
3453 * transport protocols where port names are not required;
3454 * d) Register the reservation key specified in the SERVICE ACTION
3455 * RESERVATION KEY field;
3456 * e) Retain the reservation key specified in the SERVICE ACTION
3457 * RESERVATION KEY field and associated information;
3459 * Also, It is not an error for a REGISTER AND MOVE service action to
3460 * register an I_T nexus that is already registered with the same
3461 * reservation key or a different reservation key.
3463 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3466 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
3467 kref_read(&dest_se_deve->pr_kref) != 0);
3469 spin_unlock(&dev->dev_reservation_lock);
3470 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3471 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3472 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
3473 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3476 spin_lock(&dev->dev_reservation_lock);
3477 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3482 * f) Release the persistent reservation for the persistent reservation
3483 * holder (i.e., the I_T nexus on which the
3485 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3486 dev->dev_pr_res_holder, 0, 0);
3488 * g) Move the persistent reservation to the specified I_T nexus using
3489 * the same scope and type as the persistent reservation released in
3492 dev->dev_pr_res_holder = dest_pr_reg;
3493 dest_pr_reg->pr_res_holder = 1;
3494 dest_pr_reg->pr_res_type = type;
3495 pr_reg->pr_res_scope = scope;
3496 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3498 * Increment PRGeneration for existing registrations..
3501 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3502 spin_unlock(&dev->dev_reservation_lock);
3504 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3505 " created new reservation holder TYPE: %s on object RTPI:"
3506 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
3507 core_scsi3_pr_dump_type(type), rtpi,
3508 dest_pr_reg->pr_res_generation);
3509 pr_debug("SPC-3 PR Successfully moved reservation from"
3510 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3511 tf_ops->fabric_name, pr_reg_nacl->initiatorname,
3512 i_buf, dest_tf_ops->fabric_name,
3513 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3516 * It is now safe to release configfs group dependencies for destination
3517 * of Transport ID Initiator Device/Port Identifier
3519 core_scsi3_lunacl_undepend_item(dest_se_deve);
3520 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3521 core_scsi3_tpg_undepend_item(dest_se_tpg);
3523 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3524 * nexus on which PERSISTENT RESERVE OUT command was received.
3527 spin_lock(&pr_tmpl->registration_lock);
3528 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3529 spin_unlock(&pr_tmpl->registration_lock);
3531 core_scsi3_put_pr_reg(pr_reg);
3533 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3535 core_scsi3_put_pr_reg(dest_pr_reg);
3539 transport_kunmap_data_sg(cmd);
3541 core_scsi3_lunacl_undepend_item(dest_se_deve);
3543 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3544 core_scsi3_tpg_undepend_item(dest_se_tpg);
3547 core_scsi3_put_pr_reg(pr_reg);
3552 * See spc4r17 section 6.14 Table 170
3555 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3557 struct se_device *dev = cmd->se_dev;
3558 unsigned char *cdb = &cmd->t_task_cdb[0];
3560 u64 res_key, sa_res_key;
3561 int sa, scope, type, aptpl;
3562 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3566 * Following spc2r20 5.5.1 Reservations overview:
3568 * If a logical unit has been reserved by any RESERVE command and is
3569 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3570 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3571 * initiator or service action and shall terminate with a RESERVATION
3574 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3575 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3576 " SPC-2 reservation is held, returning"
3577 " RESERVATION_CONFLICT\n");
3578 return TCM_RESERVATION_CONFLICT;
3582 * FIXME: A NULL struct se_session pointer means an this is not coming from
3583 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3586 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3588 if (cmd->data_length < 24) {
3589 pr_warn("SPC-PR: Received PR OUT parameter list"
3590 " length too small: %u\n", cmd->data_length);
3591 return TCM_PARAMETER_LIST_LENGTH_ERROR;
3595 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3597 sa = (cdb[1] & 0x1f);
3598 scope = (cdb[2] & 0xf0);
3599 type = (cdb[2] & 0x0f);
3601 buf = transport_kmap_data_sg(cmd);
3603 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3606 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3608 res_key = get_unaligned_be64(&buf[0]);
3609 sa_res_key = get_unaligned_be64(&buf[8]);
3611 * REGISTER_AND_MOVE uses a different SA parameter list containing
3612 * SCSI TransportIDs.
3614 if (sa != PRO_REGISTER_AND_MOVE) {
3615 spec_i_pt = (buf[20] & 0x08);
3616 all_tg_pt = (buf[20] & 0x04);
3617 aptpl = (buf[20] & 0x01);
3619 aptpl = (buf[17] & 0x01);
3620 unreg = (buf[17] & 0x02);
3623 * If the backend device has been configured to force APTPL metadata
3624 * write-out, go ahead and propigate aptpl=1 down now.
3626 if (dev->dev_attrib.force_pr_aptpl)
3629 transport_kunmap_data_sg(cmd);
3633 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3635 if (spec_i_pt && (sa != PRO_REGISTER))
3636 return TCM_INVALID_PARAMETER_LIST;
3639 * From spc4r17 section 6.14:
3641 * If the SPEC_I_PT bit is set to zero, the service action is not
3642 * REGISTER AND MOVE, and the parameter list length is not 24, then
3643 * the command shall be terminated with CHECK CONDITION status, with
3644 * the sense key set to ILLEGAL REQUEST, and the additional sense
3645 * code set to PARAMETER LIST LENGTH ERROR.
3647 if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
3648 (cmd->data_length != 24)) {
3649 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3650 " list length: %u\n", cmd->data_length);
3651 return TCM_PARAMETER_LIST_LENGTH_ERROR;
3655 * (core_scsi3_emulate_pro_* function parameters
3656 * are defined by spc4r17 Table 174:
3657 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3661 ret = core_scsi3_emulate_pro_register(cmd,
3662 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3665 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3668 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3671 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3674 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3675 res_key, sa_res_key, PREEMPT);
3677 case PRO_PREEMPT_AND_ABORT:
3678 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3679 res_key, sa_res_key, PREEMPT_AND_ABORT);
3681 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3682 ret = core_scsi3_emulate_pro_register(cmd,
3683 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3685 case PRO_REGISTER_AND_MOVE:
3686 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3687 sa_res_key, aptpl, unreg);
3690 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3691 " action: 0x%02x\n", sa);
3692 return TCM_INVALID_CDB_FIELD;
3696 target_complete_cmd(cmd, GOOD);
3701 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3703 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3705 static sense_reason_t
3706 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3708 struct se_device *dev = cmd->se_dev;
3709 struct t10_pr_registration *pr_reg;
3711 u32 add_len = 0, off = 8;
3713 if (cmd->data_length < 8) {
3714 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3715 " too small\n", cmd->data_length);
3716 return TCM_INVALID_CDB_FIELD;
3719 buf = transport_kmap_data_sg(cmd);
3721 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3723 put_unaligned_be32(dev->t10_pr.pr_generation, buf);
3725 spin_lock(&dev->t10_pr.registration_lock);
3726 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3729 * Check for overflow of 8byte PRI READ_KEYS payload and
3730 * next reservation key list descriptor.
3732 if (off + 8 <= cmd->data_length) {
3733 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3737 * SPC5r17: 6.16.2 READ KEYS service action
3738 * The ADDITIONAL LENGTH field indicates the number of bytes in
3739 * the Reservation key list. The contents of the ADDITIONAL
3740 * LENGTH field are not altered based on the allocation length
3744 spin_unlock(&dev->t10_pr.registration_lock);
3746 put_unaligned_be32(add_len, &buf[4]);
3748 transport_kunmap_data_sg(cmd);
3754 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3756 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3758 static sense_reason_t
3759 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3761 struct se_device *dev = cmd->se_dev;
3762 struct t10_pr_registration *pr_reg;
3765 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3767 if (cmd->data_length < 8) {
3768 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3769 " too small\n", cmd->data_length);
3770 return TCM_INVALID_CDB_FIELD;
3773 buf = transport_kmap_data_sg(cmd);
3775 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3777 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3779 spin_lock(&dev->dev_reservation_lock);
3780 pr_reg = dev->dev_pr_res_holder;
3783 * Set the hardcoded Additional Length
3785 put_unaligned_be32(add_len, &buf[4]);
3787 if (cmd->data_length < 22)
3791 * Set the Reservation key.
3793 * From spc4r17, section 5.7.10:
3794 * A persistent reservation holder has its reservation key
3795 * returned in the parameter data from a PERSISTENT
3796 * RESERVE IN command with READ RESERVATION service action as
3798 * a) For a persistent reservation of the type Write Exclusive
3799 * - All Registrants or Exclusive Access  All Regitrants,
3800 * the reservation key shall be set to zero; or
3801 * b) For all other persistent reservation types, the
3802 * reservation key shall be set to the registered
3803 * reservation key for the I_T nexus that holds the
3804 * persistent reservation.
3806 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3807 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3810 pr_res_key = pr_reg->pr_res_key;
3812 put_unaligned_be64(pr_res_key, &buf[8]);
3814 * Set the SCOPE and TYPE
3816 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3817 (pr_reg->pr_res_type & 0x0f);
3821 spin_unlock(&dev->dev_reservation_lock);
3822 transport_kunmap_data_sg(cmd);
3828 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3830 * See spc4r17 section 6.13.4 Table 165
3832 static sense_reason_t
3833 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3835 struct se_device *dev = cmd->se_dev;
3836 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3838 u16 add_len = 8; /* Hardcoded to 8. */
3840 if (cmd->data_length < 6) {
3841 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3842 " %u too small\n", cmd->data_length);
3843 return TCM_INVALID_CDB_FIELD;
3846 buf = transport_kmap_data_sg(cmd);
3848 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3850 put_unaligned_be16(add_len, &buf[0]);
3851 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3852 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3853 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3854 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3856 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3857 * set the TMV: Task Mask Valid bit.
3861 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3863 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3865 * PTPL_A: Persistence across Target Power Loss Active bit
3867 if (pr_tmpl->pr_aptpl_active)
3870 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3872 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3873 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3874 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3875 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3876 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3877 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3879 transport_kunmap_data_sg(cmd);
3885 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3887 * See spc4r17 section 6.13.5 Table 168 and 169
3889 static sense_reason_t
3890 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3892 struct se_device *dev = cmd->se_dev;
3893 struct se_node_acl *se_nacl;
3894 struct se_portal_group *se_tpg;
3895 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3896 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3898 u32 add_desc_len = 0, add_len = 0;
3899 u32 off = 8; /* off into first Full Status descriptor */
3900 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3901 int exp_desc_len, desc_len;
3902 bool all_reg = false;
3904 if (cmd->data_length < 8) {
3905 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3906 " too small\n", cmd->data_length);
3907 return TCM_INVALID_CDB_FIELD;
3910 buf = transport_kmap_data_sg(cmd);
3912 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3914 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3916 spin_lock(&dev->dev_reservation_lock);
3917 if (dev->dev_pr_res_holder) {
3918 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3920 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3921 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3923 pr_res_type = pr_holder->pr_res_type;
3924 pr_res_scope = pr_holder->pr_res_scope;
3927 spin_unlock(&dev->dev_reservation_lock);
3929 spin_lock(&pr_tmpl->registration_lock);
3930 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3931 &pr_tmpl->registration_list, pr_reg_list) {
3933 se_nacl = pr_reg->pr_reg_nacl;
3934 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3937 atomic_inc_mb(&pr_reg->pr_res_holders);
3938 spin_unlock(&pr_tmpl->registration_lock);
3940 * Determine expected length of $FABRIC_MOD specific
3941 * TransportID full status descriptor..
3943 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3945 if (exp_desc_len < 0 ||
3946 exp_desc_len + add_len > cmd->data_length) {
3947 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3948 " out of buffer: %d\n", cmd->data_length);
3949 spin_lock(&pr_tmpl->registration_lock);
3950 atomic_dec_mb(&pr_reg->pr_res_holders);
3954 * Set RESERVATION KEY
3956 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3958 off += 4; /* Skip Over Reserved area */
3961 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3963 if (pr_reg->pr_reg_all_tg_pt)
3966 * The struct se_lun pointer will be present for the
3967 * reservation holder for PR_HOLDER bit.
3969 * Also, if this registration is the reservation
3970 * holder or there is an All Registrants reservation
3971 * active, fill in SCOPE and TYPE in the next byte.
3973 if (pr_reg->pr_res_holder) {
3975 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3976 (pr_reg->pr_res_type & 0x0f);
3977 } else if (all_reg) {
3979 buf[off++] = (pr_res_scope & 0xf0) |
3980 (pr_res_type & 0x0f);
3985 off += 4; /* Skip over reserved area */
3987 * From spc4r17 6.3.15:
3989 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3990 * IDENTIFIER field contains the relative port identifier (see
3991 * 3.1.120) of the target port that is part of the I_T nexus
3992 * described by this full status descriptor. If the ALL_TG_PT
3993 * bit is set to one, the contents of the RELATIVE TARGET PORT
3994 * IDENTIFIER field are not defined by this standard.
3996 if (!pr_reg->pr_reg_all_tg_pt) {
3997 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
3999 put_unaligned_be16(sep_rtpi, &buf[off]);
4002 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4004 buf[off+4] = se_tpg->proto_id;
4007 * Now, have the $FABRIC_MOD fill in the transport ID.
4009 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4010 &format_code, &buf[off+4]);
4012 spin_lock(&pr_tmpl->registration_lock);
4013 atomic_dec_mb(&pr_reg->pr_res_holders);
4018 * Set the ADDITIONAL DESCRIPTOR LENGTH
4020 put_unaligned_be32(desc_len, &buf[off]);
4023 * Size of full desctipor header minus TransportID
4024 * containing $FABRIC_MOD specific) initiator device/port
4027 * See spc4r17 Section 6.13.5 Table 169
4029 add_desc_len = (24 + desc_len);
4032 add_len += add_desc_len;
4034 spin_unlock(&pr_tmpl->registration_lock);
4036 * Set ADDITIONAL_LENGTH
4038 put_unaligned_be32(add_len, &buf[4]);
4040 transport_kunmap_data_sg(cmd);
4046 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4051 * Following spc2r20 5.5.1 Reservations overview:
4053 * If a logical unit has been reserved by any RESERVE command and is
4054 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4055 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4056 * initiator or service action and shall terminate with a RESERVATION
4059 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4060 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4061 " SPC-2 reservation is held, returning"
4062 " RESERVATION_CONFLICT\n");
4063 return TCM_RESERVATION_CONFLICT;
4066 switch (cmd->t_task_cdb[1] & 0x1f) {
4068 ret = core_scsi3_pri_read_keys(cmd);
4070 case PRI_READ_RESERVATION:
4071 ret = core_scsi3_pri_read_reservation(cmd);
4073 case PRI_REPORT_CAPABILITIES:
4074 ret = core_scsi3_pri_report_capabilities(cmd);
4076 case PRI_READ_FULL_STATUS:
4077 ret = core_scsi3_pri_read_full_status(cmd);
4080 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4081 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4082 return TCM_INVALID_CDB_FIELD;
4086 target_complete_cmd(cmd, GOOD);
4091 target_check_reservation(struct se_cmd *cmd)
4093 struct se_device *dev = cmd->se_dev;
4098 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4100 if (!dev->dev_attrib.emulate_pr)
4102 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
4105 spin_lock(&dev->dev_reservation_lock);
4106 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4107 ret = target_scsi2_reservation_check(cmd);
4109 ret = target_scsi3_pr_reservation_check(cmd);
4110 spin_unlock(&dev->dev_reservation_lock);