1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * Filename: target_core_pr.c
5 * This file contains SPC-3 compliant persistent reservations and
6 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
8 * (c) Copyright 2009-2013 Datera, Inc.
12 ******************************************************************************/
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/list.h>
17 #include <linux/vmalloc.h>
18 #include <linux/file.h>
19 #include <linux/fcntl.h>
21 #include <scsi/scsi_proto.h>
22 #include <asm/unaligned.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_backend.h>
26 #include <target/target_core_fabric.h>
28 #include "target_core_internal.h"
29 #include "target_core_pr.h"
30 #include "target_core_ua.h"
33 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
35 struct pr_transport_id_holder {
36 struct t10_pr_registration *dest_pr_reg;
37 struct se_portal_group *dest_tpg;
38 struct se_node_acl *dest_node_acl;
39 struct se_dev_entry *dest_se_deve;
40 struct list_head dest_list;
43 void core_pr_dump_initiator_port(
44 struct t10_pr_registration *pr_reg,
48 if (!pr_reg->isid_present_at_reg) {
53 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
58 REGISTER_AND_IGNORE_EXISTING_KEY,
67 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
68 struct t10_pr_registration *, int, int);
70 static int is_reservation_holder(
71 struct t10_pr_registration *pr_res_holder,
72 struct t10_pr_registration *pr_reg)
77 pr_res_type = pr_res_holder->pr_res_type;
79 return pr_res_holder == pr_reg ||
80 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
81 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
87 target_scsi2_reservation_check(struct se_cmd *cmd)
89 struct se_device *dev = cmd->se_dev;
90 struct se_session *sess = cmd->se_sess;
92 switch (cmd->t_task_cdb[0]) {
101 if (!dev->reservation_holder || !sess)
104 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
105 return TCM_RESERVATION_CONFLICT;
107 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
108 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
109 return TCM_RESERVATION_CONFLICT;
115 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
116 struct se_node_acl *, struct se_session *);
117 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
119 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
121 struct se_session *se_sess = cmd->se_sess;
122 struct se_device *dev = cmd->se_dev;
123 struct t10_pr_registration *pr_reg;
124 struct t10_reservation *pr_tmpl = &dev->t10_pr;
127 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
131 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
134 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
135 * status, but no reservation shall be established and the
136 * persistent reservation shall not be changed, if the command
137 * is received from a) and b) below.
139 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
140 * status, but the persistent reservation shall not be released,
141 * if the command is received from a) and b)
143 * a) An I_T nexus that is a persistent reservation holder; or
144 * b) An I_T nexus that is registered if a registrants only or
145 * all registrants type persistent reservation is present.
147 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
148 * RELEASE(6) command, or RELEASE(10) command shall be processed
149 * as defined in SPC-2.
151 if (pr_reg->pr_res_holder) {
152 core_scsi3_put_pr_reg(pr_reg);
155 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
156 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
157 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
158 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
159 core_scsi3_put_pr_reg(pr_reg);
162 core_scsi3_put_pr_reg(pr_reg);
166 * Following spc2r20 5.5.1 Reservations overview:
168 * If a logical unit has executed a PERSISTENT RESERVE OUT
169 * command with the REGISTER or the REGISTER AND IGNORE
170 * EXISTING KEY service action and is still registered by any
171 * initiator, all RESERVE commands and all RELEASE commands
172 * regardless of initiator shall conflict and shall terminate
173 * with a RESERVATION CONFLICT status.
175 spin_lock(&pr_tmpl->registration_lock);
176 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
177 spin_unlock(&pr_tmpl->registration_lock);
181 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
182 " while active SPC-3 registrations exist,"
183 " returning RESERVATION_CONFLICT\n");
190 void target_release_reservation(struct se_device *dev)
192 dev->reservation_holder = NULL;
193 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
194 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
195 dev->dev_res_bin_isid = 0;
196 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
201 target_scsi2_reservation_release(struct se_cmd *cmd)
203 struct se_device *dev = cmd->se_dev;
204 struct se_session *sess = cmd->se_sess;
205 struct se_portal_group *tpg;
208 if (!sess || !sess->se_tpg)
210 rc = target_check_scsi2_reservation_conflict(cmd);
214 return TCM_RESERVATION_CONFLICT;
216 spin_lock(&dev->dev_reservation_lock);
217 if (!dev->reservation_holder || !sess)
220 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
223 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
226 target_release_reservation(dev);
228 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
229 " MAPPED LUN: %llu for %s\n",
230 tpg->se_tpg_tfo->fabric_name,
231 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
232 sess->se_node_acl->initiatorname);
235 spin_unlock(&dev->dev_reservation_lock);
237 target_complete_cmd(cmd, SAM_STAT_GOOD);
242 target_scsi2_reservation_reserve(struct se_cmd *cmd)
244 struct se_device *dev = cmd->se_dev;
245 struct se_session *sess = cmd->se_sess;
246 struct se_portal_group *tpg;
247 sense_reason_t ret = 0;
250 if ((cmd->t_task_cdb[1] & 0x01) &&
251 (cmd->t_task_cdb[1] & 0x02)) {
252 pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
253 return TCM_UNSUPPORTED_SCSI_OPCODE;
256 * This is currently the case for target_core_mod passthrough struct se_cmd
259 if (!sess || !sess->se_tpg)
261 rc = target_check_scsi2_reservation_conflict(cmd);
266 return TCM_RESERVATION_CONFLICT;
269 spin_lock(&dev->dev_reservation_lock);
270 if (dev->reservation_holder &&
271 dev->reservation_holder->se_node_acl != sess->se_node_acl) {
272 pr_err("SCSI-2 RESERVATION CONFLICT for %s fabric\n",
273 tpg->se_tpg_tfo->fabric_name);
274 pr_err("Original reserver LUN: %llu %s\n",
275 cmd->se_lun->unpacked_lun,
276 dev->reservation_holder->se_node_acl->initiatorname);
277 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
278 " from %s \n", cmd->se_lun->unpacked_lun,
280 sess->se_node_acl->initiatorname);
281 ret = TCM_RESERVATION_CONFLICT;
285 dev->reservation_holder = sess;
286 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
287 if (sess->sess_bin_isid != 0) {
288 dev->dev_res_bin_isid = sess->sess_bin_isid;
289 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
291 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
292 " for %s\n", tpg->se_tpg_tfo->fabric_name,
293 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
294 sess->se_node_acl->initiatorname);
297 spin_unlock(&dev->dev_reservation_lock);
300 target_complete_cmd(cmd, SAM_STAT_GOOD);
306 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
308 * This function is called by those initiator ports who are *NOT*
309 * the active PR reservation holder when a reservation is present.
311 static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
314 unsigned char *cdb = cmd->t_task_cdb;
315 struct se_session *se_sess = cmd->se_sess;
316 struct se_node_acl *nacl = se_sess->se_node_acl;
318 int registered_nexus = 0, ret = 1; /* Conflict by default */
319 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
320 int we = 0; /* Write Exclusive */
321 int legacy = 0; /* Act like a legacy device and return
322 * RESERVATION CONFLICT on some CDBs */
325 registered_nexus = 0;
327 struct se_dev_entry *se_deve;
330 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
332 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
333 &se_deve->deve_flags);
337 switch (pr_reg_type) {
338 case PR_TYPE_WRITE_EXCLUSIVE:
341 case PR_TYPE_EXCLUSIVE_ACCESS:
343 * Some commands are only allowed for the persistent reservation
347 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
350 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
352 * Some commands are only allowed for registered I_T Nexuses.
356 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
359 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
361 * Each registered I_T Nexus is a reservation holder.
369 * Referenced from spc4r17 table 45 for *NON* PR holder access
372 case SECURITY_PROTOCOL_IN:
373 if (registered_nexus)
381 case RECEIVE_DIAGNOSTIC:
386 if (registered_nexus) {
390 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
392 case PERSISTENT_RESERVE_OUT:
394 * This follows PERSISTENT_RESERVE_OUT service actions that
395 * are allowed in the presence of various reservations.
396 * See spc4r17, table 46
398 switch (cdb[1] & 0x1f) {
401 case PRO_PREEMPT_AND_ABORT:
402 ret = (registered_nexus) ? 0 : 1;
405 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
408 case PRO_REGISTER_AND_MOVE:
413 ret = (registered_nexus) ? 0 : 1;
416 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
417 " action: 0x%02x\n", cdb[1] & 0x1f);
423 /* Handled by CRH=1 in target_scsi2_reservation_release() */
428 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
431 case TEST_UNIT_READY:
432 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
435 switch (cdb[1] & 0x1f) {
436 case MI_MANAGEMENT_PROTOCOL_IN:
437 if (registered_nexus) {
441 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
443 case MI_REPORT_SUPPORTED_OPERATION_CODES:
444 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
449 if (registered_nexus) {
453 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
455 case MI_REPORT_ALIASES:
456 case MI_REPORT_IDENTIFYING_INFORMATION:
457 case MI_REPORT_PRIORITY:
458 case MI_REPORT_TARGET_PGS:
459 case MI_REPORT_TIMESTAMP:
460 ret = 0; /* Allowed */
463 pr_err("Unknown MI Service Action: 0x%02x\n",
468 case ACCESS_CONTROL_IN:
469 case ACCESS_CONTROL_OUT:
472 case SERVICE_ACTION_IN_12:
475 case PERSISTENT_RESERVE_IN:
476 ret = 0; /*/ Allowed CDBs */
483 * Case where the CDB is explicitly allowed in the above switch
486 if (!ret && !other_cdb) {
487 pr_debug("Allowing explicit CDB: 0x%02x for %s"
488 " reservation holder\n", cdb[0],
489 core_scsi3_pr_dump_type(pr_reg_type));
494 * Check if write exclusive initiator ports *NOT* holding the
495 * WRITE_EXCLUSIVE_* reservation.
497 if (we && !registered_nexus) {
498 if (cmd->data_direction == DMA_TO_DEVICE) {
500 * Conflict for write exclusive
502 pr_debug("%s Conflict for unregistered nexus"
503 " %s CDB: 0x%02x to %s reservation\n",
504 transport_dump_cmd_direction(cmd),
505 se_sess->se_node_acl->initiatorname, cdb[0],
506 core_scsi3_pr_dump_type(pr_reg_type));
510 * Allow non WRITE CDBs for all Write Exclusive
511 * PR TYPEs to pass for registered and
512 * non-registered_nexuxes NOT holding the reservation.
514 * We only make noise for the unregisterd nexuses,
515 * as we expect registered non-reservation holding
516 * nexuses to issue CDBs.
519 if (!registered_nexus) {
520 pr_debug("Allowing implicit CDB: 0x%02x"
521 " for %s reservation on unregistered"
523 core_scsi3_pr_dump_type(pr_reg_type));
528 } else if ((reg_only) || (all_reg)) {
529 if (registered_nexus) {
531 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
532 * allow commands from registered nexuses.
535 pr_debug("Allowing implicit CDB: 0x%02x for %s"
536 " reservation\n", cdb[0],
537 core_scsi3_pr_dump_type(pr_reg_type));
541 } else if (we && registered_nexus) {
543 * Reads are allowed for Write Exclusive locks
544 * from all registrants.
546 if (cmd->data_direction == DMA_FROM_DEVICE) {
547 pr_debug("Allowing READ CDB: 0x%02x for %s"
548 " reservation\n", cdb[0],
549 core_scsi3_pr_dump_type(pr_reg_type));
554 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
555 " for %s reservation\n", transport_dump_cmd_direction(cmd),
556 (registered_nexus) ? "" : "un",
557 se_sess->se_node_acl->initiatorname, cdb[0],
558 core_scsi3_pr_dump_type(pr_reg_type));
560 return 1; /* Conflict by default */
563 static sense_reason_t
564 target_scsi3_pr_reservation_check(struct se_cmd *cmd)
566 struct se_device *dev = cmd->se_dev;
567 struct se_session *sess = cmd->se_sess;
569 bool isid_mismatch = false;
571 if (!dev->dev_pr_res_holder)
574 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
575 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
576 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
577 goto check_nonholder;
579 if (dev->dev_pr_res_holder->isid_present_at_reg) {
580 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
581 sess->sess_bin_isid) {
582 isid_mismatch = true;
583 goto check_nonholder;
590 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
591 return TCM_RESERVATION_CONFLICT;
595 static u32 core_scsi3_pr_generation(struct se_device *dev)
600 * PRGeneration field shall contain the value of a 32-bit wrapping
601 * counter mainted by the device server.
603 * Note that this is done regardless of Active Persist across
604 * Target PowerLoss (APTPL)
606 * See spc4r17 section 6.3.12 READ_KEYS service action
608 spin_lock(&dev->dev_reservation_lock);
609 prg = dev->t10_pr.pr_generation++;
610 spin_unlock(&dev->dev_reservation_lock);
615 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
616 struct se_device *dev,
617 struct se_node_acl *nacl,
619 struct se_dev_entry *dest_deve,
626 struct t10_pr_registration *pr_reg;
628 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
630 pr_err("Unable to allocate struct t10_pr_registration\n");
634 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
635 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
636 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
637 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
638 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
639 atomic_set(&pr_reg->pr_res_holders, 0);
640 pr_reg->pr_reg_nacl = nacl;
642 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
643 * the se_dev_entry->pr_ref will have been already obtained by
644 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
646 * Otherwise, locate se_dev_entry now and obtain a reference until
647 * registration completes in __core_scsi3_add_registration().
650 pr_reg->pr_reg_deve = dest_deve;
653 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
654 if (!pr_reg->pr_reg_deve) {
656 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
657 nacl->initiatorname, mapped_lun);
658 kmem_cache_free(t10_pr_reg_cache, pr_reg);
661 kref_get(&pr_reg->pr_reg_deve->pr_kref);
664 pr_reg->pr_res_mapped_lun = mapped_lun;
665 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
666 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
667 pr_reg->pr_res_key = sa_res_key;
668 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
669 pr_reg->pr_reg_aptpl = aptpl;
671 * If an ISID value for this SCSI Initiator Port exists,
672 * save it to the registration now.
675 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
676 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
677 pr_reg->isid_present_at_reg = 1;
683 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
684 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
687 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
690 static struct t10_pr_registration *__core_scsi3_alloc_registration(
691 struct se_device *dev,
692 struct se_node_acl *nacl,
694 struct se_dev_entry *deve,
701 struct se_dev_entry *deve_tmp;
702 struct se_node_acl *nacl_tmp;
703 struct se_lun_acl *lacl_tmp;
704 struct se_lun *lun_tmp, *next, *dest_lun;
705 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
706 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
709 * Create a registration for the I_T Nexus upon which the
710 * PROUT REGISTER was received.
712 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
713 isid, sa_res_key, all_tg_pt,
718 * Return pointer to pr_reg for ALL_TG_PT=0
723 * Create list of matching SCSI Initiator Port registrations
726 spin_lock(&dev->se_port_lock);
727 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
728 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
730 spin_unlock(&dev->se_port_lock);
732 spin_lock(&lun_tmp->lun_deve_lock);
733 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
735 * This pointer will be NULL for demo mode MappedLUNs
736 * that have not been make explicit via a ConfigFS
737 * MappedLUN group for the SCSI Initiator Node ACL.
739 if (!deve_tmp->se_lun_acl)
742 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
743 lockdep_is_held(&lun_tmp->lun_deve_lock));
744 nacl_tmp = lacl_tmp->se_lun_nacl;
746 * Skip the matching struct se_node_acl that is allocated
749 if (nacl == nacl_tmp)
752 * Only perform PR registrations for target ports on
753 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
756 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
759 * Look for a matching Initiator Node ACL in ASCII format
761 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
764 kref_get(&deve_tmp->pr_kref);
765 spin_unlock(&lun_tmp->lun_deve_lock);
767 * Grab a configfs group dependency that is released
768 * for the exception path at label out: below, or upon
769 * completion of adding ALL_TG_PT=1 registrations in
770 * __core_scsi3_add_registration()
772 ret = core_scsi3_lunacl_depend_item(deve_tmp);
774 pr_err("core_scsi3_lunacl_depend"
776 percpu_ref_put(&lun_tmp->lun_ref);
777 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
781 * Located a matching SCSI Initiator Port on a different
782 * port, allocate the pr_reg_atp and attach it to the
783 * pr_reg->pr_reg_atp_list that will be processed once
784 * the original *pr_reg is processed in
785 * __core_scsi3_add_registration()
787 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
788 kref_read(&deve_tmp->pr_kref) != 0);
790 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
791 nacl_tmp, dest_lun, deve_tmp,
792 deve_tmp->mapped_lun, NULL,
793 sa_res_key, all_tg_pt, aptpl);
795 percpu_ref_put(&lun_tmp->lun_ref);
796 core_scsi3_lunacl_undepend_item(deve_tmp);
800 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
801 &pr_reg->pr_reg_atp_list);
802 spin_lock(&lun_tmp->lun_deve_lock);
804 spin_unlock(&lun_tmp->lun_deve_lock);
806 spin_lock(&dev->se_port_lock);
807 percpu_ref_put(&lun_tmp->lun_ref);
809 spin_unlock(&dev->se_port_lock);
813 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
814 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
815 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
816 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
817 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
819 kmem_cache_free(t10_pr_reg_cache, pr_reg);
823 int core_scsi3_alloc_aptpl_registration(
824 struct t10_reservation *pr_tmpl,
826 unsigned char *i_port,
829 unsigned char *t_port,
836 struct t10_pr_registration *pr_reg;
838 if (!i_port || !t_port || !sa_res_key) {
839 pr_err("Illegal parameters for APTPL registration\n");
843 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
845 pr_err("Unable to allocate struct t10_pr_registration\n");
849 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
850 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
851 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
852 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
853 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
854 atomic_set(&pr_reg->pr_res_holders, 0);
855 pr_reg->pr_reg_nacl = NULL;
856 pr_reg->pr_reg_deve = NULL;
857 pr_reg->pr_res_mapped_lun = mapped_lun;
858 pr_reg->pr_aptpl_target_lun = target_lun;
859 pr_reg->pr_res_key = sa_res_key;
860 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
861 pr_reg->pr_reg_aptpl = 1;
862 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
863 pr_reg->pr_res_type = type;
865 * If an ISID value had been saved in APTPL metadata for this
866 * SCSI Initiator Port, restore it now.
869 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
870 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
871 pr_reg->isid_present_at_reg = 1;
874 * Copy the i_port and t_port information from caller.
876 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
877 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
878 pr_reg->pr_reg_tpgt = tpgt;
880 * Set pr_res_holder from caller, the pr_reg who is the reservation
881 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
882 * the Initiator Node LUN ACL from the fabric module is created for
885 pr_reg->pr_res_holder = res_holder;
887 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
888 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
889 " metadata\n", (res_holder) ? "+reservation" : "");
893 static void core_scsi3_aptpl_reserve(
894 struct se_device *dev,
895 struct se_portal_group *tpg,
896 struct se_node_acl *node_acl,
897 struct t10_pr_registration *pr_reg)
899 char i_buf[PR_REG_ISID_ID_LEN] = { };
901 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
903 spin_lock(&dev->dev_reservation_lock);
904 dev->dev_pr_res_holder = pr_reg;
905 spin_unlock(&dev->dev_reservation_lock);
907 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
908 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
909 tpg->se_tpg_tfo->fabric_name,
910 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
911 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
912 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
913 tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
917 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
918 struct t10_pr_registration *, enum register_type, int);
920 static int __core_scsi3_check_aptpl_registration(
921 struct se_device *dev,
922 struct se_portal_group *tpg,
925 struct se_node_acl *nacl,
928 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
929 struct t10_reservation *pr_tmpl = &dev->t10_pr;
930 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN] = { };
931 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN] = { };
935 * Copy Initiator Port information from struct se_node_acl
937 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
938 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
939 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
940 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
942 * Look for the matching registrations+reservation from those
943 * created from APTPL metadata. Note that multiple registrations
944 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
947 spin_lock(&pr_tmpl->aptpl_reg_lock);
948 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
951 if (!strcmp(pr_reg->pr_iport, i_port) &&
952 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
953 !(strcmp(pr_reg->pr_tport, t_port)) &&
954 (pr_reg->pr_reg_tpgt == tpgt) &&
955 (pr_reg->pr_aptpl_target_lun == target_lun)) {
957 * Obtain the ->pr_reg_deve pointer + reference, that
958 * is released by __core_scsi3_add_registration() below.
961 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
962 if (!pr_reg->pr_reg_deve) {
963 pr_err("Unable to locate PR APTPL %s mapped_lun:"
964 " %llu\n", nacl->initiatorname, mapped_lun);
968 kref_get(&pr_reg->pr_reg_deve->pr_kref);
971 pr_reg->pr_reg_nacl = nacl;
972 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
973 list_del(&pr_reg->pr_reg_aptpl_list);
974 spin_unlock(&pr_tmpl->aptpl_reg_lock);
976 * At this point all of the pointers in *pr_reg will
977 * be setup, so go ahead and add the registration.
979 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
981 * If this registration is the reservation holder,
982 * make that happen now..
984 if (pr_reg->pr_res_holder)
985 core_scsi3_aptpl_reserve(dev, tpg,
988 * Reenable pr_aptpl_active to accept new metadata
989 * updates once the SCSI device is active again..
991 spin_lock(&pr_tmpl->aptpl_reg_lock);
992 pr_tmpl->pr_aptpl_active = 1;
995 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1000 int core_scsi3_check_aptpl_registration(
1001 struct se_device *dev,
1002 struct se_portal_group *tpg,
1004 struct se_node_acl *nacl,
1007 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1010 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1011 lun->unpacked_lun, nacl,
1015 static void __core_scsi3_dump_registration(
1016 const struct target_core_fabric_ops *tfo,
1017 struct se_device *dev,
1018 struct se_node_acl *nacl,
1019 struct t10_pr_registration *pr_reg,
1020 enum register_type register_type)
1022 struct se_portal_group *se_tpg = nacl->se_tpg;
1023 char i_buf[PR_REG_ISID_ID_LEN] = { };
1025 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1027 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1028 " Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
1029 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1030 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1032 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1033 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
1034 tfo->tpg_get_tag(se_tpg));
1035 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1036 " Port(s)\n", tfo->fabric_name,
1037 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1038 dev->transport->name);
1039 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1040 " 0x%08x APTPL: %d\n", tfo->fabric_name,
1041 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1042 pr_reg->pr_reg_aptpl);
1045 static void __core_scsi3_add_registration(
1046 struct se_device *dev,
1047 struct se_node_acl *nacl,
1048 struct t10_pr_registration *pr_reg,
1049 enum register_type register_type,
1052 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1053 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1054 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1055 struct se_dev_entry *deve;
1058 * Increment PRgeneration counter for struct se_device upon a successful
1059 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1061 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1062 * action, the struct se_device->dev_reservation_lock will already be held,
1063 * so we do not call core_scsi3_pr_generation() which grabs the lock
1066 pr_reg->pr_res_generation = (register_move) ?
1067 dev->t10_pr.pr_generation++ :
1068 core_scsi3_pr_generation(dev);
1070 spin_lock(&pr_tmpl->registration_lock);
1071 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1073 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1074 spin_unlock(&pr_tmpl->registration_lock);
1076 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1078 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1081 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1082 * allocated in __core_scsi3_alloc_registration()
1084 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1085 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1086 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1088 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1090 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1092 spin_lock(&pr_tmpl->registration_lock);
1093 list_add_tail(&pr_reg_tmp->pr_reg_list,
1094 &pr_tmpl->registration_list);
1096 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1098 spin_unlock(&pr_tmpl->registration_lock);
1100 * Drop configfs group dependency reference and deve->pr_kref
1101 * obtained from __core_scsi3_alloc_registration() code.
1104 deve = pr_reg_tmp->pr_reg_deve;
1106 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1107 core_scsi3_lunacl_undepend_item(deve);
1108 pr_reg_tmp->pr_reg_deve = NULL;
1114 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1117 deve = pr_reg->pr_reg_deve;
1119 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1120 kref_put(&deve->pr_kref, target_pr_kref_release);
1121 pr_reg->pr_reg_deve = NULL;
1126 static int core_scsi3_alloc_registration(
1127 struct se_device *dev,
1128 struct se_node_acl *nacl,
1130 struct se_dev_entry *deve,
1132 unsigned char *isid,
1136 enum register_type register_type,
1139 struct t10_pr_registration *pr_reg;
1141 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1142 isid, sa_res_key, all_tg_pt,
1147 __core_scsi3_add_registration(dev, nacl, pr_reg,
1148 register_type, register_move);
1152 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1153 struct se_device *dev,
1154 struct se_node_acl *nacl,
1155 unsigned char *isid)
1157 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1158 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1160 spin_lock(&pr_tmpl->registration_lock);
1161 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1162 &pr_tmpl->registration_list, pr_reg_list) {
1164 * First look for a matching struct se_node_acl
1166 if (pr_reg->pr_reg_nacl != nacl)
1170 * If this registration does NOT contain a fabric provided
1171 * ISID, then we have found a match.
1173 if (!pr_reg->isid_present_at_reg) {
1174 atomic_inc_mb(&pr_reg->pr_res_holders);
1175 spin_unlock(&pr_tmpl->registration_lock);
1179 * If the *pr_reg contains a fabric defined ISID for multi-value
1180 * SCSI Initiator Port TransportIDs, then we expect a valid
1181 * matching ISID to be provided by the local SCSI Initiator Port.
1185 if (strcmp(isid, pr_reg->pr_reg_isid))
1188 atomic_inc_mb(&pr_reg->pr_res_holders);
1189 spin_unlock(&pr_tmpl->registration_lock);
1192 spin_unlock(&pr_tmpl->registration_lock);
1197 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1198 struct se_device *dev,
1199 struct se_node_acl *nacl,
1200 struct se_session *sess)
1202 struct se_portal_group *tpg = nacl->se_tpg;
1203 unsigned char buf[PR_REG_ISID_LEN] = { };
1204 unsigned char *isid_ptr = NULL;
1206 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1207 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1212 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1215 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1217 atomic_dec_mb(&pr_reg->pr_res_holders);
1220 static int core_scsi3_check_implicit_release(
1221 struct se_device *dev,
1222 struct t10_pr_registration *pr_reg)
1224 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1225 struct t10_pr_registration *pr_res_holder;
1228 spin_lock(&dev->dev_reservation_lock);
1229 pr_res_holder = dev->dev_pr_res_holder;
1230 if (!pr_res_holder) {
1231 spin_unlock(&dev->dev_reservation_lock);
1234 if (pr_res_holder == pr_reg) {
1236 * Perform an implicit RELEASE if the registration that
1237 * is being released is holding the reservation.
1239 * From spc4r17, section 5.7.11.1:
1241 * e) If the I_T nexus is the persistent reservation holder
1242 * and the persistent reservation is not an all registrants
1243 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1244 * service action or REGISTER AND IGNORE EXISTING KEY
1245 * service action with the SERVICE ACTION RESERVATION KEY
1246 * field set to zero (see 5.7.11.3).
1248 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1251 * For 'All Registrants' reservation types, all existing
1252 * registrations are still processed as reservation holders
1253 * in core_scsi3_pr_seq_non_holder() after the initial
1254 * reservation holder is implicitly released here.
1256 } else if (pr_reg->pr_reg_all_tg_pt &&
1257 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1258 pr_reg->pr_reg_nacl->initiatorname)) &&
1259 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1260 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1261 " UNREGISTER while existing reservation with matching"
1262 " key 0x%016Lx is present from another SCSI Initiator"
1263 " Port\n", pr_reg->pr_res_key);
1266 spin_unlock(&dev->dev_reservation_lock);
1271 static void __core_scsi3_free_registration(
1272 struct se_device *dev,
1273 struct t10_pr_registration *pr_reg,
1274 struct list_head *preempt_and_abort_list,
1276 __releases(&pr_tmpl->registration_lock)
1277 __acquires(&pr_tmpl->registration_lock)
1279 const struct target_core_fabric_ops *tfo =
1280 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1281 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1282 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1283 struct se_dev_entry *deve;
1284 char i_buf[PR_REG_ISID_ID_LEN] = { };
1286 lockdep_assert_held(&pr_tmpl->registration_lock);
1288 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1290 if (!list_empty(&pr_reg->pr_reg_list))
1291 list_del(&pr_reg->pr_reg_list);
1293 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1294 * so call core_scsi3_put_pr_reg() to decrement our reference.
1297 core_scsi3_put_pr_reg(pr_reg);
1299 spin_unlock(&pr_tmpl->registration_lock);
1301 * Wait until all reference from any other I_T nexuses for this
1302 * *pr_reg have been released. Because list_del() is called above,
1303 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1304 * count back to zero, and we release *pr_reg.
1306 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1307 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1313 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1315 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1318 spin_lock(&pr_tmpl->registration_lock);
1319 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1320 " Node: %s%s\n", tfo->fabric_name,
1321 pr_reg->pr_reg_nacl->initiatorname,
1323 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1324 " Port(s)\n", tfo->fabric_name,
1325 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1326 dev->transport->name);
1327 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1328 " 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
1329 pr_reg->pr_res_generation);
1331 if (!preempt_and_abort_list) {
1332 pr_reg->pr_reg_deve = NULL;
1333 pr_reg->pr_reg_nacl = NULL;
1334 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1338 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1339 * are released once the ABORT_TASK_SET has completed..
1341 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1344 void core_scsi3_free_pr_reg_from_nacl(
1345 struct se_device *dev,
1346 struct se_node_acl *nacl)
1348 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1349 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1350 bool free_reg = false;
1352 * If the passed se_node_acl matches the reservation holder,
1353 * release the reservation.
1355 spin_lock(&dev->dev_reservation_lock);
1356 pr_res_holder = dev->dev_pr_res_holder;
1357 if ((pr_res_holder != NULL) &&
1358 (pr_res_holder->pr_reg_nacl == nacl)) {
1359 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1362 spin_unlock(&dev->dev_reservation_lock);
1364 * Release any registration associated with the struct se_node_acl.
1366 spin_lock(&pr_tmpl->registration_lock);
1367 if (pr_res_holder && free_reg)
1368 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1370 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1371 &pr_tmpl->registration_list, pr_reg_list) {
1373 if (pr_reg->pr_reg_nacl != nacl)
1376 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1378 spin_unlock(&pr_tmpl->registration_lock);
1381 void core_scsi3_free_all_registrations(
1382 struct se_device *dev)
1384 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1385 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1387 spin_lock(&dev->dev_reservation_lock);
1388 pr_res_holder = dev->dev_pr_res_holder;
1389 if (pr_res_holder != NULL) {
1390 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1391 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1392 pr_res_holder, 0, 0);
1394 spin_unlock(&dev->dev_reservation_lock);
1396 spin_lock(&pr_tmpl->registration_lock);
1397 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1398 &pr_tmpl->registration_list, pr_reg_list) {
1400 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1402 spin_unlock(&pr_tmpl->registration_lock);
1404 spin_lock(&pr_tmpl->aptpl_reg_lock);
1405 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1406 pr_reg_aptpl_list) {
1407 list_del(&pr_reg->pr_reg_aptpl_list);
1408 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1410 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1413 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1415 return target_depend_item(&tpg->tpg_group.cg_item);
1418 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1420 target_undepend_item(&tpg->tpg_group.cg_item);
1421 atomic_dec_mb(&tpg->tpg_pr_ref_count);
1424 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1426 if (nacl->dynamic_node_acl)
1428 return target_depend_item(&nacl->acl_group.cg_item);
1431 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1433 if (!nacl->dynamic_node_acl)
1434 target_undepend_item(&nacl->acl_group.cg_item);
1435 atomic_dec_mb(&nacl->acl_pr_ref_count);
1438 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1440 struct se_lun_acl *lun_acl;
1443 * For nacl->dynamic_node_acl=1
1445 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1446 kref_read(&se_deve->pr_kref) != 0);
1450 return target_depend_item(&lun_acl->se_lun_group.cg_item);
1453 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1455 struct se_lun_acl *lun_acl;
1458 * For nacl->dynamic_node_acl=1
1460 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1461 kref_read(&se_deve->pr_kref) != 0);
1463 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1467 target_undepend_item(&lun_acl->se_lun_group.cg_item);
1468 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1471 static sense_reason_t
1472 core_scsi3_decode_spec_i_port(
1474 struct se_portal_group *tpg,
1475 unsigned char *l_isid,
1480 struct se_device *dev = cmd->se_dev;
1481 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1482 struct se_session *se_sess = cmd->se_sess;
1483 struct se_node_acl *dest_node_acl = NULL;
1484 struct se_dev_entry *dest_se_deve = NULL;
1485 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1486 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1487 LIST_HEAD(tid_dest_list);
1488 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1489 unsigned char *buf, *ptr, proto_ident;
1490 const unsigned char *i_str = NULL;
1491 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1493 u32 tpdl, tid_len = 0;
1497 * Allocate a struct pr_transport_id_holder and setup the
1498 * local_node_acl pointer and add to struct list_head tid_dest_list
1499 * for add registration processing in the loop of tid_dest_list below.
1501 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1503 pr_err("Unable to allocate tidh_new\n");
1504 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1506 INIT_LIST_HEAD(&tidh_new->dest_list);
1507 tidh_new->dest_tpg = tpg;
1508 tidh_new->dest_node_acl = se_sess->se_node_acl;
1510 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1511 se_sess->se_node_acl, cmd->se_lun,
1512 NULL, cmd->orig_fe_lun, l_isid,
1513 sa_res_key, all_tg_pt, aptpl);
1514 if (!local_pr_reg) {
1516 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1519 if (core_scsi3_lunacl_depend_item(local_pr_reg->pr_reg_deve)) {
1521 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1522 target_pr_kref_release);
1523 kmem_cache_free(t10_pr_reg_cache, local_pr_reg);
1524 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1527 tidh_new->dest_pr_reg = local_pr_reg;
1528 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1530 if (cmd->data_length < 28) {
1531 pr_warn("SPC-PR: Received PR OUT parameter list"
1532 " length too small: %u\n", cmd->data_length);
1533 ret = TCM_INVALID_PARAMETER_LIST;
1537 buf = transport_kmap_data_sg(cmd);
1539 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1544 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1545 * first extract TransportID Parameter Data Length, and make sure
1546 * the value matches up to the SCSI expected data transfer length.
1548 tpdl = get_unaligned_be32(&buf[24]);
1550 if ((tpdl + 28) != cmd->data_length) {
1551 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1552 " does not equal CDB data_length: %u\n", tpdl,
1554 ret = TCM_INVALID_PARAMETER_LIST;
1558 * Start processing the received transport IDs using the
1559 * receiving I_T Nexus portal's fabric dependent methods to
1560 * obtain the SCSI Initiator Port/Device Identifiers.
1565 struct se_lun *dest_lun, *tmp_lun;
1567 proto_ident = (ptr[0] & 0x0f);
1570 spin_lock(&dev->se_port_lock);
1571 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1572 tmp_tpg = tmp_lun->lun_tpg;
1575 * Look for the matching proto_ident provided by
1576 * the received TransportID
1578 if (tmp_tpg->proto_id != proto_ident)
1580 dest_rtpi = tmp_lun->lun_rtpi;
1583 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1584 ptr, &tid_len, &iport_ptr);
1588 * Determine if this SCSI device server requires that
1589 * SCSI Intiatior TransportID w/ ISIDs is enforced
1590 * for fabric modules (iSCSI) requiring them.
1592 if (tpg->se_tpg_tfo->sess_get_initiator_sid &&
1593 dev->dev_attrib.enforce_pr_isids &&
1595 pr_warn("SPC-PR: enforce_pr_isids is set but a isid has not been sent in the SPEC_I_PT data for %s.",
1597 ret = TCM_INVALID_PARAMETER_LIST;
1598 spin_unlock(&dev->se_port_lock);
1602 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1603 spin_unlock(&dev->se_port_lock);
1605 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1606 pr_err(" core_scsi3_tpg_depend_item()"
1608 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1609 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1613 * Locate the destination initiator ACL to be registered
1614 * from the decoded fabric module specific TransportID
1617 mutex_lock(&tmp_tpg->acl_node_mutex);
1618 dest_node_acl = __core_tpg_get_initiator_node_acl(
1621 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1622 mutex_unlock(&tmp_tpg->acl_node_mutex);
1624 if (!dest_node_acl) {
1625 core_scsi3_tpg_undepend_item(tmp_tpg);
1626 spin_lock(&dev->se_port_lock);
1630 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1631 pr_err("configfs_depend_item() failed"
1632 " for dest_node_acl->acl_group\n");
1633 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1634 core_scsi3_tpg_undepend_item(tmp_tpg);
1635 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1640 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s Port RTPI: %u\n",
1641 dest_tpg->se_tpg_tfo->fabric_name,
1642 dest_node_acl->initiatorname, dest_rtpi);
1644 spin_lock(&dev->se_port_lock);
1647 spin_unlock(&dev->se_port_lock);
1650 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1652 ret = TCM_INVALID_PARAMETER_LIST;
1656 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1657 " tid_len: %d for %s + %s\n",
1658 dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
1659 tpdl, tid_len, i_str, iport_ptr);
1661 if (tid_len > tpdl) {
1662 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1663 " %u for Transport ID: %s\n", tid_len, ptr);
1664 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1665 core_scsi3_tpg_undepend_item(dest_tpg);
1666 ret = TCM_INVALID_PARAMETER_LIST;
1670 * Locate the desintation struct se_dev_entry pointer for matching
1671 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1674 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1676 if (!dest_se_deve) {
1677 pr_err("Unable to locate %s dest_se_deve from destination RTPI: %u\n",
1678 dest_tpg->se_tpg_tfo->fabric_name,
1681 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1682 core_scsi3_tpg_undepend_item(dest_tpg);
1683 ret = TCM_INVALID_PARAMETER_LIST;
1687 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1688 pr_err("core_scsi3_lunacl_depend_item()"
1690 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1691 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1692 core_scsi3_tpg_undepend_item(dest_tpg);
1693 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1697 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1698 " dest_se_deve mapped_lun: %llu\n",
1699 dest_tpg->se_tpg_tfo->fabric_name,
1700 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1703 * Skip any TransportIDs that already have a registration for
1706 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1709 core_scsi3_put_pr_reg(pr_reg_e);
1710 core_scsi3_lunacl_undepend_item(dest_se_deve);
1711 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1712 core_scsi3_tpg_undepend_item(dest_tpg);
1719 * Allocate a struct pr_transport_id_holder and setup
1720 * the dest_node_acl and dest_se_deve pointers for the
1723 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1726 pr_err("Unable to allocate tidh_new\n");
1727 core_scsi3_lunacl_undepend_item(dest_se_deve);
1728 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1729 core_scsi3_tpg_undepend_item(dest_tpg);
1730 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1733 INIT_LIST_HEAD(&tidh_new->dest_list);
1734 tidh_new->dest_tpg = dest_tpg;
1735 tidh_new->dest_node_acl = dest_node_acl;
1736 tidh_new->dest_se_deve = dest_se_deve;
1739 * Allocate, but do NOT add the registration for the
1740 * TransportID referenced SCSI Initiator port. This
1741 * done because of the following from spc4r17 in section
1742 * 6.14.3 wrt SPEC_I_PT:
1744 * "If a registration fails for any initiator port (e.g., if th
1745 * logical unit does not have enough resources available to
1746 * hold the registration information), no registrations shall be
1747 * made, and the command shall be terminated with
1748 * CHECK CONDITION status."
1750 * That means we call __core_scsi3_alloc_registration() here,
1751 * and then call __core_scsi3_add_registration() in the
1752 * 2nd loop which will never fail.
1754 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
1755 kref_read(&dest_se_deve->pr_kref) != 0);
1757 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1758 dest_node_acl, dest_lun, dest_se_deve,
1759 dest_se_deve->mapped_lun, iport_ptr,
1760 sa_res_key, all_tg_pt, aptpl);
1762 core_scsi3_lunacl_undepend_item(dest_se_deve);
1763 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1764 core_scsi3_tpg_undepend_item(dest_tpg);
1766 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1769 tidh_new->dest_pr_reg = dest_pr_reg;
1770 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1778 transport_kunmap_data_sg(cmd);
1781 * Go ahead and create a registrations from tid_dest_list for the
1782 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1785 * The SA Reservation Key from the PROUT is set for the
1786 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1787 * means that the TransportID Initiator port will be
1788 * registered on all of the target ports in the SCSI target device
1789 * ALL_TG_PT=0 means the registration will only be for the
1790 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1793 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1794 dest_tpg = tidh->dest_tpg;
1795 dest_node_acl = tidh->dest_node_acl;
1796 dest_se_deve = tidh->dest_se_deve;
1797 dest_pr_reg = tidh->dest_pr_reg;
1799 list_del(&tidh->dest_list);
1802 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1803 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1805 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1808 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1809 " registered Transport ID for Node: %s%s Mapped LUN:"
1810 " %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
1811 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1812 dest_se_deve->mapped_lun : 0);
1814 if (dest_pr_reg == local_pr_reg)
1817 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1818 core_scsi3_tpg_undepend_item(dest_tpg);
1823 transport_kunmap_data_sg(cmd);
1826 * For the failure case, release everything from tid_dest_list
1827 * including *dest_pr_reg and the configfs dependances..
1829 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1830 bool is_local = false;
1832 dest_tpg = tidh->dest_tpg;
1833 dest_node_acl = tidh->dest_node_acl;
1834 dest_se_deve = tidh->dest_se_deve;
1835 dest_pr_reg = tidh->dest_pr_reg;
1837 if (dest_pr_reg == local_pr_reg)
1840 list_del(&tidh->dest_list);
1843 * Release any extra ALL_TG_PT=1 registrations for
1844 * the SPEC_I_PT=1 case.
1846 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1847 &dest_pr_reg->pr_reg_atp_list,
1848 pr_reg_atp_mem_list) {
1849 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1850 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1851 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1854 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1855 core_scsi3_lunacl_undepend_item(dest_se_deve);
1860 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1861 core_scsi3_tpg_undepend_item(dest_tpg);
1866 static int core_scsi3_update_aptpl_buf(
1867 struct se_device *dev,
1869 u32 pr_aptpl_buf_len)
1871 struct se_portal_group *tpg;
1872 struct t10_pr_registration *pr_reg;
1873 unsigned char tmp[512], isid_buf[32];
1878 spin_lock(&dev->dev_reservation_lock);
1879 spin_lock(&dev->t10_pr.registration_lock);
1881 * Walk the registration list..
1883 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1888 tpg = pr_reg->pr_reg_nacl->se_tpg;
1890 * Write out any ISID value to APTPL metadata that was included
1891 * in the original registration.
1893 if (pr_reg->isid_present_at_reg)
1894 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1895 pr_reg->pr_reg_isid);
1897 * Include special metadata if the pr_reg matches the
1898 * reservation holder.
1900 if (dev->dev_pr_res_holder == pr_reg) {
1901 snprintf(tmp, 512, "PR_REG_START: %d"
1902 "\ninitiator_fabric=%s\n"
1903 "initiator_node=%s\n%s"
1905 "res_holder=1\nres_type=%02x\n"
1906 "res_scope=%02x\nres_all_tg_pt=%d\n"
1907 "mapped_lun=%llu\n", reg_count,
1908 tpg->se_tpg_tfo->fabric_name,
1909 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1910 pr_reg->pr_res_key, pr_reg->pr_res_type,
1911 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1912 pr_reg->pr_res_mapped_lun);
1914 snprintf(tmp, 512, "PR_REG_START: %d\n"
1915 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1916 "sa_res_key=%llu\nres_holder=0\n"
1917 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
1918 reg_count, tpg->se_tpg_tfo->fabric_name,
1919 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1920 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1921 pr_reg->pr_res_mapped_lun);
1924 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1925 pr_err("Unable to update renaming APTPL metadata,"
1926 " reallocating larger buffer\n");
1930 len += sprintf(buf+len, "%s", tmp);
1933 * Include information about the associated SCSI target port.
1935 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1936 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
1937 " %d\n", tpg->se_tpg_tfo->fabric_name,
1938 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1939 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1940 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1943 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1944 pr_err("Unable to update renaming APTPL metadata,"
1945 " reallocating larger buffer\n");
1949 len += sprintf(buf+len, "%s", tmp);
1954 len += sprintf(buf+len, "No Registrations or Reservations");
1957 spin_unlock(&dev->t10_pr.registration_lock);
1958 spin_unlock(&dev->dev_reservation_lock);
1963 static int __core_scsi3_write_aptpl_to_file(
1964 struct se_device *dev,
1967 struct t10_wwn *wwn = &dev->t10_wwn;
1969 int flags = O_RDWR | O_CREAT | O_TRUNC;
1971 u32 pr_aptpl_buf_len;
1975 path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
1976 &wwn->unit_serial[0]);
1980 file = filp_open(path, flags, 0600);
1982 pr_err("filp_open(%s) for APTPL metadata"
1985 return PTR_ERR(file);
1988 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
1990 ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
1993 pr_debug("Error writing APTPL metadata file: %s\n", path);
1997 return (ret < 0) ? -EIO : 0;
2001 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2002 * write out the updated metadata to struct file for this SCSI device.
2004 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
2007 int rc, len = PR_APTPL_BUF_LEN;
2010 char *null_buf = "No Registrations or Reservations\n";
2012 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2013 dev->t10_pr.pr_aptpl_active = 0;
2014 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
2017 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2024 return TCM_OUT_OF_RESOURCES;
2026 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
2033 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2035 pr_err("SPC-3 PR: Could not update APTPL\n");
2037 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2039 dev->t10_pr.pr_aptpl_active = 1;
2041 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2045 static sense_reason_t
2046 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2047 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2049 struct se_session *se_sess = cmd->se_sess;
2050 struct se_device *dev = cmd->se_dev;
2051 struct se_lun *se_lun = cmd->se_lun;
2052 struct se_portal_group *se_tpg;
2053 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2054 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2055 unsigned char isid_buf[PR_REG_ISID_LEN] = { };
2056 unsigned char *isid_ptr = NULL;
2057 sense_reason_t ret = TCM_NO_SENSE;
2058 int pr_holder = 0, type;
2060 if (!se_sess || !se_lun) {
2061 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2062 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2064 se_tpg = se_sess->se_tpg;
2066 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2067 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2069 isid_ptr = &isid_buf[0];
2072 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2074 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2077 pr_warn("SPC-3 PR: Reservation Key non-zero"
2078 " for SA REGISTER, returning CONFLICT\n");
2079 return TCM_RESERVATION_CONFLICT;
2082 * Do nothing but return GOOD status.
2089 * Perform the Service Action REGISTER on the Initiator
2090 * Port Endpoint that the PRO was received from on the
2091 * Logical Unit of the SCSI device server.
2093 if (core_scsi3_alloc_registration(cmd->se_dev,
2094 se_sess->se_node_acl, cmd->se_lun,
2095 NULL, cmd->orig_fe_lun, isid_ptr,
2096 sa_res_key, all_tg_pt, aptpl,
2097 register_type, 0)) {
2098 pr_err("Unable to allocate"
2099 " struct t10_pr_registration\n");
2100 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
2104 * Register both the Initiator port that received
2105 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2106 * TransportID from Parameter list and loop through
2107 * fabric dependent parameter list while calling
2108 * logic from of core_scsi3_alloc_registration() for
2109 * each TransportID provided SCSI Initiator Port/Device
2111 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2112 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2116 return core_scsi3_update_and_write_aptpl(dev, aptpl);
2119 /* ok, existing registration */
2121 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2122 pr_err("SPC-3 PR REGISTER: Received"
2123 " res_key: 0x%016Lx does not match"
2124 " existing SA REGISTER res_key:"
2125 " 0x%016Lx\n", res_key,
2126 pr_reg->pr_res_key);
2127 ret = TCM_RESERVATION_CONFLICT;
2132 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2133 " set on a registered nexus\n");
2134 ret = TCM_INVALID_PARAMETER_LIST;
2139 * An existing ALL_TG_PT=1 registration being released
2140 * must also set ALL_TG_PT=1 in the incoming PROUT.
2142 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2143 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2144 " registration exists, but ALL_TG_PT=1 bit not"
2145 " present in received PROUT\n");
2146 ret = TCM_INVALID_CDB_FIELD;
2151 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2155 * Increment PRgeneration counter for struct se_device"
2156 * upon a successful REGISTER, see spc4r17 section 6.3.2
2157 * READ_KEYS service action.
2159 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2160 pr_reg->pr_res_key = sa_res_key;
2161 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2162 " Key for %s to: 0x%016Lx PRgeneration:"
2163 " 0x%08x\n", cmd->se_tfo->fabric_name,
2164 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2165 pr_reg->pr_reg_nacl->initiatorname,
2166 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2170 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2172 type = pr_reg->pr_res_type;
2173 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2175 if (pr_holder < 0) {
2176 ret = TCM_RESERVATION_CONFLICT;
2180 spin_lock(&pr_tmpl->registration_lock);
2182 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2183 * and matching pr_res_key.
2185 if (pr_reg->pr_reg_all_tg_pt) {
2186 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2187 &pr_tmpl->registration_list,
2190 if (!pr_reg_p->pr_reg_all_tg_pt)
2192 if (pr_reg_p->pr_res_key != res_key)
2194 if (pr_reg == pr_reg_p)
2196 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2197 pr_reg_p->pr_reg_nacl->initiatorname))
2200 __core_scsi3_free_registration(dev,
2206 * Release the calling I_T Nexus registration now..
2208 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2212 * From spc4r17, section 5.7.11.3 Unregistering
2214 * If the persistent reservation is a registrants only
2215 * type, the device server shall establish a unit
2216 * attention condition for the initiator port associated
2217 * with every registered I_T nexus except for the I_T
2218 * nexus on which the PERSISTENT RESERVE OUT command was
2219 * received, with the additional sense code set to
2220 * RESERVATIONS RELEASED.
2223 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2224 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2225 list_for_each_entry(pr_reg_p,
2226 &pr_tmpl->registration_list,
2229 target_ua_allocate_lun(
2230 pr_reg_p->pr_reg_nacl,
2231 pr_reg_p->pr_res_mapped_lun,
2233 ASCQ_2AH_RESERVATIONS_RELEASED);
2237 spin_unlock(&pr_tmpl->registration_lock);
2240 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2244 core_scsi3_put_pr_reg(pr_reg);
2248 unsigned char *core_scsi3_pr_dump_type(int type)
2251 case PR_TYPE_WRITE_EXCLUSIVE:
2252 return "Write Exclusive Access";
2253 case PR_TYPE_EXCLUSIVE_ACCESS:
2254 return "Exclusive Access";
2255 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2256 return "Write Exclusive Access, Registrants Only";
2257 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2258 return "Exclusive Access, Registrants Only";
2259 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2260 return "Write Exclusive Access, All Registrants";
2261 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2262 return "Exclusive Access, All Registrants";
2267 return "Unknown SPC-3 PR Type";
2270 static sense_reason_t
2271 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2273 struct se_device *dev = cmd->se_dev;
2274 struct se_session *se_sess = cmd->se_sess;
2275 struct se_lun *se_lun = cmd->se_lun;
2276 struct t10_pr_registration *pr_reg, *pr_res_holder;
2277 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2278 char i_buf[PR_REG_ISID_ID_LEN] = { };
2281 if (!se_sess || !se_lun) {
2282 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2283 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2286 * Locate the existing *pr_reg via struct se_node_acl pointers
2288 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2291 pr_err("SPC-3 PR: Unable to locate"
2292 " PR_REGISTERED *pr_reg for RESERVE\n");
2293 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2296 * From spc4r17 Section 5.7.9: Reserving:
2298 * An application client creates a persistent reservation by issuing
2299 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2300 * a registered I_T nexus with the following parameters:
2301 * a) RESERVATION KEY set to the value of the reservation key that is
2302 * registered with the logical unit for the I_T nexus; and
2304 if (res_key != pr_reg->pr_res_key) {
2305 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2306 " does not match existing SA REGISTER res_key:"
2307 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2308 ret = TCM_RESERVATION_CONFLICT;
2309 goto out_put_pr_reg;
2312 * From spc4r17 Section 5.7.9: Reserving:
2315 * b) TYPE field and SCOPE field set to the persistent reservation
2318 * Only one persistent reservation is allowed at a time per logical unit
2319 * and that persistent reservation has a scope of LU_SCOPE.
2321 if (scope != PR_SCOPE_LU_SCOPE) {
2322 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2323 ret = TCM_INVALID_PARAMETER_LIST;
2324 goto out_put_pr_reg;
2327 * See if we have an existing PR reservation holder pointer at
2328 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2331 spin_lock(&dev->dev_reservation_lock);
2332 pr_res_holder = dev->dev_pr_res_holder;
2333 if (pr_res_holder) {
2335 * From spc4r17 Section 5.7.9: Reserving:
2337 * If the device server receives a PERSISTENT RESERVE OUT
2338 * command from an I_T nexus other than a persistent reservation
2339 * holder (see 5.7.10) that attempts to create a persistent
2340 * reservation when a persistent reservation already exists for
2341 * the logical unit, then the command shall be completed with
2342 * RESERVATION CONFLICT status.
2344 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2345 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2346 pr_err("SPC-3 PR: Attempted RESERVE from"
2347 " [%s]: %s while reservation already held by"
2348 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2349 cmd->se_tfo->fabric_name,
2350 se_sess->se_node_acl->initiatorname,
2351 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2352 pr_res_holder->pr_reg_nacl->initiatorname);
2354 spin_unlock(&dev->dev_reservation_lock);
2355 ret = TCM_RESERVATION_CONFLICT;
2356 goto out_put_pr_reg;
2359 * From spc4r17 Section 5.7.9: Reserving:
2361 * If a persistent reservation holder attempts to modify the
2362 * type or scope of an existing persistent reservation, the
2363 * command shall be completed with RESERVATION CONFLICT status.
2365 if ((pr_res_holder->pr_res_type != type) ||
2366 (pr_res_holder->pr_res_scope != scope)) {
2367 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2368 pr_err("SPC-3 PR: Attempted RESERVE from"
2369 " [%s]: %s trying to change TYPE and/or SCOPE,"
2370 " while reservation already held by [%s]: %s,"
2371 " returning RESERVATION_CONFLICT\n",
2372 cmd->se_tfo->fabric_name,
2373 se_sess->se_node_acl->initiatorname,
2374 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2375 pr_res_holder->pr_reg_nacl->initiatorname);
2377 spin_unlock(&dev->dev_reservation_lock);
2378 ret = TCM_RESERVATION_CONFLICT;
2379 goto out_put_pr_reg;
2382 * From spc4r17 Section 5.7.9: Reserving:
2384 * If the device server receives a PERSISTENT RESERVE OUT
2385 * command with RESERVE service action where the TYPE field and
2386 * the SCOPE field contain the same values as the existing type
2387 * and scope from a persistent reservation holder, it shall not
2388 * make any change to the existing persistent reservation and
2389 * shall completethe command with GOOD status.
2391 spin_unlock(&dev->dev_reservation_lock);
2393 goto out_put_pr_reg;
2396 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2397 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2399 pr_reg->pr_res_scope = scope;
2400 pr_reg->pr_res_type = type;
2401 pr_reg->pr_res_holder = 1;
2402 dev->dev_pr_res_holder = pr_reg;
2403 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2405 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2406 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2407 cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
2408 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2409 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2410 cmd->se_tfo->fabric_name,
2411 se_sess->se_node_acl->initiatorname,
2413 spin_unlock(&dev->dev_reservation_lock);
2415 if (pr_tmpl->pr_aptpl_active)
2416 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2420 core_scsi3_put_pr_reg(pr_reg);
2424 static sense_reason_t
2425 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2429 case PR_TYPE_WRITE_EXCLUSIVE:
2430 case PR_TYPE_EXCLUSIVE_ACCESS:
2431 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2432 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2433 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2434 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2435 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2437 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2439 return TCM_INVALID_CDB_FIELD;
2443 static void __core_scsi3_complete_pro_release(
2444 struct se_device *dev,
2445 struct se_node_acl *se_nacl,
2446 struct t10_pr_registration *pr_reg,
2450 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2451 char i_buf[PR_REG_ISID_ID_LEN] = { };
2452 int pr_res_type = 0, pr_res_scope = 0;
2454 lockdep_assert_held(&dev->dev_reservation_lock);
2456 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2458 * Go ahead and release the current PR reservation holder.
2459 * If an All Registrants reservation is currently active and
2460 * a unregister operation is requested, replace the current
2461 * dev_pr_res_holder with another active registration.
2463 if (dev->dev_pr_res_holder) {
2464 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2465 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2466 dev->dev_pr_res_holder->pr_res_type = 0;
2467 dev->dev_pr_res_holder->pr_res_scope = 0;
2468 dev->dev_pr_res_holder->pr_res_holder = 0;
2469 dev->dev_pr_res_holder = NULL;
2474 spin_lock(&dev->t10_pr.registration_lock);
2475 list_del_init(&pr_reg->pr_reg_list);
2477 * If the I_T nexus is a reservation holder, the persistent reservation
2478 * is of an all registrants type, and the I_T nexus is the last remaining
2479 * registered I_T nexus, then the device server shall also release the
2480 * persistent reservation.
2482 if (!list_empty(&dev->t10_pr.registration_list) &&
2483 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2484 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2485 dev->dev_pr_res_holder =
2486 list_entry(dev->t10_pr.registration_list.next,
2487 struct t10_pr_registration, pr_reg_list);
2488 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2489 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2490 dev->dev_pr_res_holder->pr_res_holder = 1;
2492 spin_unlock(&dev->t10_pr.registration_lock);
2494 if (!dev->dev_pr_res_holder) {
2495 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2496 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2497 tfo->fabric_name, (explicit) ? "explicit" :
2498 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2499 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2501 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2502 tfo->fabric_name, se_nacl->initiatorname,
2505 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2507 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2510 static sense_reason_t
2511 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2514 struct se_device *dev = cmd->se_dev;
2515 struct se_session *se_sess = cmd->se_sess;
2516 struct se_lun *se_lun = cmd->se_lun;
2517 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2518 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2519 sense_reason_t ret = 0;
2521 if (!se_sess || !se_lun) {
2522 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2523 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2526 * Locate the existing *pr_reg via struct se_node_acl pointers
2528 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2530 pr_err("SPC-3 PR: Unable to locate"
2531 " PR_REGISTERED *pr_reg for RELEASE\n");
2532 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2535 * From spc4r17 Section 5.7.11.2 Releasing:
2537 * If there is no persistent reservation or in response to a persistent
2538 * reservation release request from a registered I_T nexus that is not a
2539 * persistent reservation holder (see 5.7.10), the device server shall
2542 * a) Not release the persistent reservation, if any;
2543 * b) Not remove any registrations; and
2544 * c) Complete the command with GOOD status.
2546 spin_lock(&dev->dev_reservation_lock);
2547 pr_res_holder = dev->dev_pr_res_holder;
2548 if (!pr_res_holder) {
2550 * No persistent reservation, return GOOD status.
2552 spin_unlock(&dev->dev_reservation_lock);
2553 goto out_put_pr_reg;
2556 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2558 * Release request from a registered I_T nexus that is not a
2559 * persistent reservation holder. return GOOD status.
2561 spin_unlock(&dev->dev_reservation_lock);
2562 goto out_put_pr_reg;
2566 * From spc4r17 Section 5.7.11.2 Releasing:
2568 * Only the persistent reservation holder (see 5.7.10) is allowed to
2569 * release a persistent reservation.
2571 * An application client releases the persistent reservation by issuing
2572 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2573 * an I_T nexus that is a persistent reservation holder with the
2574 * following parameters:
2576 * a) RESERVATION KEY field set to the value of the reservation key
2577 * that is registered with the logical unit for the I_T nexus;
2579 if (res_key != pr_reg->pr_res_key) {
2580 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2581 " does not match existing SA REGISTER res_key:"
2582 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2583 spin_unlock(&dev->dev_reservation_lock);
2584 ret = TCM_RESERVATION_CONFLICT;
2585 goto out_put_pr_reg;
2588 * From spc4r17 Section 5.7.11.2 Releasing and above:
2590 * b) TYPE field and SCOPE field set to match the persistent
2591 * reservation being released.
2593 if ((pr_res_holder->pr_res_type != type) ||
2594 (pr_res_holder->pr_res_scope != scope)) {
2595 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2596 pr_err("SPC-3 PR RELEASE: Attempted to release"
2597 " reservation from [%s]: %s with different TYPE "
2598 "and/or SCOPE while reservation already held by"
2599 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2600 cmd->se_tfo->fabric_name,
2601 se_sess->se_node_acl->initiatorname,
2602 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2603 pr_res_holder->pr_reg_nacl->initiatorname);
2605 spin_unlock(&dev->dev_reservation_lock);
2606 ret = TCM_RESERVATION_CONFLICT;
2607 goto out_put_pr_reg;
2610 * In response to a persistent reservation release request from the
2611 * persistent reservation holder the device server shall perform a
2612 * release by doing the following as an uninterrupted series of actions:
2613 * a) Release the persistent reservation;
2614 * b) Not remove any registration(s);
2615 * c) If the released persistent reservation is a registrants only type
2616 * or all registrants type persistent reservation,
2617 * the device server shall establish a unit attention condition for
2618 * the initiator port associated with every regis-
2619 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2620 * RESERVE OUT command with RELEASE service action was received,
2621 * with the additional sense code set to RESERVATIONS RELEASED; and
2622 * d) If the persistent reservation is of any other type, the device
2623 * server shall not establish a unit attention condition.
2625 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2628 spin_unlock(&dev->dev_reservation_lock);
2630 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2631 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2632 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2633 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2635 * If no UNIT ATTENTION conditions will be established for
2636 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2637 * go ahead and check for APTPL=1 update+write below
2642 spin_lock(&pr_tmpl->registration_lock);
2643 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2646 * Do not establish a UNIT ATTENTION condition
2647 * for the calling I_T Nexus
2649 if (pr_reg_p == pr_reg)
2652 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2653 pr_reg_p->pr_res_mapped_lun,
2654 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2656 spin_unlock(&pr_tmpl->registration_lock);
2659 if (pr_tmpl->pr_aptpl_active)
2660 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2663 core_scsi3_put_pr_reg(pr_reg);
2667 static sense_reason_t
2668 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2670 struct se_device *dev = cmd->se_dev;
2671 struct se_node_acl *pr_reg_nacl;
2672 struct se_session *se_sess = cmd->se_sess;
2673 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2674 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2675 u64 pr_res_mapped_lun = 0;
2676 int calling_it_nexus = 0;
2678 * Locate the existing *pr_reg via struct se_node_acl pointers
2680 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2681 se_sess->se_node_acl, se_sess);
2683 pr_err("SPC-3 PR: Unable to locate"
2684 " PR_REGISTERED *pr_reg for CLEAR\n");
2685 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2688 * From spc4r17 section 5.7.11.6, Clearing:
2690 * Any application client may release the persistent reservation and
2691 * remove all registrations from a device server by issuing a
2692 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2693 * registered I_T nexus with the following parameter:
2695 * a) RESERVATION KEY field set to the value of the reservation key
2696 * that is registered with the logical unit for the I_T nexus.
2698 if (res_key != pr_reg_n->pr_res_key) {
2699 pr_err("SPC-3 PR REGISTER: Received"
2700 " res_key: 0x%016Lx does not match"
2701 " existing SA REGISTER res_key:"
2702 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2703 core_scsi3_put_pr_reg(pr_reg_n);
2704 return TCM_RESERVATION_CONFLICT;
2707 * a) Release the persistent reservation, if any;
2709 spin_lock(&dev->dev_reservation_lock);
2710 pr_res_holder = dev->dev_pr_res_holder;
2711 if (pr_res_holder) {
2712 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2713 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2714 pr_res_holder, 0, 0);
2716 spin_unlock(&dev->dev_reservation_lock);
2718 * b) Remove all registration(s) (see spc4r17 5.7.7);
2720 spin_lock(&pr_tmpl->registration_lock);
2721 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2722 &pr_tmpl->registration_list, pr_reg_list) {
2724 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2725 pr_reg_nacl = pr_reg->pr_reg_nacl;
2726 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2727 __core_scsi3_free_registration(dev, pr_reg, NULL,
2730 * e) Establish a unit attention condition for the initiator
2731 * port associated with every registered I_T nexus other
2732 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2733 * command with CLEAR service action was received, with the
2734 * additional sense code set to RESERVATIONS PREEMPTED.
2736 if (!calling_it_nexus)
2737 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2738 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2740 spin_unlock(&pr_tmpl->registration_lock);
2742 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2743 cmd->se_tfo->fabric_name);
2745 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2747 core_scsi3_pr_generation(dev);
2751 static void __core_scsi3_complete_pro_preempt(
2752 struct se_device *dev,
2753 struct t10_pr_registration *pr_reg,
2754 struct list_head *preempt_and_abort_list,
2757 enum preempt_type preempt_type)
2759 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2760 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2761 char i_buf[PR_REG_ISID_ID_LEN] = { };
2763 lockdep_assert_held(&dev->dev_reservation_lock);
2765 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2767 * Do an implicit RELEASE of the existing reservation.
2769 if (dev->dev_pr_res_holder)
2770 __core_scsi3_complete_pro_release(dev, nacl,
2771 dev->dev_pr_res_holder, 0, 0);
2773 dev->dev_pr_res_holder = pr_reg;
2774 pr_reg->pr_res_holder = 1;
2775 pr_reg->pr_res_type = type;
2776 pr_reg->pr_res_scope = scope;
2778 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2779 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2780 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2781 core_scsi3_pr_dump_type(type),
2782 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2783 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2784 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2785 nacl->initiatorname, i_buf);
2787 * For PREEMPT_AND_ABORT, add the preempting reservation's
2788 * struct t10_pr_registration to the list that will be compared
2789 * against received CDBs..
2791 if (preempt_and_abort_list)
2792 list_add_tail(&pr_reg->pr_reg_abort_list,
2793 preempt_and_abort_list);
2796 static void core_scsi3_release_preempt_and_abort(
2797 struct list_head *preempt_and_abort_list,
2798 struct t10_pr_registration *pr_reg_holder)
2800 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2802 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2803 pr_reg_abort_list) {
2805 list_del(&pr_reg->pr_reg_abort_list);
2806 if (pr_reg_holder == pr_reg)
2808 if (pr_reg->pr_res_holder) {
2809 pr_warn("pr_reg->pr_res_holder still set\n");
2813 pr_reg->pr_reg_deve = NULL;
2814 pr_reg->pr_reg_nacl = NULL;
2815 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2819 static sense_reason_t
2820 core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2821 u64 sa_res_key, enum preempt_type preempt_type)
2823 struct se_device *dev = cmd->se_dev;
2824 struct se_node_acl *pr_reg_nacl;
2825 struct se_session *se_sess = cmd->se_sess;
2826 LIST_HEAD(preempt_and_abort_list);
2827 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2828 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2829 u64 pr_res_mapped_lun = 0;
2830 int all_reg = 0, calling_it_nexus = 0;
2831 bool sa_res_key_unmatched = sa_res_key != 0;
2832 int prh_type = 0, prh_scope = 0;
2835 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2837 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2840 pr_err("SPC-3 PR: Unable to locate"
2841 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2842 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2843 return TCM_RESERVATION_CONFLICT;
2845 if (pr_reg_n->pr_res_key != res_key) {
2846 core_scsi3_put_pr_reg(pr_reg_n);
2847 return TCM_RESERVATION_CONFLICT;
2849 if (scope != PR_SCOPE_LU_SCOPE) {
2850 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2851 core_scsi3_put_pr_reg(pr_reg_n);
2852 return TCM_INVALID_PARAMETER_LIST;
2855 spin_lock(&dev->dev_reservation_lock);
2856 pr_res_holder = dev->dev_pr_res_holder;
2857 if (pr_res_holder &&
2858 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2859 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2862 if (!all_reg && !sa_res_key) {
2863 spin_unlock(&dev->dev_reservation_lock);
2864 core_scsi3_put_pr_reg(pr_reg_n);
2865 return TCM_INVALID_PARAMETER_LIST;
2868 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2870 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2871 * persistent reservation holder or there is no persistent reservation
2872 * holder (i.e., there is no persistent reservation), then the device
2873 * server shall perform a preempt by doing the following in an
2874 * uninterrupted series of actions. (See below..)
2876 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2878 * No existing or SA Reservation Key matching reservations..
2880 * PROUT SA PREEMPT with All Registrant type reservations are
2881 * allowed to be processed without a matching SA Reservation Key
2883 spin_lock(&pr_tmpl->registration_lock);
2884 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2885 &pr_tmpl->registration_list, pr_reg_list) {
2887 * Removing of registrations in non all registrants
2888 * type reservations without a matching SA reservation
2891 * a) Remove the registrations for all I_T nexuses
2892 * specified by the SERVICE ACTION RESERVATION KEY
2894 * b) Ignore the contents of the SCOPE and TYPE fields;
2895 * c) Process tasks as defined in 5.7.1; and
2896 * d) Establish a unit attention condition for the
2897 * initiator port associated with every I_T nexus
2898 * that lost its registration other than the I_T
2899 * nexus on which the PERSISTENT RESERVE OUT command
2900 * was received, with the additional sense code set
2901 * to REGISTRATIONS PREEMPTED.
2904 if (pr_reg->pr_res_key != sa_res_key)
2906 sa_res_key_unmatched = false;
2908 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2909 pr_reg_nacl = pr_reg->pr_reg_nacl;
2910 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2911 __core_scsi3_free_registration(dev, pr_reg,
2912 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2913 NULL, calling_it_nexus);
2916 * Case for any existing all registrants type
2917 * reservation, follow logic in spc4r17 section
2918 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2920 * For a ZERO SA Reservation key, release
2921 * all other registrations and do an implicit
2922 * release of active persistent reservation.
2924 * For a non-ZERO SA Reservation key, only
2925 * release the matching reservation key from
2929 (pr_reg->pr_res_key != sa_res_key))
2931 sa_res_key_unmatched = false;
2933 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2934 if (calling_it_nexus)
2937 pr_reg_nacl = pr_reg->pr_reg_nacl;
2938 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2939 __core_scsi3_free_registration(dev, pr_reg,
2940 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2943 if (!calling_it_nexus)
2944 target_ua_allocate_lun(pr_reg_nacl,
2945 pr_res_mapped_lun, 0x2A,
2946 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2948 spin_unlock(&pr_tmpl->registration_lock);
2950 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2951 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2952 * RESERVATION KEY field to a value that does not match any
2953 * registered reservation key, then the device server shall
2954 * complete the command with RESERVATION CONFLICT status.
2956 if (sa_res_key_unmatched) {
2957 spin_unlock(&dev->dev_reservation_lock);
2958 core_scsi3_put_pr_reg(pr_reg_n);
2959 return TCM_RESERVATION_CONFLICT;
2962 * For an existing all registrants type reservation
2963 * with a zero SA rservation key, preempt the existing
2964 * reservation with the new PR type and scope.
2966 if (pr_res_holder && all_reg && !(sa_res_key)) {
2967 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
2968 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2969 type, scope, preempt_type);
2971 if (preempt_type == PREEMPT_AND_ABORT)
2972 core_scsi3_release_preempt_and_abort(
2973 &preempt_and_abort_list, pr_reg_n);
2975 spin_unlock(&dev->dev_reservation_lock);
2977 if (pr_tmpl->pr_aptpl_active)
2978 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2980 core_scsi3_put_pr_reg(pr_reg_n);
2981 core_scsi3_pr_generation(cmd->se_dev);
2985 * The PREEMPTing SA reservation key matches that of the
2986 * existing persistent reservation, first, we check if
2987 * we are preempting our own reservation.
2988 * From spc4r17, section 5.7.11.4.3 Preempting
2989 * persistent reservations and registration handling
2991 * If an all registrants persistent reservation is not
2992 * present, it is not an error for the persistent
2993 * reservation holder to preempt itself (i.e., a
2994 * PERSISTENT RESERVE OUT with a PREEMPT service action
2995 * or a PREEMPT AND ABORT service action with the
2996 * SERVICE ACTION RESERVATION KEY value equal to the
2997 * persistent reservation holder's reservation key that
2998 * is received from the persistent reservation holder).
2999 * In that case, the device server shall establish the
3000 * new persistent reservation and maintain the
3003 prh_type = pr_res_holder->pr_res_type;
3004 prh_scope = pr_res_holder->pr_res_scope;
3006 * If the SERVICE ACTION RESERVATION KEY field identifies a
3007 * persistent reservation holder (see 5.7.10), the device
3008 * server shall perform a preempt by doing the following as
3009 * an uninterrupted series of actions:
3011 * a) Release the persistent reservation for the holder
3012 * identified by the SERVICE ACTION RESERVATION KEY field;
3014 if (pr_reg_n != pr_res_holder)
3015 __core_scsi3_complete_pro_release(dev,
3016 pr_res_holder->pr_reg_nacl,
3017 dev->dev_pr_res_holder, 0, 0);
3019 * b) Remove the registrations for all I_T nexuses identified
3020 * by the SERVICE ACTION RESERVATION KEY field, except the
3021 * I_T nexus that is being used for the PERSISTENT RESERVE
3022 * OUT command. If an all registrants persistent reservation
3023 * is present and the SERVICE ACTION RESERVATION KEY field
3024 * is set to zero, then all registrations shall be removed
3025 * except for that of the I_T nexus that is being used for
3026 * the PERSISTENT RESERVE OUT command;
3028 spin_lock(&pr_tmpl->registration_lock);
3029 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3030 &pr_tmpl->registration_list, pr_reg_list) {
3032 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3033 if (calling_it_nexus)
3036 if (pr_reg->pr_res_key != sa_res_key)
3039 pr_reg_nacl = pr_reg->pr_reg_nacl;
3040 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3041 __core_scsi3_free_registration(dev, pr_reg,
3042 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3045 * e) Establish a unit attention condition for the initiator
3046 * port associated with every I_T nexus that lost its
3047 * persistent reservation and/or registration, with the
3048 * additional sense code set to REGISTRATIONS PREEMPTED;
3050 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3051 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3053 spin_unlock(&pr_tmpl->registration_lock);
3055 * c) Establish a persistent reservation for the preempting
3056 * I_T nexus using the contents of the SCOPE and TYPE fields;
3058 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3059 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3060 type, scope, preempt_type);
3062 * d) Process tasks as defined in 5.7.1;
3064 * f) If the type or scope has changed, then for every I_T nexus
3065 * whose reservation key was not removed, except for the I_T
3066 * nexus on which the PERSISTENT RESERVE OUT command was
3067 * received, the device server shall establish a unit
3068 * attention condition for the initiator port associated with
3069 * that I_T nexus, with the additional sense code set to
3070 * RESERVATIONS RELEASED. If the type or scope have not
3071 * changed, then no unit attention condition(s) shall be
3072 * established for this reason.
3074 if ((prh_type != type) || (prh_scope != scope)) {
3075 spin_lock(&pr_tmpl->registration_lock);
3076 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3077 &pr_tmpl->registration_list, pr_reg_list) {
3079 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3080 if (calling_it_nexus)
3083 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3084 pr_reg->pr_res_mapped_lun, 0x2A,
3085 ASCQ_2AH_RESERVATIONS_RELEASED);
3087 spin_unlock(&pr_tmpl->registration_lock);
3089 spin_unlock(&dev->dev_reservation_lock);
3091 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3092 * All received CDBs for the matching existing reservation and
3093 * registrations undergo ABORT_TASK logic.
3095 * From there, core_scsi3_release_preempt_and_abort() will
3096 * release every registration in the list (which have already
3097 * been removed from the primary pr_reg list), except the
3098 * new persistent reservation holder, the calling Initiator Port.
3100 if (preempt_type == PREEMPT_AND_ABORT) {
3101 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3102 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3106 if (pr_tmpl->pr_aptpl_active)
3107 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3109 core_scsi3_put_pr_reg(pr_reg_n);
3110 core_scsi3_pr_generation(cmd->se_dev);
3114 static sense_reason_t
3115 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3116 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3119 case PR_TYPE_WRITE_EXCLUSIVE:
3120 case PR_TYPE_EXCLUSIVE_ACCESS:
3121 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3122 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3123 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3124 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3125 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3126 sa_res_key, preempt_type);
3128 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3129 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3130 return TCM_INVALID_CDB_FIELD;
3135 static sense_reason_t
3136 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3137 u64 sa_res_key, int aptpl, int unreg)
3139 struct se_session *se_sess = cmd->se_sess;
3140 struct se_device *dev = cmd->se_dev;
3141 struct se_dev_entry *dest_se_deve = NULL;
3142 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3143 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3144 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3145 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3146 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3147 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3149 const unsigned char *initiator_str;
3150 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN] = { };
3151 u32 tid_len, tmp_tid_len;
3152 int new_reg = 0, type, scope, matching_iname;
3154 unsigned short rtpi;
3155 unsigned char proto_ident;
3157 if (!se_sess || !se_lun) {
3158 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3159 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3162 se_tpg = se_sess->se_tpg;
3163 tf_ops = se_tpg->se_tpg_tfo;
3165 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3166 * Register behaviors for a REGISTER AND MOVE service action
3168 * Locate the existing *pr_reg via struct se_node_acl pointers
3170 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3173 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3174 " *pr_reg for REGISTER_AND_MOVE\n");
3175 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3178 * The provided reservation key much match the existing reservation key
3179 * provided during this initiator's I_T nexus registration.
3181 if (res_key != pr_reg->pr_res_key) {
3182 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3183 " res_key: 0x%016Lx does not match existing SA REGISTER"
3184 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3185 ret = TCM_RESERVATION_CONFLICT;
3186 goto out_put_pr_reg;
3189 * The service active reservation key needs to be non zero
3192 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3194 ret = TCM_INVALID_PARAMETER_LIST;
3195 goto out_put_pr_reg;
3199 * Determine the Relative Target Port Identifier where the reservation
3200 * will be moved to for the TransportID containing SCSI initiator WWN
3203 buf = transport_kmap_data_sg(cmd);
3205 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3206 goto out_put_pr_reg;
3209 rtpi = get_unaligned_be16(&buf[18]);
3210 tid_len = get_unaligned_be32(&buf[20]);
3211 transport_kunmap_data_sg(cmd);
3214 if ((tid_len + 24) != cmd->data_length) {
3215 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3216 " does not equal CDB data_length: %u\n", tid_len,
3218 ret = TCM_INVALID_PARAMETER_LIST;
3219 goto out_put_pr_reg;
3222 spin_lock(&dev->se_port_lock);
3223 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3224 if (tmp_lun->lun_rtpi != rtpi)
3226 dest_se_tpg = tmp_lun->lun_tpg;
3227 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3231 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3232 spin_unlock(&dev->se_port_lock);
3234 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3235 pr_err("core_scsi3_tpg_depend_item() failed"
3236 " for dest_se_tpg\n");
3237 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3238 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3239 goto out_put_pr_reg;
3242 spin_lock(&dev->se_port_lock);
3245 spin_unlock(&dev->se_port_lock);
3247 if (!dest_se_tpg || !dest_tf_ops) {
3248 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3249 " fabric ops from Relative Target Port Identifier:"
3251 ret = TCM_INVALID_PARAMETER_LIST;
3252 goto out_put_pr_reg;
3255 buf = transport_kmap_data_sg(cmd);
3257 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3258 goto out_put_pr_reg;
3260 proto_ident = (buf[24] & 0x0f);
3262 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3263 " 0x%02x\n", proto_ident);
3265 if (proto_ident != dest_se_tpg->proto_id) {
3266 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3267 " proto_ident: 0x%02x does not match ident: 0x%02x"
3268 " from fabric: %s\n", proto_ident,
3269 dest_se_tpg->proto_id,
3270 dest_tf_ops->fabric_name);
3271 ret = TCM_INVALID_PARAMETER_LIST;
3274 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
3275 &buf[24], &tmp_tid_len, &iport_ptr);
3276 if (!initiator_str) {
3277 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3278 " initiator_str from Transport ID\n");
3279 ret = TCM_INVALID_PARAMETER_LIST;
3283 transport_kunmap_data_sg(cmd);
3286 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3287 " %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
3288 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3291 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3292 * action specifies a TransportID that is the same as the initiator port
3293 * of the I_T nexus for the command received, then the command shall
3294 * be terminated with CHECK CONDITION status, with the sense key set to
3295 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3296 * IN PARAMETER LIST.
3298 pr_reg_nacl = pr_reg->pr_reg_nacl;
3299 matching_iname = (!strcmp(initiator_str,
3300 pr_reg_nacl->initiatorname)) ? 1 : 0;
3301 if (!matching_iname)
3302 goto after_iport_check;
3304 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3305 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3306 " matches: %s on received I_T Nexus\n", initiator_str,
3307 pr_reg_nacl->initiatorname);
3308 ret = TCM_INVALID_PARAMETER_LIST;
3311 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3312 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3313 " matches: %s %s on received I_T Nexus\n",
3314 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3315 pr_reg->pr_reg_isid);
3316 ret = TCM_INVALID_PARAMETER_LIST;
3321 * Locate the destination struct se_node_acl from the received Transport ID
3323 mutex_lock(&dest_se_tpg->acl_node_mutex);
3324 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3327 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3328 mutex_unlock(&dest_se_tpg->acl_node_mutex);
3330 if (!dest_node_acl) {
3331 pr_err("Unable to locate %s dest_node_acl for"
3332 " TransportID%s\n", dest_tf_ops->fabric_name,
3334 ret = TCM_INVALID_PARAMETER_LIST;
3338 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3339 pr_err("core_scsi3_nodeacl_depend_item() for"
3340 " dest_node_acl\n");
3341 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3342 dest_node_acl = NULL;
3343 ret = TCM_INVALID_PARAMETER_LIST;
3347 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3348 " %s from TransportID\n", dest_tf_ops->fabric_name,
3349 dest_node_acl->initiatorname);
3352 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3355 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3356 if (!dest_se_deve) {
3357 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3358 " %hu\n", dest_tf_ops->fabric_name, rtpi);
3359 ret = TCM_INVALID_PARAMETER_LIST;
3363 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3364 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3365 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3366 dest_se_deve = NULL;
3367 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3371 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3372 " ACL for dest_se_deve->mapped_lun: %llu\n",
3373 dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
3374 dest_se_deve->mapped_lun);
3377 * A persistent reservation needs to already existing in order to
3378 * successfully complete the REGISTER_AND_MOVE service action..
3380 spin_lock(&dev->dev_reservation_lock);
3381 pr_res_holder = dev->dev_pr_res_holder;
3382 if (!pr_res_holder) {
3383 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3384 " currently held\n");
3385 spin_unlock(&dev->dev_reservation_lock);
3386 ret = TCM_INVALID_CDB_FIELD;
3390 * The received on I_T Nexus must be the reservation holder.
3392 * From spc4r17 section 5.7.8 Table 50 --
3393 * Register behaviors for a REGISTER AND MOVE service action
3395 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3396 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3397 " Nexus is not reservation holder\n");
3398 spin_unlock(&dev->dev_reservation_lock);
3399 ret = TCM_RESERVATION_CONFLICT;
3403 * From spc4r17 section 5.7.8: registering and moving reservation
3405 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3406 * action is received and the established persistent reservation is a
3407 * Write Exclusive - All Registrants type or Exclusive Access -
3408 * All Registrants type reservation, then the command shall be completed
3409 * with RESERVATION CONFLICT status.
3411 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3412 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3413 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3414 " reservation for type: %s\n",
3415 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3416 spin_unlock(&dev->dev_reservation_lock);
3417 ret = TCM_RESERVATION_CONFLICT;
3420 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3422 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3424 type = pr_res_holder->pr_res_type;
3425 scope = pr_res_holder->pr_res_type;
3427 * c) Associate the reservation key specified in the SERVICE ACTION
3428 * RESERVATION KEY field with the I_T nexus specified as the
3429 * destination of the register and move, where:
3430 * A) The I_T nexus is specified by the TransportID and the
3431 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3432 * B) Regardless of the TransportID format used, the association for
3433 * the initiator port is based on either the initiator port name
3434 * (see 3.1.71) on SCSI transport protocols where port names are
3435 * required or the initiator port identifier (see 3.1.70) on SCSI
3436 * transport protocols where port names are not required;
3437 * d) Register the reservation key specified in the SERVICE ACTION
3438 * RESERVATION KEY field;
3439 * e) Retain the reservation key specified in the SERVICE ACTION
3440 * RESERVATION KEY field and associated information;
3442 * Also, It is not an error for a REGISTER AND MOVE service action to
3443 * register an I_T nexus that is already registered with the same
3444 * reservation key or a different reservation key.
3446 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3449 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
3450 kref_read(&dest_se_deve->pr_kref) != 0);
3452 spin_unlock(&dev->dev_reservation_lock);
3453 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3454 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3455 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
3456 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3459 spin_lock(&dev->dev_reservation_lock);
3460 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3465 * f) Release the persistent reservation for the persistent reservation
3466 * holder (i.e., the I_T nexus on which the
3468 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3469 dev->dev_pr_res_holder, 0, 0);
3471 * g) Move the persistent reservation to the specified I_T nexus using
3472 * the same scope and type as the persistent reservation released in
3475 dev->dev_pr_res_holder = dest_pr_reg;
3476 dest_pr_reg->pr_res_holder = 1;
3477 dest_pr_reg->pr_res_type = type;
3478 pr_reg->pr_res_scope = scope;
3479 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3481 * Increment PRGeneration for existing registrations..
3484 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3485 spin_unlock(&dev->dev_reservation_lock);
3487 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3488 " created new reservation holder TYPE: %s on object RTPI:"
3489 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
3490 core_scsi3_pr_dump_type(type), rtpi,
3491 dest_pr_reg->pr_res_generation);
3492 pr_debug("SPC-3 PR Successfully moved reservation from"
3493 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3494 tf_ops->fabric_name, pr_reg_nacl->initiatorname,
3495 i_buf, dest_tf_ops->fabric_name,
3496 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3499 * It is now safe to release configfs group dependencies for destination
3500 * of Transport ID Initiator Device/Port Identifier
3502 core_scsi3_lunacl_undepend_item(dest_se_deve);
3503 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3504 core_scsi3_tpg_undepend_item(dest_se_tpg);
3506 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3507 * nexus on which PERSISTENT RESERVE OUT command was received.
3510 spin_lock(&pr_tmpl->registration_lock);
3511 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3512 spin_unlock(&pr_tmpl->registration_lock);
3514 core_scsi3_put_pr_reg(pr_reg);
3516 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3518 core_scsi3_put_pr_reg(dest_pr_reg);
3522 transport_kunmap_data_sg(cmd);
3524 core_scsi3_lunacl_undepend_item(dest_se_deve);
3526 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3527 core_scsi3_tpg_undepend_item(dest_se_tpg);
3530 core_scsi3_put_pr_reg(pr_reg);
3535 * See spc4r17 section 6.14 Table 170
3538 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3540 struct se_device *dev = cmd->se_dev;
3541 unsigned char *cdb = &cmd->t_task_cdb[0];
3543 u64 res_key, sa_res_key;
3544 int sa, scope, type, aptpl;
3545 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3549 * Following spc2r20 5.5.1 Reservations overview:
3551 * If a logical unit has been reserved by any RESERVE command and is
3552 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3553 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3554 * initiator or service action and shall terminate with a RESERVATION
3557 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3558 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3559 " SPC-2 reservation is held, returning"
3560 " RESERVATION_CONFLICT\n");
3561 return TCM_RESERVATION_CONFLICT;
3565 * FIXME: A NULL struct se_session pointer means an this is not coming from
3566 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3569 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3571 if (cmd->data_length < 24) {
3572 pr_warn("SPC-PR: Received PR OUT parameter list"
3573 " length too small: %u\n", cmd->data_length);
3574 return TCM_PARAMETER_LIST_LENGTH_ERROR;
3578 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3580 sa = (cdb[1] & 0x1f);
3581 scope = (cdb[2] & 0xf0);
3582 type = (cdb[2] & 0x0f);
3584 buf = transport_kmap_data_sg(cmd);
3586 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3589 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3591 res_key = get_unaligned_be64(&buf[0]);
3592 sa_res_key = get_unaligned_be64(&buf[8]);
3594 * REGISTER_AND_MOVE uses a different SA parameter list containing
3595 * SCSI TransportIDs.
3597 if (sa != PRO_REGISTER_AND_MOVE) {
3598 spec_i_pt = (buf[20] & 0x08);
3599 all_tg_pt = (buf[20] & 0x04);
3600 aptpl = (buf[20] & 0x01);
3602 aptpl = (buf[17] & 0x01);
3603 unreg = (buf[17] & 0x02);
3606 * If the backend device has been configured to force APTPL metadata
3607 * write-out, go ahead and propigate aptpl=1 down now.
3609 if (dev->dev_attrib.force_pr_aptpl)
3612 transport_kunmap_data_sg(cmd);
3616 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3618 if (spec_i_pt && (sa != PRO_REGISTER))
3619 return TCM_INVALID_PARAMETER_LIST;
3622 * From spc4r17 section 6.14:
3624 * If the SPEC_I_PT bit is set to zero, the service action is not
3625 * REGISTER AND MOVE, and the parameter list length is not 24, then
3626 * the command shall be terminated with CHECK CONDITION status, with
3627 * the sense key set to ILLEGAL REQUEST, and the additional sense
3628 * code set to PARAMETER LIST LENGTH ERROR.
3630 if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
3631 (cmd->data_length != 24)) {
3632 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3633 " list length: %u\n", cmd->data_length);
3634 return TCM_PARAMETER_LIST_LENGTH_ERROR;
3638 * (core_scsi3_emulate_pro_* function parameters
3639 * are defined by spc4r17 Table 174:
3640 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3644 ret = core_scsi3_emulate_pro_register(cmd,
3645 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3648 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3651 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3654 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3657 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3658 res_key, sa_res_key, PREEMPT);
3660 case PRO_PREEMPT_AND_ABORT:
3661 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3662 res_key, sa_res_key, PREEMPT_AND_ABORT);
3664 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3665 ret = core_scsi3_emulate_pro_register(cmd,
3666 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3668 case PRO_REGISTER_AND_MOVE:
3669 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3670 sa_res_key, aptpl, unreg);
3673 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3674 " action: 0x%02x\n", sa);
3675 return TCM_INVALID_CDB_FIELD;
3679 target_complete_cmd(cmd, SAM_STAT_GOOD);
3684 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3686 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3688 static sense_reason_t
3689 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3691 struct se_device *dev = cmd->se_dev;
3692 struct t10_pr_registration *pr_reg;
3694 u32 add_len = 0, off = 8;
3696 if (cmd->data_length < 8) {
3697 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3698 " too small\n", cmd->data_length);
3699 return TCM_INVALID_CDB_FIELD;
3702 buf = transport_kmap_data_sg(cmd);
3704 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3706 put_unaligned_be32(dev->t10_pr.pr_generation, buf);
3708 spin_lock(&dev->t10_pr.registration_lock);
3709 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3712 * Check for overflow of 8byte PRI READ_KEYS payload and
3713 * next reservation key list descriptor.
3715 if (off + 8 <= cmd->data_length) {
3716 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3720 * SPC5r17: 6.16.2 READ KEYS service action
3721 * The ADDITIONAL LENGTH field indicates the number of bytes in
3722 * the Reservation key list. The contents of the ADDITIONAL
3723 * LENGTH field are not altered based on the allocation length
3727 spin_unlock(&dev->t10_pr.registration_lock);
3729 put_unaligned_be32(add_len, &buf[4]);
3730 target_set_cmd_data_length(cmd, 8 + add_len);
3732 transport_kunmap_data_sg(cmd);
3738 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3740 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3742 static sense_reason_t
3743 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3745 struct se_device *dev = cmd->se_dev;
3746 struct t10_pr_registration *pr_reg;
3751 if (cmd->data_length < 8) {
3752 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3753 " too small\n", cmd->data_length);
3754 return TCM_INVALID_CDB_FIELD;
3757 buf = transport_kmap_data_sg(cmd);
3759 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3761 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3763 spin_lock(&dev->dev_reservation_lock);
3764 pr_reg = dev->dev_pr_res_holder;
3767 * Set the Additional Length to 16 when a reservation is held
3770 put_unaligned_be32(add_len, &buf[4]);
3772 if (cmd->data_length < 22)
3776 * Set the Reservation key.
3778 * From spc4r17, section 5.7.10:
3779 * A persistent reservation holder has its reservation key
3780 * returned in the parameter data from a PERSISTENT
3781 * RESERVE IN command with READ RESERVATION service action as
3783 * a) For a persistent reservation of the type Write Exclusive
3784 * - All Registrants or Exclusive Access  All Regitrants,
3785 * the reservation key shall be set to zero; or
3786 * b) For all other persistent reservation types, the
3787 * reservation key shall be set to the registered
3788 * reservation key for the I_T nexus that holds the
3789 * persistent reservation.
3791 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3792 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3795 pr_res_key = pr_reg->pr_res_key;
3797 put_unaligned_be64(pr_res_key, &buf[8]);
3799 * Set the SCOPE and TYPE
3801 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3802 (pr_reg->pr_res_type & 0x0f);
3805 target_set_cmd_data_length(cmd, 8 + add_len);
3808 spin_unlock(&dev->dev_reservation_lock);
3809 transport_kunmap_data_sg(cmd);
3815 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3817 * See spc4r17 section 6.13.4 Table 165
3819 static sense_reason_t
3820 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3822 struct se_device *dev = cmd->se_dev;
3823 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3825 u16 len = 8; /* Hardcoded to 8. */
3827 if (cmd->data_length < 6) {
3828 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3829 " %u too small\n", cmd->data_length);
3830 return TCM_INVALID_CDB_FIELD;
3833 buf = transport_kmap_data_sg(cmd);
3835 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3837 put_unaligned_be16(len, &buf[0]);
3838 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3839 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3840 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3841 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3843 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3844 * set the TMV: Task Mask Valid bit.
3848 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3850 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3852 * PTPL_A: Persistence across Target Power Loss Active bit
3854 if (pr_tmpl->pr_aptpl_active)
3857 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3859 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3860 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3861 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3862 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3863 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3864 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3866 target_set_cmd_data_length(cmd, len);
3868 transport_kunmap_data_sg(cmd);
3874 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3876 * See spc4r17 section 6.13.5 Table 168 and 169
3878 static sense_reason_t
3879 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3881 struct se_device *dev = cmd->se_dev;
3882 struct se_node_acl *se_nacl;
3883 struct se_portal_group *se_tpg;
3884 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3885 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3887 u32 add_desc_len = 0, add_len = 0;
3888 u32 off = 8; /* off into first Full Status descriptor */
3889 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3890 int exp_desc_len, desc_len;
3891 bool all_reg = false;
3893 if (cmd->data_length < 8) {
3894 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3895 " too small\n", cmd->data_length);
3896 return TCM_INVALID_CDB_FIELD;
3899 buf = transport_kmap_data_sg(cmd);
3901 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3903 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3905 spin_lock(&dev->dev_reservation_lock);
3906 if (dev->dev_pr_res_holder) {
3907 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3909 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3910 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3912 pr_res_type = pr_holder->pr_res_type;
3913 pr_res_scope = pr_holder->pr_res_scope;
3916 spin_unlock(&dev->dev_reservation_lock);
3918 spin_lock(&pr_tmpl->registration_lock);
3919 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3920 &pr_tmpl->registration_list, pr_reg_list) {
3922 se_nacl = pr_reg->pr_reg_nacl;
3923 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3926 atomic_inc_mb(&pr_reg->pr_res_holders);
3927 spin_unlock(&pr_tmpl->registration_lock);
3929 * Determine expected length of $FABRIC_MOD specific
3930 * TransportID full status descriptor..
3932 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3934 if (exp_desc_len < 0 ||
3935 exp_desc_len + add_len > cmd->data_length) {
3936 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3937 " out of buffer: %d\n", cmd->data_length);
3938 spin_lock(&pr_tmpl->registration_lock);
3939 atomic_dec_mb(&pr_reg->pr_res_holders);
3943 * Set RESERVATION KEY
3945 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3947 off += 4; /* Skip Over Reserved area */
3950 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3952 if (pr_reg->pr_reg_all_tg_pt)
3955 * The struct se_lun pointer will be present for the
3956 * reservation holder for PR_HOLDER bit.
3958 * Also, if this registration is the reservation
3959 * holder or there is an All Registrants reservation
3960 * active, fill in SCOPE and TYPE in the next byte.
3962 if (pr_reg->pr_res_holder) {
3964 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3965 (pr_reg->pr_res_type & 0x0f);
3966 } else if (all_reg) {
3968 buf[off++] = (pr_res_scope & 0xf0) |
3969 (pr_res_type & 0x0f);
3974 off += 4; /* Skip over reserved area */
3976 * From spc4r17 6.3.15:
3978 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3979 * IDENTIFIER field contains the relative port identifier (see
3980 * 3.1.120) of the target port that is part of the I_T nexus
3981 * described by this full status descriptor. If the ALL_TG_PT
3982 * bit is set to one, the contents of the RELATIVE TARGET PORT
3983 * IDENTIFIER field are not defined by this standard.
3985 if (!pr_reg->pr_reg_all_tg_pt) {
3986 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
3988 put_unaligned_be16(sep_rtpi, &buf[off]);
3991 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
3993 buf[off+4] = se_tpg->proto_id;
3996 * Now, have the $FABRIC_MOD fill in the transport ID.
3998 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
3999 &format_code, &buf[off+4]);
4001 spin_lock(&pr_tmpl->registration_lock);
4002 atomic_dec_mb(&pr_reg->pr_res_holders);
4007 * Set the ADDITIONAL DESCRIPTOR LENGTH
4009 put_unaligned_be32(desc_len, &buf[off]);
4012 * Size of full desctipor header minus TransportID
4013 * containing $FABRIC_MOD specific) initiator device/port
4016 * See spc4r17 Section 6.13.5 Table 169
4018 add_desc_len = (24 + desc_len);
4021 add_len += add_desc_len;
4023 spin_unlock(&pr_tmpl->registration_lock);
4025 * Set ADDITIONAL_LENGTH
4027 put_unaligned_be32(add_len, &buf[4]);
4028 target_set_cmd_data_length(cmd, 8 + add_len);
4030 transport_kunmap_data_sg(cmd);
4036 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4041 * Following spc2r20 5.5.1 Reservations overview:
4043 * If a logical unit has been reserved by any RESERVE command and is
4044 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4045 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4046 * initiator or service action and shall terminate with a RESERVATION
4049 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4050 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4051 " SPC-2 reservation is held, returning"
4052 " RESERVATION_CONFLICT\n");
4053 return TCM_RESERVATION_CONFLICT;
4056 switch (cmd->t_task_cdb[1] & 0x1f) {
4058 ret = core_scsi3_pri_read_keys(cmd);
4060 case PRI_READ_RESERVATION:
4061 ret = core_scsi3_pri_read_reservation(cmd);
4063 case PRI_REPORT_CAPABILITIES:
4064 ret = core_scsi3_pri_report_capabilities(cmd);
4066 case PRI_READ_FULL_STATUS:
4067 ret = core_scsi3_pri_read_full_status(cmd);
4070 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4071 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4072 return TCM_INVALID_CDB_FIELD;
4076 target_complete_cmd(cmd, SAM_STAT_GOOD);
4081 target_check_reservation(struct se_cmd *cmd)
4083 struct se_device *dev = cmd->se_dev;
4088 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4090 if (!dev->dev_attrib.emulate_pr)
4092 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
4095 spin_lock(&dev->dev_reservation_lock);
4096 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4097 ret = target_scsi2_reservation_check(cmd);
4099 ret = target_scsi3_pr_reservation_check(cmd);
4100 spin_unlock(&dev->dev_reservation_lock);