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.
10 * Nicholas A. Bellinger <nab@kernel.org>
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_tpg->tpg_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 = deve_tmp->se_lun_acl;
743 nacl_tmp = lacl_tmp->se_lun_nacl;
745 * Skip the matching struct se_node_acl that is allocated
748 if (nacl == nacl_tmp)
751 * Only perform PR registrations for target ports on
752 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
755 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
758 * Look for a matching Initiator Node ACL in ASCII format
760 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
763 kref_get(&deve_tmp->pr_kref);
764 spin_unlock(&lun_tmp->lun_deve_lock);
766 * Grab a configfs group dependency that is released
767 * for the exception path at label out: below, or upon
768 * completion of adding ALL_TG_PT=1 registrations in
769 * __core_scsi3_add_registration()
771 ret = core_scsi3_lunacl_depend_item(deve_tmp);
773 pr_err("core_scsi3_lunacl_depend"
775 percpu_ref_put(&lun_tmp->lun_ref);
776 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
780 * Located a matching SCSI Initiator Port on a different
781 * port, allocate the pr_reg_atp and attach it to the
782 * pr_reg->pr_reg_atp_list that will be processed once
783 * the original *pr_reg is processed in
784 * __core_scsi3_add_registration()
786 dest_lun = deve_tmp->se_lun;
788 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
789 nacl_tmp, dest_lun, deve_tmp,
790 deve_tmp->mapped_lun, NULL,
791 sa_res_key, all_tg_pt, aptpl);
793 percpu_ref_put(&lun_tmp->lun_ref);
794 core_scsi3_lunacl_undepend_item(deve_tmp);
798 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
799 &pr_reg->pr_reg_atp_list);
800 spin_lock(&lun_tmp->lun_deve_lock);
802 spin_unlock(&lun_tmp->lun_deve_lock);
804 spin_lock(&dev->se_port_lock);
805 percpu_ref_put(&lun_tmp->lun_ref);
807 spin_unlock(&dev->se_port_lock);
811 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
812 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
813 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
814 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
815 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
817 kmem_cache_free(t10_pr_reg_cache, pr_reg);
821 int core_scsi3_alloc_aptpl_registration(
822 struct t10_reservation *pr_tmpl,
824 unsigned char *i_port,
827 unsigned char *t_port,
834 struct t10_pr_registration *pr_reg;
836 if (!i_port || !t_port || !sa_res_key) {
837 pr_err("Illegal parameters for APTPL registration\n");
841 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
843 pr_err("Unable to allocate struct t10_pr_registration\n");
847 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
848 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
849 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
850 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
851 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
852 atomic_set(&pr_reg->pr_res_holders, 0);
853 pr_reg->pr_reg_nacl = NULL;
854 pr_reg->pr_reg_deve = NULL;
855 pr_reg->pr_res_mapped_lun = mapped_lun;
856 pr_reg->pr_aptpl_target_lun = target_lun;
857 pr_reg->pr_res_key = sa_res_key;
858 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
859 pr_reg->pr_reg_aptpl = 1;
860 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
861 pr_reg->pr_res_type = type;
863 * If an ISID value had been saved in APTPL metadata for this
864 * SCSI Initiator Port, restore it now.
867 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
868 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
869 pr_reg->isid_present_at_reg = 1;
872 * Copy the i_port and t_port information from caller.
874 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
875 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
876 pr_reg->pr_reg_tpgt = tpgt;
878 * Set pr_res_holder from caller, the pr_reg who is the reservation
879 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
880 * the Initiator Node LUN ACL from the fabric module is created for
883 pr_reg->pr_res_holder = res_holder;
885 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
886 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
887 " metadata\n", (res_holder) ? "+reservation" : "");
891 static void core_scsi3_aptpl_reserve(
892 struct se_device *dev,
893 struct se_portal_group *tpg,
894 struct se_node_acl *node_acl,
895 struct t10_pr_registration *pr_reg)
897 char i_buf[PR_REG_ISID_ID_LEN] = { };
899 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
901 spin_lock(&dev->dev_reservation_lock);
902 dev->dev_pr_res_holder = pr_reg;
903 spin_unlock(&dev->dev_reservation_lock);
905 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
906 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
907 tpg->se_tpg_tfo->fabric_name,
908 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
909 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
910 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
911 tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
915 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
916 struct t10_pr_registration *, enum register_type, int);
918 static int __core_scsi3_check_aptpl_registration(
919 struct se_device *dev,
920 struct se_portal_group *tpg,
923 struct se_node_acl *nacl,
926 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
927 struct t10_reservation *pr_tmpl = &dev->t10_pr;
928 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN] = { };
929 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN] = { };
933 * Copy Initiator Port information from struct se_node_acl
935 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
936 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
937 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
938 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
940 * Look for the matching registrations+reservation from those
941 * created from APTPL metadata. Note that multiple registrations
942 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
945 spin_lock(&pr_tmpl->aptpl_reg_lock);
946 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
949 if (!strcmp(pr_reg->pr_iport, i_port) &&
950 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
951 !(strcmp(pr_reg->pr_tport, t_port)) &&
952 (pr_reg->pr_reg_tpgt == tpgt) &&
953 (pr_reg->pr_aptpl_target_lun == target_lun)) {
955 * Obtain the ->pr_reg_deve pointer + reference, that
956 * is released by __core_scsi3_add_registration() below.
959 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
960 if (!pr_reg->pr_reg_deve) {
961 pr_err("Unable to locate PR APTPL %s mapped_lun:"
962 " %llu\n", nacl->initiatorname, mapped_lun);
966 kref_get(&pr_reg->pr_reg_deve->pr_kref);
969 pr_reg->pr_reg_nacl = nacl;
970 pr_reg->tg_pt_sep_rtpi = lun->lun_tpg->tpg_rtpi;
971 list_del(&pr_reg->pr_reg_aptpl_list);
972 spin_unlock(&pr_tmpl->aptpl_reg_lock);
974 * At this point all of the pointers in *pr_reg will
975 * be setup, so go ahead and add the registration.
977 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
979 * If this registration is the reservation holder,
980 * make that happen now..
982 if (pr_reg->pr_res_holder)
983 core_scsi3_aptpl_reserve(dev, tpg,
986 * Reenable pr_aptpl_active to accept new metadata
987 * updates once the SCSI device is active again..
989 spin_lock(&pr_tmpl->aptpl_reg_lock);
990 pr_tmpl->pr_aptpl_active = 1;
993 spin_unlock(&pr_tmpl->aptpl_reg_lock);
998 int core_scsi3_check_aptpl_registration(
999 struct se_device *dev,
1000 struct se_portal_group *tpg,
1002 struct se_node_acl *nacl,
1005 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1008 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1009 lun->unpacked_lun, nacl,
1013 static void __core_scsi3_dump_registration(
1014 const struct target_core_fabric_ops *tfo,
1015 struct se_device *dev,
1016 struct se_node_acl *nacl,
1017 struct t10_pr_registration *pr_reg,
1018 enum register_type register_type)
1020 struct se_portal_group *se_tpg = nacl->se_tpg;
1021 char i_buf[PR_REG_ISID_ID_LEN] = { };
1023 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1025 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1026 " Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
1027 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1028 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1030 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1031 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
1032 tfo->tpg_get_tag(se_tpg));
1033 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1034 " Port(s)\n", tfo->fabric_name,
1035 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1036 dev->transport->name);
1037 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1038 " 0x%08x APTPL: %d\n", tfo->fabric_name,
1039 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1040 pr_reg->pr_reg_aptpl);
1043 static void __core_scsi3_add_registration(
1044 struct se_device *dev,
1045 struct se_node_acl *nacl,
1046 struct t10_pr_registration *pr_reg,
1047 enum register_type register_type,
1050 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1051 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1052 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1053 struct se_dev_entry *deve;
1056 * Increment PRgeneration counter for struct se_device upon a successful
1057 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1059 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1060 * action, the struct se_device->dev_reservation_lock will already be held,
1061 * so we do not call core_scsi3_pr_generation() which grabs the lock
1064 pr_reg->pr_res_generation = (register_move) ?
1065 dev->t10_pr.pr_generation++ :
1066 core_scsi3_pr_generation(dev);
1068 spin_lock(&pr_tmpl->registration_lock);
1069 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1071 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1072 spin_unlock(&pr_tmpl->registration_lock);
1074 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1076 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1079 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1080 * allocated in __core_scsi3_alloc_registration()
1082 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1083 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1084 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1086 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1088 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1090 spin_lock(&pr_tmpl->registration_lock);
1091 list_add_tail(&pr_reg_tmp->pr_reg_list,
1092 &pr_tmpl->registration_list);
1094 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1096 spin_unlock(&pr_tmpl->registration_lock);
1098 * Drop configfs group dependency reference and deve->pr_kref
1099 * obtained from __core_scsi3_alloc_registration() code.
1102 deve = pr_reg_tmp->pr_reg_deve;
1104 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1105 core_scsi3_lunacl_undepend_item(deve);
1106 pr_reg_tmp->pr_reg_deve = NULL;
1112 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1115 deve = pr_reg->pr_reg_deve;
1117 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1118 kref_put(&deve->pr_kref, target_pr_kref_release);
1119 pr_reg->pr_reg_deve = NULL;
1124 static int core_scsi3_alloc_registration(
1125 struct se_device *dev,
1126 struct se_node_acl *nacl,
1128 struct se_dev_entry *deve,
1130 unsigned char *isid,
1134 enum register_type register_type,
1137 struct t10_pr_registration *pr_reg;
1139 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1140 isid, sa_res_key, all_tg_pt,
1145 __core_scsi3_add_registration(dev, nacl, pr_reg,
1146 register_type, register_move);
1150 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1151 struct se_device *dev,
1152 struct se_node_acl *nacl,
1153 unsigned char *isid)
1155 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1156 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1158 spin_lock(&pr_tmpl->registration_lock);
1159 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1160 &pr_tmpl->registration_list, pr_reg_list) {
1162 * First look for a matching struct se_node_acl
1164 if (pr_reg->pr_reg_nacl != nacl)
1168 * If this registration does NOT contain a fabric provided
1169 * ISID, then we have found a match.
1171 if (!pr_reg->isid_present_at_reg) {
1172 atomic_inc_mb(&pr_reg->pr_res_holders);
1173 spin_unlock(&pr_tmpl->registration_lock);
1177 * If the *pr_reg contains a fabric defined ISID for multi-value
1178 * SCSI Initiator Port TransportIDs, then we expect a valid
1179 * matching ISID to be provided by the local SCSI Initiator Port.
1183 if (strcmp(isid, pr_reg->pr_reg_isid))
1186 atomic_inc_mb(&pr_reg->pr_res_holders);
1187 spin_unlock(&pr_tmpl->registration_lock);
1190 spin_unlock(&pr_tmpl->registration_lock);
1195 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1196 struct se_device *dev,
1197 struct se_node_acl *nacl,
1198 struct se_session *sess)
1200 struct se_portal_group *tpg = nacl->se_tpg;
1201 unsigned char buf[PR_REG_ISID_LEN] = { };
1202 unsigned char *isid_ptr = NULL;
1204 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1205 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1210 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1213 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1215 atomic_dec_mb(&pr_reg->pr_res_holders);
1218 static int core_scsi3_check_implicit_release(
1219 struct se_device *dev,
1220 struct t10_pr_registration *pr_reg)
1222 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1223 struct t10_pr_registration *pr_res_holder;
1226 spin_lock(&dev->dev_reservation_lock);
1227 pr_res_holder = dev->dev_pr_res_holder;
1228 if (!pr_res_holder) {
1229 spin_unlock(&dev->dev_reservation_lock);
1232 if (pr_res_holder == pr_reg) {
1234 * Perform an implicit RELEASE if the registration that
1235 * is being released is holding the reservation.
1237 * From spc4r17, section 5.7.11.1:
1239 * e) If the I_T nexus is the persistent reservation holder
1240 * and the persistent reservation is not an all registrants
1241 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1242 * service action or REGISTER AND IGNORE EXISTING KEY
1243 * service action with the SERVICE ACTION RESERVATION KEY
1244 * field set to zero (see 5.7.11.3).
1246 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1249 * For 'All Registrants' reservation types, all existing
1250 * registrations are still processed as reservation holders
1251 * in core_scsi3_pr_seq_non_holder() after the initial
1252 * reservation holder is implicitly released here.
1254 } else if (pr_reg->pr_reg_all_tg_pt &&
1255 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1256 pr_reg->pr_reg_nacl->initiatorname)) &&
1257 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1258 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1259 " UNREGISTER while existing reservation with matching"
1260 " key 0x%016Lx is present from another SCSI Initiator"
1261 " Port\n", pr_reg->pr_res_key);
1264 spin_unlock(&dev->dev_reservation_lock);
1269 static void __core_scsi3_free_registration(
1270 struct se_device *dev,
1271 struct t10_pr_registration *pr_reg,
1272 struct list_head *preempt_and_abort_list,
1274 __releases(&pr_tmpl->registration_lock)
1275 __acquires(&pr_tmpl->registration_lock)
1277 const struct target_core_fabric_ops *tfo =
1278 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1279 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1280 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1281 struct se_dev_entry *deve;
1282 char i_buf[PR_REG_ISID_ID_LEN] = { };
1284 lockdep_assert_held(&pr_tmpl->registration_lock);
1286 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1288 if (!list_empty(&pr_reg->pr_reg_list))
1289 list_del(&pr_reg->pr_reg_list);
1291 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1292 * so call core_scsi3_put_pr_reg() to decrement our reference.
1295 core_scsi3_put_pr_reg(pr_reg);
1297 spin_unlock(&pr_tmpl->registration_lock);
1299 * Wait until all reference from any other I_T nexuses for this
1300 * *pr_reg have been released. Because list_del() is called above,
1301 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1302 * count back to zero, and we release *pr_reg.
1304 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1305 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1311 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1313 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1316 spin_lock(&pr_tmpl->registration_lock);
1317 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1318 " Node: %s%s\n", tfo->fabric_name,
1319 pr_reg->pr_reg_nacl->initiatorname,
1321 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1322 " Port(s)\n", tfo->fabric_name,
1323 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1324 dev->transport->name);
1325 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1326 " 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
1327 pr_reg->pr_res_generation);
1329 if (!preempt_and_abort_list) {
1330 pr_reg->pr_reg_deve = NULL;
1331 pr_reg->pr_reg_nacl = NULL;
1332 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1336 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1337 * are released once the ABORT_TASK_SET has completed..
1339 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1342 void core_scsi3_free_pr_reg_from_nacl(
1343 struct se_device *dev,
1344 struct se_node_acl *nacl)
1346 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1347 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1348 bool free_reg = false;
1350 * If the passed se_node_acl matches the reservation holder,
1351 * release the reservation.
1353 spin_lock(&dev->dev_reservation_lock);
1354 pr_res_holder = dev->dev_pr_res_holder;
1355 if ((pr_res_holder != NULL) &&
1356 (pr_res_holder->pr_reg_nacl == nacl)) {
1357 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1360 spin_unlock(&dev->dev_reservation_lock);
1362 * Release any registration associated with the struct se_node_acl.
1364 spin_lock(&pr_tmpl->registration_lock);
1365 if (pr_res_holder && free_reg)
1366 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1368 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1369 &pr_tmpl->registration_list, pr_reg_list) {
1371 if (pr_reg->pr_reg_nacl != nacl)
1374 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1376 spin_unlock(&pr_tmpl->registration_lock);
1379 void core_scsi3_free_all_registrations(
1380 struct se_device *dev)
1382 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1383 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1385 spin_lock(&dev->dev_reservation_lock);
1386 pr_res_holder = dev->dev_pr_res_holder;
1387 if (pr_res_holder != NULL) {
1388 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1389 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1390 pr_res_holder, 0, 0);
1392 spin_unlock(&dev->dev_reservation_lock);
1394 spin_lock(&pr_tmpl->registration_lock);
1395 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1396 &pr_tmpl->registration_list, pr_reg_list) {
1398 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1400 spin_unlock(&pr_tmpl->registration_lock);
1402 spin_lock(&pr_tmpl->aptpl_reg_lock);
1403 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1404 pr_reg_aptpl_list) {
1405 list_del(&pr_reg->pr_reg_aptpl_list);
1406 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1408 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1411 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1413 return target_depend_item(&tpg->tpg_group.cg_item);
1416 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1418 target_undepend_item(&tpg->tpg_group.cg_item);
1419 atomic_dec_mb(&tpg->tpg_pr_ref_count);
1422 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1424 if (nacl->dynamic_node_acl)
1426 return target_depend_item(&nacl->acl_group.cg_item);
1429 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1431 if (!nacl->dynamic_node_acl)
1432 target_undepend_item(&nacl->acl_group.cg_item);
1433 atomic_dec_mb(&nacl->acl_pr_ref_count);
1436 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1439 * For nacl->dynamic_node_acl=1
1441 if (!se_deve->se_lun_acl)
1444 return target_depend_item(&se_deve->se_lun_acl->se_lun_group.cg_item);
1447 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1450 * For nacl->dynamic_node_acl=1
1452 if (!se_deve->se_lun_acl) {
1453 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1457 target_undepend_item(&se_deve->se_lun_acl->se_lun_group.cg_item);
1458 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1461 static sense_reason_t
1462 core_scsi3_decode_spec_i_port(
1464 struct se_portal_group *tpg,
1465 unsigned char *l_isid,
1470 struct se_device *dev = cmd->se_dev;
1471 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1472 struct se_session *se_sess = cmd->se_sess;
1473 struct se_node_acl *dest_node_acl = NULL;
1474 struct se_dev_entry *dest_se_deve = NULL;
1475 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1476 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1477 LIST_HEAD(tid_dest_list);
1478 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1479 unsigned char *buf, *ptr, proto_ident;
1480 const unsigned char *i_str = NULL;
1481 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1483 u32 tpdl, tid_len = 0;
1487 * Allocate a struct pr_transport_id_holder and setup the
1488 * local_node_acl pointer and add to struct list_head tid_dest_list
1489 * for add registration processing in the loop of tid_dest_list below.
1491 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1493 pr_err("Unable to allocate tidh_new\n");
1494 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1496 INIT_LIST_HEAD(&tidh_new->dest_list);
1497 tidh_new->dest_tpg = tpg;
1498 tidh_new->dest_node_acl = se_sess->se_node_acl;
1500 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1501 se_sess->se_node_acl, cmd->se_lun,
1502 NULL, cmd->orig_fe_lun, l_isid,
1503 sa_res_key, all_tg_pt, aptpl);
1504 if (!local_pr_reg) {
1506 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1509 if (core_scsi3_lunacl_depend_item(local_pr_reg->pr_reg_deve)) {
1511 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1512 target_pr_kref_release);
1513 kmem_cache_free(t10_pr_reg_cache, local_pr_reg);
1514 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1517 tidh_new->dest_pr_reg = local_pr_reg;
1518 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1520 if (cmd->data_length < 28) {
1521 pr_warn("SPC-PR: Received PR OUT parameter list"
1522 " length too small: %u\n", cmd->data_length);
1523 ret = TCM_INVALID_PARAMETER_LIST;
1527 buf = transport_kmap_data_sg(cmd);
1529 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1534 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1535 * first extract TransportID Parameter Data Length, and make sure
1536 * the value matches up to the SCSI expected data transfer length.
1538 tpdl = get_unaligned_be32(&buf[24]);
1540 if ((tpdl + 28) != cmd->data_length) {
1541 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1542 " does not equal CDB data_length: %u\n", tpdl,
1544 ret = TCM_INVALID_PARAMETER_LIST;
1548 * Start processing the received transport IDs using the
1549 * receiving I_T Nexus portal's fabric dependent methods to
1550 * obtain the SCSI Initiator Port/Device Identifiers.
1555 struct se_lun *dest_lun, *tmp_lun;
1557 proto_ident = (ptr[0] & 0x0f);
1560 spin_lock(&dev->se_port_lock);
1561 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1562 tmp_tpg = tmp_lun->lun_tpg;
1565 * Look for the matching proto_ident provided by
1566 * the received TransportID
1568 if (tmp_tpg->proto_id != proto_ident)
1570 dest_rtpi = tmp_lun->lun_tpg->tpg_rtpi;
1573 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1574 ptr, &tid_len, &iport_ptr);
1578 * Determine if this SCSI device server requires that
1579 * SCSI Intiatior TransportID w/ ISIDs is enforced
1580 * for fabric modules (iSCSI) requiring them.
1582 if (tpg->se_tpg_tfo->sess_get_initiator_sid &&
1583 dev->dev_attrib.enforce_pr_isids &&
1585 pr_warn("SPC-PR: enforce_pr_isids is set but a isid has not been sent in the SPEC_I_PT data for %s.",
1587 ret = TCM_INVALID_PARAMETER_LIST;
1588 spin_unlock(&dev->se_port_lock);
1592 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1593 spin_unlock(&dev->se_port_lock);
1595 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1596 pr_err(" core_scsi3_tpg_depend_item()"
1598 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1599 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1603 * Locate the destination initiator ACL to be registered
1604 * from the decoded fabric module specific TransportID
1607 mutex_lock(&tmp_tpg->acl_node_mutex);
1608 dest_node_acl = __core_tpg_get_initiator_node_acl(
1611 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1612 mutex_unlock(&tmp_tpg->acl_node_mutex);
1614 if (!dest_node_acl) {
1615 core_scsi3_tpg_undepend_item(tmp_tpg);
1616 spin_lock(&dev->se_port_lock);
1620 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1621 pr_err("configfs_depend_item() failed"
1622 " for dest_node_acl->acl_group\n");
1623 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1624 core_scsi3_tpg_undepend_item(tmp_tpg);
1625 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1630 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s Port RTPI: %u\n",
1631 dest_tpg->se_tpg_tfo->fabric_name,
1632 dest_node_acl->initiatorname, dest_rtpi);
1634 spin_lock(&dev->se_port_lock);
1637 spin_unlock(&dev->se_port_lock);
1640 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1642 ret = TCM_INVALID_PARAMETER_LIST;
1646 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1647 " tid_len: %d for %s + %s\n",
1648 dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
1649 tpdl, tid_len, i_str, iport_ptr);
1651 if (tid_len > tpdl) {
1652 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1653 " %u for Transport ID: %s\n", tid_len, ptr);
1654 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1655 core_scsi3_tpg_undepend_item(dest_tpg);
1656 ret = TCM_INVALID_PARAMETER_LIST;
1660 * Locate the desintation struct se_dev_entry pointer for matching
1661 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1664 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1666 if (!dest_se_deve) {
1667 pr_err("Unable to locate %s dest_se_deve from destination RTPI: %u\n",
1668 dest_tpg->se_tpg_tfo->fabric_name,
1671 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1672 core_scsi3_tpg_undepend_item(dest_tpg);
1673 ret = TCM_INVALID_PARAMETER_LIST;
1677 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1678 pr_err("core_scsi3_lunacl_depend_item()"
1680 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1681 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1682 core_scsi3_tpg_undepend_item(dest_tpg);
1683 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1687 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1688 " dest_se_deve mapped_lun: %llu\n",
1689 dest_tpg->se_tpg_tfo->fabric_name,
1690 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1693 * Skip any TransportIDs that already have a registration for
1696 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1699 core_scsi3_put_pr_reg(pr_reg_e);
1700 core_scsi3_lunacl_undepend_item(dest_se_deve);
1701 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1702 core_scsi3_tpg_undepend_item(dest_tpg);
1709 * Allocate a struct pr_transport_id_holder and setup
1710 * the dest_node_acl and dest_se_deve pointers for the
1713 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1716 pr_err("Unable to allocate tidh_new\n");
1717 core_scsi3_lunacl_undepend_item(dest_se_deve);
1718 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1719 core_scsi3_tpg_undepend_item(dest_tpg);
1720 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1723 INIT_LIST_HEAD(&tidh_new->dest_list);
1724 tidh_new->dest_tpg = dest_tpg;
1725 tidh_new->dest_node_acl = dest_node_acl;
1726 tidh_new->dest_se_deve = dest_se_deve;
1729 * Allocate, but do NOT add the registration for the
1730 * TransportID referenced SCSI Initiator port. This
1731 * done because of the following from spc4r17 in section
1732 * 6.14.3 wrt SPEC_I_PT:
1734 * "If a registration fails for any initiator port (e.g., if th
1735 * logical unit does not have enough resources available to
1736 * hold the registration information), no registrations shall be
1737 * made, and the command shall be terminated with
1738 * CHECK CONDITION status."
1740 * That means we call __core_scsi3_alloc_registration() here,
1741 * and then call __core_scsi3_add_registration() in the
1742 * 2nd loop which will never fail.
1744 dest_lun = dest_se_deve->se_lun;
1746 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1747 dest_node_acl, dest_lun, dest_se_deve,
1748 dest_se_deve->mapped_lun, iport_ptr,
1749 sa_res_key, all_tg_pt, aptpl);
1751 core_scsi3_lunacl_undepend_item(dest_se_deve);
1752 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1753 core_scsi3_tpg_undepend_item(dest_tpg);
1755 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1758 tidh_new->dest_pr_reg = dest_pr_reg;
1759 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1767 transport_kunmap_data_sg(cmd);
1770 * Go ahead and create a registrations from tid_dest_list for the
1771 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1774 * The SA Reservation Key from the PROUT is set for the
1775 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1776 * means that the TransportID Initiator port will be
1777 * registered on all of the target ports in the SCSI target device
1778 * ALL_TG_PT=0 means the registration will only be for the
1779 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1782 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1783 dest_tpg = tidh->dest_tpg;
1784 dest_node_acl = tidh->dest_node_acl;
1785 dest_se_deve = tidh->dest_se_deve;
1786 dest_pr_reg = tidh->dest_pr_reg;
1788 list_del(&tidh->dest_list);
1791 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1792 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1794 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1797 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1798 " registered Transport ID for Node: %s%s Mapped LUN:"
1799 " %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
1800 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1801 dest_se_deve->mapped_lun : 0);
1803 if (dest_pr_reg == local_pr_reg)
1806 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1807 core_scsi3_tpg_undepend_item(dest_tpg);
1812 transport_kunmap_data_sg(cmd);
1815 * For the failure case, release everything from tid_dest_list
1816 * including *dest_pr_reg and the configfs dependances..
1818 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1819 bool is_local = false;
1821 dest_tpg = tidh->dest_tpg;
1822 dest_node_acl = tidh->dest_node_acl;
1823 dest_se_deve = tidh->dest_se_deve;
1824 dest_pr_reg = tidh->dest_pr_reg;
1826 if (dest_pr_reg == local_pr_reg)
1829 list_del(&tidh->dest_list);
1832 * Release any extra ALL_TG_PT=1 registrations for
1833 * the SPEC_I_PT=1 case.
1835 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1836 &dest_pr_reg->pr_reg_atp_list,
1837 pr_reg_atp_mem_list) {
1838 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1839 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1840 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1843 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1844 core_scsi3_lunacl_undepend_item(dest_se_deve);
1849 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1850 core_scsi3_tpg_undepend_item(dest_tpg);
1855 static int core_scsi3_update_aptpl_buf(
1856 struct se_device *dev,
1858 u32 pr_aptpl_buf_len)
1860 struct se_portal_group *tpg;
1861 struct t10_pr_registration *pr_reg;
1862 unsigned char tmp[512], isid_buf[32];
1867 spin_lock(&dev->dev_reservation_lock);
1868 spin_lock(&dev->t10_pr.registration_lock);
1870 * Walk the registration list..
1872 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1877 tpg = pr_reg->pr_reg_nacl->se_tpg;
1879 * Write out any ISID value to APTPL metadata that was included
1880 * in the original registration.
1882 if (pr_reg->isid_present_at_reg)
1883 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1884 pr_reg->pr_reg_isid);
1886 * Include special metadata if the pr_reg matches the
1887 * reservation holder.
1889 if (dev->dev_pr_res_holder == pr_reg) {
1890 snprintf(tmp, 512, "PR_REG_START: %d"
1891 "\ninitiator_fabric=%s\n"
1892 "initiator_node=%s\n%s"
1894 "res_holder=1\nres_type=%02x\n"
1895 "res_scope=%02x\nres_all_tg_pt=%d\n"
1896 "mapped_lun=%llu\n", reg_count,
1897 tpg->se_tpg_tfo->fabric_name,
1898 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1899 pr_reg->pr_res_key, pr_reg->pr_res_type,
1900 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1901 pr_reg->pr_res_mapped_lun);
1903 snprintf(tmp, 512, "PR_REG_START: %d\n"
1904 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1905 "sa_res_key=%llu\nres_holder=0\n"
1906 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
1907 reg_count, tpg->se_tpg_tfo->fabric_name,
1908 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1909 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1910 pr_reg->pr_res_mapped_lun);
1913 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1914 pr_err("Unable to update renaming APTPL metadata,"
1915 " reallocating larger buffer\n");
1919 len += sprintf(buf+len, "%s", tmp);
1922 * Include information about the associated SCSI target port.
1924 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1925 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
1926 " %d\n", tpg->se_tpg_tfo->fabric_name,
1927 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1928 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1929 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1932 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1933 pr_err("Unable to update renaming APTPL metadata,"
1934 " reallocating larger buffer\n");
1938 len += sprintf(buf+len, "%s", tmp);
1943 len += sprintf(buf+len, "No Registrations or Reservations");
1946 spin_unlock(&dev->t10_pr.registration_lock);
1947 spin_unlock(&dev->dev_reservation_lock);
1952 static int __core_scsi3_write_aptpl_to_file(
1953 struct se_device *dev,
1956 struct t10_wwn *wwn = &dev->t10_wwn;
1958 int flags = O_RDWR | O_CREAT | O_TRUNC;
1960 u32 pr_aptpl_buf_len;
1964 path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
1965 &wwn->unit_serial[0]);
1969 file = filp_open(path, flags, 0600);
1971 pr_err("filp_open(%s) for APTPL metadata"
1974 return PTR_ERR(file);
1977 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
1979 ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
1982 pr_debug("Error writing APTPL metadata file: %s\n", path);
1986 return (ret < 0) ? -EIO : 0;
1990 * Clear the APTPL metadata if APTPL has been disabled, otherwise
1991 * write out the updated metadata to struct file for this SCSI device.
1993 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
1996 int rc, len = PR_APTPL_BUF_LEN;
1999 char *null_buf = "No Registrations or Reservations\n";
2001 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2002 dev->t10_pr.pr_aptpl_active = 0;
2003 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
2006 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2013 return TCM_OUT_OF_RESOURCES;
2015 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
2022 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2024 pr_err("SPC-3 PR: Could not update APTPL\n");
2026 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2028 dev->t10_pr.pr_aptpl_active = 1;
2030 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2034 static sense_reason_t
2035 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2036 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2038 struct se_session *se_sess = cmd->se_sess;
2039 struct se_device *dev = cmd->se_dev;
2040 struct se_lun *se_lun = cmd->se_lun;
2041 struct se_portal_group *se_tpg;
2042 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2043 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2044 unsigned char isid_buf[PR_REG_ISID_LEN] = { };
2045 unsigned char *isid_ptr = NULL;
2046 sense_reason_t ret = TCM_NO_SENSE;
2047 int pr_holder = 0, type;
2049 if (!se_sess || !se_lun) {
2050 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2051 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2053 se_tpg = se_sess->se_tpg;
2055 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2056 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2058 isid_ptr = &isid_buf[0];
2061 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2063 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2066 pr_warn("SPC-3 PR: Reservation Key non-zero"
2067 " for SA REGISTER, returning CONFLICT\n");
2068 return TCM_RESERVATION_CONFLICT;
2071 * Do nothing but return GOOD status.
2078 * Perform the Service Action REGISTER on the Initiator
2079 * Port Endpoint that the PRO was received from on the
2080 * Logical Unit of the SCSI device server.
2082 if (core_scsi3_alloc_registration(cmd->se_dev,
2083 se_sess->se_node_acl, cmd->se_lun,
2084 NULL, cmd->orig_fe_lun, isid_ptr,
2085 sa_res_key, all_tg_pt, aptpl,
2086 register_type, 0)) {
2087 pr_err("Unable to allocate"
2088 " struct t10_pr_registration\n");
2089 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
2093 * Register both the Initiator port that received
2094 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2095 * TransportID from Parameter list and loop through
2096 * fabric dependent parameter list while calling
2097 * logic from of core_scsi3_alloc_registration() for
2098 * each TransportID provided SCSI Initiator Port/Device
2100 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2101 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2105 return core_scsi3_update_and_write_aptpl(dev, aptpl);
2108 /* ok, existing registration */
2110 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2111 pr_err("SPC-3 PR REGISTER: Received"
2112 " res_key: 0x%016Lx does not match"
2113 " existing SA REGISTER res_key:"
2114 " 0x%016Lx\n", res_key,
2115 pr_reg->pr_res_key);
2116 ret = TCM_RESERVATION_CONFLICT;
2121 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2122 " set on a registered nexus\n");
2123 ret = TCM_INVALID_PARAMETER_LIST;
2128 * An existing ALL_TG_PT=1 registration being released
2129 * must also set ALL_TG_PT=1 in the incoming PROUT.
2131 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2132 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2133 " registration exists, but ALL_TG_PT=1 bit not"
2134 " present in received PROUT\n");
2135 ret = TCM_INVALID_CDB_FIELD;
2140 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2144 * Increment PRgeneration counter for struct se_device"
2145 * upon a successful REGISTER, see spc4r17 section 6.3.2
2146 * READ_KEYS service action.
2148 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2149 pr_reg->pr_res_key = sa_res_key;
2150 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2151 " Key for %s to: 0x%016Lx PRgeneration:"
2152 " 0x%08x\n", cmd->se_tfo->fabric_name,
2153 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2154 pr_reg->pr_reg_nacl->initiatorname,
2155 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2159 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2161 type = pr_reg->pr_res_type;
2162 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2164 if (pr_holder < 0) {
2165 ret = TCM_RESERVATION_CONFLICT;
2169 spin_lock(&pr_tmpl->registration_lock);
2171 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2172 * and matching pr_res_key.
2174 if (pr_reg->pr_reg_all_tg_pt) {
2175 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2176 &pr_tmpl->registration_list,
2179 if (!pr_reg_p->pr_reg_all_tg_pt)
2181 if (pr_reg_p->pr_res_key != res_key)
2183 if (pr_reg == pr_reg_p)
2185 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2186 pr_reg_p->pr_reg_nacl->initiatorname))
2189 __core_scsi3_free_registration(dev,
2195 * Release the calling I_T Nexus registration now..
2197 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2201 * From spc4r17, section 5.7.11.3 Unregistering
2203 * If the persistent reservation is a registrants only
2204 * type, the device server shall establish a unit
2205 * attention condition for the initiator port associated
2206 * with every registered I_T nexus except for the I_T
2207 * nexus on which the PERSISTENT RESERVE OUT command was
2208 * received, with the additional sense code set to
2209 * RESERVATIONS RELEASED.
2212 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2213 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2214 list_for_each_entry(pr_reg_p,
2215 &pr_tmpl->registration_list,
2218 target_ua_allocate_lun(
2219 pr_reg_p->pr_reg_nacl,
2220 pr_reg_p->pr_res_mapped_lun,
2222 ASCQ_2AH_RESERVATIONS_RELEASED);
2226 spin_unlock(&pr_tmpl->registration_lock);
2229 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2233 core_scsi3_put_pr_reg(pr_reg);
2237 unsigned char *core_scsi3_pr_dump_type(int type)
2240 case PR_TYPE_WRITE_EXCLUSIVE:
2241 return "Write Exclusive Access";
2242 case PR_TYPE_EXCLUSIVE_ACCESS:
2243 return "Exclusive Access";
2244 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2245 return "Write Exclusive Access, Registrants Only";
2246 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2247 return "Exclusive Access, Registrants Only";
2248 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2249 return "Write Exclusive Access, All Registrants";
2250 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2251 return "Exclusive Access, All Registrants";
2256 return "Unknown SPC-3 PR Type";
2259 static sense_reason_t
2260 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2262 struct se_device *dev = cmd->se_dev;
2263 struct se_session *se_sess = cmd->se_sess;
2264 struct se_lun *se_lun = cmd->se_lun;
2265 struct t10_pr_registration *pr_reg, *pr_res_holder;
2266 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2267 char i_buf[PR_REG_ISID_ID_LEN] = { };
2270 if (!se_sess || !se_lun) {
2271 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2272 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2275 * Locate the existing *pr_reg via struct se_node_acl pointers
2277 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2280 pr_err("SPC-3 PR: Unable to locate"
2281 " PR_REGISTERED *pr_reg for RESERVE\n");
2282 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2285 * From spc4r17 Section 5.7.9: Reserving:
2287 * An application client creates a persistent reservation by issuing
2288 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2289 * a registered I_T nexus with the following parameters:
2290 * a) RESERVATION KEY set to the value of the reservation key that is
2291 * registered with the logical unit for the I_T nexus; and
2293 if (res_key != pr_reg->pr_res_key) {
2294 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2295 " does not match existing SA REGISTER res_key:"
2296 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2297 ret = TCM_RESERVATION_CONFLICT;
2298 goto out_put_pr_reg;
2301 * From spc4r17 Section 5.7.9: Reserving:
2304 * b) TYPE field and SCOPE field set to the persistent reservation
2307 * Only one persistent reservation is allowed at a time per logical unit
2308 * and that persistent reservation has a scope of LU_SCOPE.
2310 if (scope != PR_SCOPE_LU_SCOPE) {
2311 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2312 ret = TCM_INVALID_PARAMETER_LIST;
2313 goto out_put_pr_reg;
2316 * See if we have an existing PR reservation holder pointer at
2317 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2320 spin_lock(&dev->dev_reservation_lock);
2321 pr_res_holder = dev->dev_pr_res_holder;
2322 if (pr_res_holder) {
2324 * From spc4r17 Section 5.7.9: Reserving:
2326 * If the device server receives a PERSISTENT RESERVE OUT
2327 * command from an I_T nexus other than a persistent reservation
2328 * holder (see 5.7.10) that attempts to create a persistent
2329 * reservation when a persistent reservation already exists for
2330 * the logical unit, then the command shall be completed with
2331 * RESERVATION CONFLICT status.
2333 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2334 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2335 pr_err("SPC-3 PR: Attempted RESERVE from"
2336 " [%s]: %s while reservation already held by"
2337 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2338 cmd->se_tfo->fabric_name,
2339 se_sess->se_node_acl->initiatorname,
2340 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2341 pr_res_holder->pr_reg_nacl->initiatorname);
2343 spin_unlock(&dev->dev_reservation_lock);
2344 ret = TCM_RESERVATION_CONFLICT;
2345 goto out_put_pr_reg;
2348 * From spc4r17 Section 5.7.9: Reserving:
2350 * If a persistent reservation holder attempts to modify the
2351 * type or scope of an existing persistent reservation, the
2352 * command shall be completed with RESERVATION CONFLICT status.
2354 if ((pr_res_holder->pr_res_type != type) ||
2355 (pr_res_holder->pr_res_scope != scope)) {
2356 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2357 pr_err("SPC-3 PR: Attempted RESERVE from"
2358 " [%s]: %s trying to change TYPE and/or SCOPE,"
2359 " while reservation already held by [%s]: %s,"
2360 " returning RESERVATION_CONFLICT\n",
2361 cmd->se_tfo->fabric_name,
2362 se_sess->se_node_acl->initiatorname,
2363 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2364 pr_res_holder->pr_reg_nacl->initiatorname);
2366 spin_unlock(&dev->dev_reservation_lock);
2367 ret = TCM_RESERVATION_CONFLICT;
2368 goto out_put_pr_reg;
2371 * From spc4r17 Section 5.7.9: Reserving:
2373 * If the device server receives a PERSISTENT RESERVE OUT
2374 * command with RESERVE service action where the TYPE field and
2375 * the SCOPE field contain the same values as the existing type
2376 * and scope from a persistent reservation holder, it shall not
2377 * make any change to the existing persistent reservation and
2378 * shall completethe command with GOOD status.
2380 spin_unlock(&dev->dev_reservation_lock);
2382 goto out_put_pr_reg;
2385 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2386 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2388 pr_reg->pr_res_scope = scope;
2389 pr_reg->pr_res_type = type;
2390 pr_reg->pr_res_holder = 1;
2391 dev->dev_pr_res_holder = pr_reg;
2392 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2394 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2395 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2396 cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
2397 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2398 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2399 cmd->se_tfo->fabric_name,
2400 se_sess->se_node_acl->initiatorname,
2402 spin_unlock(&dev->dev_reservation_lock);
2404 if (pr_tmpl->pr_aptpl_active)
2405 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2409 core_scsi3_put_pr_reg(pr_reg);
2413 static sense_reason_t
2414 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2418 case PR_TYPE_WRITE_EXCLUSIVE:
2419 case PR_TYPE_EXCLUSIVE_ACCESS:
2420 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2421 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2422 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2423 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2424 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2426 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2428 return TCM_INVALID_CDB_FIELD;
2432 static void __core_scsi3_complete_pro_release(
2433 struct se_device *dev,
2434 struct se_node_acl *se_nacl,
2435 struct t10_pr_registration *pr_reg,
2439 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2440 char i_buf[PR_REG_ISID_ID_LEN] = { };
2441 int pr_res_type = 0, pr_res_scope = 0;
2443 lockdep_assert_held(&dev->dev_reservation_lock);
2445 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2447 * Go ahead and release the current PR reservation holder.
2448 * If an All Registrants reservation is currently active and
2449 * a unregister operation is requested, replace the current
2450 * dev_pr_res_holder with another active registration.
2452 if (dev->dev_pr_res_holder) {
2453 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2454 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2455 dev->dev_pr_res_holder->pr_res_type = 0;
2456 dev->dev_pr_res_holder->pr_res_scope = 0;
2457 dev->dev_pr_res_holder->pr_res_holder = 0;
2458 dev->dev_pr_res_holder = NULL;
2463 spin_lock(&dev->t10_pr.registration_lock);
2464 list_del_init(&pr_reg->pr_reg_list);
2466 * If the I_T nexus is a reservation holder, the persistent reservation
2467 * is of an all registrants type, and the I_T nexus is the last remaining
2468 * registered I_T nexus, then the device server shall also release the
2469 * persistent reservation.
2471 if (!list_empty(&dev->t10_pr.registration_list) &&
2472 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2473 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2474 dev->dev_pr_res_holder =
2475 list_entry(dev->t10_pr.registration_list.next,
2476 struct t10_pr_registration, pr_reg_list);
2477 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2478 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2479 dev->dev_pr_res_holder->pr_res_holder = 1;
2481 spin_unlock(&dev->t10_pr.registration_lock);
2483 if (!dev->dev_pr_res_holder) {
2484 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2485 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2486 tfo->fabric_name, (explicit) ? "explicit" :
2487 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2488 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2490 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2491 tfo->fabric_name, se_nacl->initiatorname,
2494 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2496 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2499 static sense_reason_t
2500 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2503 struct se_device *dev = cmd->se_dev;
2504 struct se_session *se_sess = cmd->se_sess;
2505 struct se_lun *se_lun = cmd->se_lun;
2506 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2507 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2508 sense_reason_t ret = 0;
2510 if (!se_sess || !se_lun) {
2511 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2512 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2515 * Locate the existing *pr_reg via struct se_node_acl pointers
2517 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2519 pr_err("SPC-3 PR: Unable to locate"
2520 " PR_REGISTERED *pr_reg for RELEASE\n");
2521 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2524 * From spc4r17 Section 5.7.11.2 Releasing:
2526 * If there is no persistent reservation or in response to a persistent
2527 * reservation release request from a registered I_T nexus that is not a
2528 * persistent reservation holder (see 5.7.10), the device server shall
2531 * a) Not release the persistent reservation, if any;
2532 * b) Not remove any registrations; and
2533 * c) Complete the command with GOOD status.
2535 spin_lock(&dev->dev_reservation_lock);
2536 pr_res_holder = dev->dev_pr_res_holder;
2537 if (!pr_res_holder) {
2539 * No persistent reservation, return GOOD status.
2541 spin_unlock(&dev->dev_reservation_lock);
2542 goto out_put_pr_reg;
2545 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2547 * Release request from a registered I_T nexus that is not a
2548 * persistent reservation holder. return GOOD status.
2550 spin_unlock(&dev->dev_reservation_lock);
2551 goto out_put_pr_reg;
2555 * From spc4r17 Section 5.7.11.2 Releasing:
2557 * Only the persistent reservation holder (see 5.7.10) is allowed to
2558 * release a persistent reservation.
2560 * An application client releases the persistent reservation by issuing
2561 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2562 * an I_T nexus that is a persistent reservation holder with the
2563 * following parameters:
2565 * a) RESERVATION KEY field set to the value of the reservation key
2566 * that is registered with the logical unit for the I_T nexus;
2568 if (res_key != pr_reg->pr_res_key) {
2569 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2570 " does not match existing SA REGISTER res_key:"
2571 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2572 spin_unlock(&dev->dev_reservation_lock);
2573 ret = TCM_RESERVATION_CONFLICT;
2574 goto out_put_pr_reg;
2577 * From spc4r17 Section 5.7.11.2 Releasing and above:
2579 * b) TYPE field and SCOPE field set to match the persistent
2580 * reservation being released.
2582 if ((pr_res_holder->pr_res_type != type) ||
2583 (pr_res_holder->pr_res_scope != scope)) {
2584 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2585 pr_err("SPC-3 PR RELEASE: Attempted to release"
2586 " reservation from [%s]: %s with different TYPE "
2587 "and/or SCOPE while reservation already held by"
2588 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2589 cmd->se_tfo->fabric_name,
2590 se_sess->se_node_acl->initiatorname,
2591 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2592 pr_res_holder->pr_reg_nacl->initiatorname);
2594 spin_unlock(&dev->dev_reservation_lock);
2595 ret = TCM_RESERVATION_CONFLICT;
2596 goto out_put_pr_reg;
2599 * In response to a persistent reservation release request from the
2600 * persistent reservation holder the device server shall perform a
2601 * release by doing the following as an uninterrupted series of actions:
2602 * a) Release the persistent reservation;
2603 * b) Not remove any registration(s);
2604 * c) If the released persistent reservation is a registrants only type
2605 * or all registrants type persistent reservation,
2606 * the device server shall establish a unit attention condition for
2607 * the initiator port associated with every regis-
2608 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2609 * RESERVE OUT command with RELEASE service action was received,
2610 * with the additional sense code set to RESERVATIONS RELEASED; and
2611 * d) If the persistent reservation is of any other type, the device
2612 * server shall not establish a unit attention condition.
2614 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2617 spin_unlock(&dev->dev_reservation_lock);
2619 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2620 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2621 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2622 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2624 * If no UNIT ATTENTION conditions will be established for
2625 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2626 * go ahead and check for APTPL=1 update+write below
2631 spin_lock(&pr_tmpl->registration_lock);
2632 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2635 * Do not establish a UNIT ATTENTION condition
2636 * for the calling I_T Nexus
2638 if (pr_reg_p == pr_reg)
2641 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2642 pr_reg_p->pr_res_mapped_lun,
2643 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2645 spin_unlock(&pr_tmpl->registration_lock);
2648 if (pr_tmpl->pr_aptpl_active)
2649 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2652 core_scsi3_put_pr_reg(pr_reg);
2656 static sense_reason_t
2657 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2659 struct se_device *dev = cmd->se_dev;
2660 struct se_node_acl *pr_reg_nacl;
2661 struct se_session *se_sess = cmd->se_sess;
2662 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2663 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2664 u64 pr_res_mapped_lun = 0;
2665 int calling_it_nexus = 0;
2667 * Locate the existing *pr_reg via struct se_node_acl pointers
2669 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2670 se_sess->se_node_acl, se_sess);
2672 pr_err("SPC-3 PR: Unable to locate"
2673 " PR_REGISTERED *pr_reg for CLEAR\n");
2674 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2677 * From spc4r17 section 5.7.11.6, Clearing:
2679 * Any application client may release the persistent reservation and
2680 * remove all registrations from a device server by issuing a
2681 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2682 * registered I_T nexus with the following parameter:
2684 * a) RESERVATION KEY field set to the value of the reservation key
2685 * that is registered with the logical unit for the I_T nexus.
2687 if (res_key != pr_reg_n->pr_res_key) {
2688 pr_err("SPC-3 PR REGISTER: Received"
2689 " res_key: 0x%016Lx does not match"
2690 " existing SA REGISTER res_key:"
2691 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2692 core_scsi3_put_pr_reg(pr_reg_n);
2693 return TCM_RESERVATION_CONFLICT;
2696 * a) Release the persistent reservation, if any;
2698 spin_lock(&dev->dev_reservation_lock);
2699 pr_res_holder = dev->dev_pr_res_holder;
2700 if (pr_res_holder) {
2701 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2702 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2703 pr_res_holder, 0, 0);
2705 spin_unlock(&dev->dev_reservation_lock);
2707 * b) Remove all registration(s) (see spc4r17 5.7.7);
2709 spin_lock(&pr_tmpl->registration_lock);
2710 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2711 &pr_tmpl->registration_list, pr_reg_list) {
2713 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2714 pr_reg_nacl = pr_reg->pr_reg_nacl;
2715 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2716 __core_scsi3_free_registration(dev, pr_reg, NULL,
2719 * e) Establish a unit attention condition for the initiator
2720 * port associated with every registered I_T nexus other
2721 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2722 * command with CLEAR service action was received, with the
2723 * additional sense code set to RESERVATIONS PREEMPTED.
2725 if (!calling_it_nexus)
2726 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2727 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2729 spin_unlock(&pr_tmpl->registration_lock);
2731 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2732 cmd->se_tfo->fabric_name);
2734 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2736 core_scsi3_pr_generation(dev);
2740 static void __core_scsi3_complete_pro_preempt(
2741 struct se_device *dev,
2742 struct t10_pr_registration *pr_reg,
2743 struct list_head *preempt_and_abort_list,
2746 enum preempt_type preempt_type)
2748 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2749 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2750 char i_buf[PR_REG_ISID_ID_LEN] = { };
2752 lockdep_assert_held(&dev->dev_reservation_lock);
2754 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2756 * Do an implicit RELEASE of the existing reservation.
2758 if (dev->dev_pr_res_holder)
2759 __core_scsi3_complete_pro_release(dev, nacl,
2760 dev->dev_pr_res_holder, 0, 0);
2762 dev->dev_pr_res_holder = pr_reg;
2763 pr_reg->pr_res_holder = 1;
2764 pr_reg->pr_res_type = type;
2765 pr_reg->pr_res_scope = scope;
2767 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2768 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2769 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2770 core_scsi3_pr_dump_type(type),
2771 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2772 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2773 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2774 nacl->initiatorname, i_buf);
2776 * For PREEMPT_AND_ABORT, add the preempting reservation's
2777 * struct t10_pr_registration to the list that will be compared
2778 * against received CDBs..
2780 if (preempt_and_abort_list)
2781 list_add_tail(&pr_reg->pr_reg_abort_list,
2782 preempt_and_abort_list);
2785 static void core_scsi3_release_preempt_and_abort(
2786 struct list_head *preempt_and_abort_list,
2787 struct t10_pr_registration *pr_reg_holder)
2789 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2791 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2792 pr_reg_abort_list) {
2794 list_del(&pr_reg->pr_reg_abort_list);
2795 if (pr_reg_holder == pr_reg)
2797 if (pr_reg->pr_res_holder) {
2798 pr_warn("pr_reg->pr_res_holder still set\n");
2802 pr_reg->pr_reg_deve = NULL;
2803 pr_reg->pr_reg_nacl = NULL;
2804 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2808 static sense_reason_t
2809 core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2810 u64 sa_res_key, enum preempt_type preempt_type)
2812 struct se_device *dev = cmd->se_dev;
2813 struct se_node_acl *pr_reg_nacl;
2814 struct se_session *se_sess = cmd->se_sess;
2815 LIST_HEAD(preempt_and_abort_list);
2816 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2817 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2818 u64 pr_res_mapped_lun = 0;
2819 int all_reg = 0, calling_it_nexus = 0;
2820 bool sa_res_key_unmatched = sa_res_key != 0;
2821 int prh_type = 0, prh_scope = 0;
2824 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2826 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2829 pr_err("SPC-3 PR: Unable to locate"
2830 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2831 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2832 return TCM_RESERVATION_CONFLICT;
2834 if (pr_reg_n->pr_res_key != res_key) {
2835 core_scsi3_put_pr_reg(pr_reg_n);
2836 return TCM_RESERVATION_CONFLICT;
2838 if (scope != PR_SCOPE_LU_SCOPE) {
2839 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2840 core_scsi3_put_pr_reg(pr_reg_n);
2841 return TCM_INVALID_PARAMETER_LIST;
2844 spin_lock(&dev->dev_reservation_lock);
2845 pr_res_holder = dev->dev_pr_res_holder;
2846 if (pr_res_holder &&
2847 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2848 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2851 if (!all_reg && !sa_res_key) {
2852 spin_unlock(&dev->dev_reservation_lock);
2853 core_scsi3_put_pr_reg(pr_reg_n);
2854 return TCM_INVALID_PARAMETER_LIST;
2857 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2859 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2860 * persistent reservation holder or there is no persistent reservation
2861 * holder (i.e., there is no persistent reservation), then the device
2862 * server shall perform a preempt by doing the following in an
2863 * uninterrupted series of actions. (See below..)
2865 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2867 * No existing or SA Reservation Key matching reservations..
2869 * PROUT SA PREEMPT with All Registrant type reservations are
2870 * allowed to be processed without a matching SA Reservation Key
2872 spin_lock(&pr_tmpl->registration_lock);
2873 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2874 &pr_tmpl->registration_list, pr_reg_list) {
2876 * Removing of registrations in non all registrants
2877 * type reservations without a matching SA reservation
2880 * a) Remove the registrations for all I_T nexuses
2881 * specified by the SERVICE ACTION RESERVATION KEY
2883 * b) Ignore the contents of the SCOPE and TYPE fields;
2884 * c) Process tasks as defined in 5.7.1; and
2885 * d) Establish a unit attention condition for the
2886 * initiator port associated with every I_T nexus
2887 * that lost its registration other than the I_T
2888 * nexus on which the PERSISTENT RESERVE OUT command
2889 * was received, with the additional sense code set
2890 * to REGISTRATIONS PREEMPTED.
2893 if (pr_reg->pr_res_key != sa_res_key)
2895 sa_res_key_unmatched = false;
2897 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2898 pr_reg_nacl = pr_reg->pr_reg_nacl;
2899 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2900 __core_scsi3_free_registration(dev, pr_reg,
2901 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2902 NULL, calling_it_nexus);
2905 * Case for any existing all registrants type
2906 * reservation, follow logic in spc4r17 section
2907 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2909 * For a ZERO SA Reservation key, release
2910 * all other registrations and do an implicit
2911 * release of active persistent reservation.
2913 * For a non-ZERO SA Reservation key, only
2914 * release the matching reservation key from
2918 (pr_reg->pr_res_key != sa_res_key))
2920 sa_res_key_unmatched = false;
2922 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2923 if (calling_it_nexus)
2926 pr_reg_nacl = pr_reg->pr_reg_nacl;
2927 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2928 __core_scsi3_free_registration(dev, pr_reg,
2929 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2932 if (!calling_it_nexus)
2933 target_ua_allocate_lun(pr_reg_nacl,
2934 pr_res_mapped_lun, 0x2A,
2935 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2937 spin_unlock(&pr_tmpl->registration_lock);
2939 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2940 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2941 * RESERVATION KEY field to a value that does not match any
2942 * registered reservation key, then the device server shall
2943 * complete the command with RESERVATION CONFLICT status.
2945 if (sa_res_key_unmatched) {
2946 spin_unlock(&dev->dev_reservation_lock);
2947 core_scsi3_put_pr_reg(pr_reg_n);
2948 return TCM_RESERVATION_CONFLICT;
2951 * For an existing all registrants type reservation
2952 * with a zero SA rservation key, preempt the existing
2953 * reservation with the new PR type and scope.
2955 if (pr_res_holder && all_reg && !(sa_res_key)) {
2956 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
2957 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2958 type, scope, preempt_type);
2961 spin_unlock(&dev->dev_reservation_lock);
2964 * SPC-4 5.12.11.2.6 Preempting and aborting
2965 * The actions described in this subclause shall be performed
2966 * for all I_T nexuses that are registered with the non-zero
2967 * SERVICE ACTION RESERVATION KEY value, without regard for
2968 * whether the preempted I_T nexuses hold the persistent
2969 * reservation. If the SERVICE ACTION RESERVATION KEY field is
2970 * set to zero and an all registrants persistent reservation is
2971 * present, the device server shall abort all commands for all
2972 * registered I_T nexuses.
2974 if (preempt_type == PREEMPT_AND_ABORT) {
2975 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list,
2977 core_scsi3_release_preempt_and_abort(
2978 &preempt_and_abort_list, pr_reg_n);
2981 if (pr_tmpl->pr_aptpl_active)
2982 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2984 core_scsi3_put_pr_reg(pr_reg_n);
2985 core_scsi3_pr_generation(cmd->se_dev);
2989 * The PREEMPTing SA reservation key matches that of the
2990 * existing persistent reservation, first, we check if
2991 * we are preempting our own reservation.
2992 * From spc4r17, section 5.7.11.4.3 Preempting
2993 * persistent reservations and registration handling
2995 * If an all registrants persistent reservation is not
2996 * present, it is not an error for the persistent
2997 * reservation holder to preempt itself (i.e., a
2998 * PERSISTENT RESERVE OUT with a PREEMPT service action
2999 * or a PREEMPT AND ABORT service action with the
3000 * SERVICE ACTION RESERVATION KEY value equal to the
3001 * persistent reservation holder's reservation key that
3002 * is received from the persistent reservation holder).
3003 * In that case, the device server shall establish the
3004 * new persistent reservation and maintain the
3007 prh_type = pr_res_holder->pr_res_type;
3008 prh_scope = pr_res_holder->pr_res_scope;
3010 * If the SERVICE ACTION RESERVATION KEY field identifies a
3011 * persistent reservation holder (see 5.7.10), the device
3012 * server shall perform a preempt by doing the following as
3013 * an uninterrupted series of actions:
3015 * a) Release the persistent reservation for the holder
3016 * identified by the SERVICE ACTION RESERVATION KEY field;
3018 if (pr_reg_n != pr_res_holder)
3019 __core_scsi3_complete_pro_release(dev,
3020 pr_res_holder->pr_reg_nacl,
3021 dev->dev_pr_res_holder, 0, 0);
3023 * b) Remove the registrations for all I_T nexuses identified
3024 * by the SERVICE ACTION RESERVATION KEY field, except the
3025 * I_T nexus that is being used for the PERSISTENT RESERVE
3026 * OUT command. If an all registrants persistent reservation
3027 * is present and the SERVICE ACTION RESERVATION KEY field
3028 * is set to zero, then all registrations shall be removed
3029 * except for that of the I_T nexus that is being used for
3030 * the PERSISTENT RESERVE OUT command;
3032 spin_lock(&pr_tmpl->registration_lock);
3033 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3034 &pr_tmpl->registration_list, pr_reg_list) {
3036 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3037 if (calling_it_nexus)
3040 if (sa_res_key && pr_reg->pr_res_key != sa_res_key)
3043 pr_reg_nacl = pr_reg->pr_reg_nacl;
3044 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3045 __core_scsi3_free_registration(dev, pr_reg,
3046 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3049 * e) Establish a unit attention condition for the initiator
3050 * port associated with every I_T nexus that lost its
3051 * persistent reservation and/or registration, with the
3052 * additional sense code set to REGISTRATIONS PREEMPTED;
3054 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3055 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3057 spin_unlock(&pr_tmpl->registration_lock);
3059 * c) Establish a persistent reservation for the preempting
3060 * I_T nexus using the contents of the SCOPE and TYPE fields;
3062 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3063 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3064 type, scope, preempt_type);
3066 * d) Process tasks as defined in 5.7.1;
3068 * f) If the type or scope has changed, then for every I_T nexus
3069 * whose reservation key was not removed, except for the I_T
3070 * nexus on which the PERSISTENT RESERVE OUT command was
3071 * received, the device server shall establish a unit
3072 * attention condition for the initiator port associated with
3073 * that I_T nexus, with the additional sense code set to
3074 * RESERVATIONS RELEASED. If the type or scope have not
3075 * changed, then no unit attention condition(s) shall be
3076 * established for this reason.
3078 if ((prh_type != type) || (prh_scope != scope)) {
3079 spin_lock(&pr_tmpl->registration_lock);
3080 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3081 &pr_tmpl->registration_list, pr_reg_list) {
3083 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3084 if (calling_it_nexus)
3087 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3088 pr_reg->pr_res_mapped_lun, 0x2A,
3089 ASCQ_2AH_RESERVATIONS_RELEASED);
3091 spin_unlock(&pr_tmpl->registration_lock);
3093 spin_unlock(&dev->dev_reservation_lock);
3095 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3096 * All received CDBs for the matching existing reservation and
3097 * registrations undergo ABORT_TASK logic.
3099 * From there, core_scsi3_release_preempt_and_abort() will
3100 * release every registration in the list (which have already
3101 * been removed from the primary pr_reg list), except the
3102 * new persistent reservation holder, the calling Initiator Port.
3104 if (preempt_type == PREEMPT_AND_ABORT) {
3105 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3106 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3110 if (pr_tmpl->pr_aptpl_active)
3111 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3113 core_scsi3_put_pr_reg(pr_reg_n);
3114 core_scsi3_pr_generation(cmd->se_dev);
3118 static sense_reason_t
3119 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3120 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3123 case PR_TYPE_WRITE_EXCLUSIVE:
3124 case PR_TYPE_EXCLUSIVE_ACCESS:
3125 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3126 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3127 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3128 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3129 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3130 sa_res_key, preempt_type);
3132 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3133 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3134 return TCM_INVALID_CDB_FIELD;
3139 static sense_reason_t
3140 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3141 u64 sa_res_key, int aptpl, int unreg)
3143 struct se_session *se_sess = cmd->se_sess;
3144 struct se_device *dev = cmd->se_dev;
3145 struct se_dev_entry *dest_se_deve = NULL;
3146 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3147 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3148 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3149 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3150 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3151 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3153 const unsigned char *initiator_str;
3154 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN] = { };
3155 u32 tid_len, tmp_tid_len;
3156 int new_reg = 0, type, scope, matching_iname;
3158 unsigned short rtpi;
3159 unsigned char proto_ident;
3161 if (!se_sess || !se_lun) {
3162 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3163 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3166 se_tpg = se_sess->se_tpg;
3167 tf_ops = se_tpg->se_tpg_tfo;
3169 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3170 * Register behaviors for a REGISTER AND MOVE service action
3172 * Locate the existing *pr_reg via struct se_node_acl pointers
3174 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3177 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3178 " *pr_reg for REGISTER_AND_MOVE\n");
3179 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3182 * The provided reservation key much match the existing reservation key
3183 * provided during this initiator's I_T nexus registration.
3185 if (res_key != pr_reg->pr_res_key) {
3186 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3187 " res_key: 0x%016Lx does not match existing SA REGISTER"
3188 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3189 ret = TCM_RESERVATION_CONFLICT;
3190 goto out_put_pr_reg;
3193 * The service active reservation key needs to be non zero
3196 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3198 ret = TCM_INVALID_PARAMETER_LIST;
3199 goto out_put_pr_reg;
3203 * Determine the Relative Target Port Identifier where the reservation
3204 * will be moved to for the TransportID containing SCSI initiator WWN
3207 buf = transport_kmap_data_sg(cmd);
3209 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3210 goto out_put_pr_reg;
3213 rtpi = get_unaligned_be16(&buf[18]);
3214 tid_len = get_unaligned_be32(&buf[20]);
3215 transport_kunmap_data_sg(cmd);
3218 if ((tid_len + 24) != cmd->data_length) {
3219 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3220 " does not equal CDB data_length: %u\n", tid_len,
3222 ret = TCM_INVALID_PARAMETER_LIST;
3223 goto out_put_pr_reg;
3226 spin_lock(&dev->se_port_lock);
3227 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3228 if (tmp_lun->lun_tpg->tpg_rtpi != rtpi)
3230 dest_se_tpg = tmp_lun->lun_tpg;
3231 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3235 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3236 spin_unlock(&dev->se_port_lock);
3238 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3239 pr_err("core_scsi3_tpg_depend_item() failed"
3240 " for dest_se_tpg\n");
3241 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3242 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3243 goto out_put_pr_reg;
3246 spin_lock(&dev->se_port_lock);
3249 spin_unlock(&dev->se_port_lock);
3251 if (!dest_se_tpg || !dest_tf_ops) {
3252 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3253 " fabric ops from Relative Target Port Identifier:"
3255 ret = TCM_INVALID_PARAMETER_LIST;
3256 goto out_put_pr_reg;
3259 buf = transport_kmap_data_sg(cmd);
3261 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3262 goto out_put_pr_reg;
3264 proto_ident = (buf[24] & 0x0f);
3266 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3267 " 0x%02x\n", proto_ident);
3269 if (proto_ident != dest_se_tpg->proto_id) {
3270 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3271 " proto_ident: 0x%02x does not match ident: 0x%02x"
3272 " from fabric: %s\n", proto_ident,
3273 dest_se_tpg->proto_id,
3274 dest_tf_ops->fabric_name);
3275 ret = TCM_INVALID_PARAMETER_LIST;
3278 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
3279 &buf[24], &tmp_tid_len, &iport_ptr);
3280 if (!initiator_str) {
3281 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3282 " initiator_str from Transport ID\n");
3283 ret = TCM_INVALID_PARAMETER_LIST;
3287 transport_kunmap_data_sg(cmd);
3290 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3291 " %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
3292 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3295 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3296 * action specifies a TransportID that is the same as the initiator port
3297 * of the I_T nexus for the command received, then the command shall
3298 * be terminated with CHECK CONDITION status, with the sense key set to
3299 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3300 * IN PARAMETER LIST.
3302 pr_reg_nacl = pr_reg->pr_reg_nacl;
3303 matching_iname = (!strcmp(initiator_str,
3304 pr_reg_nacl->initiatorname)) ? 1 : 0;
3305 if (!matching_iname)
3306 goto after_iport_check;
3308 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3309 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3310 " matches: %s on received I_T Nexus\n", initiator_str,
3311 pr_reg_nacl->initiatorname);
3312 ret = TCM_INVALID_PARAMETER_LIST;
3315 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3316 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3317 " matches: %s %s on received I_T Nexus\n",
3318 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3319 pr_reg->pr_reg_isid);
3320 ret = TCM_INVALID_PARAMETER_LIST;
3325 * Locate the destination struct se_node_acl from the received Transport ID
3327 mutex_lock(&dest_se_tpg->acl_node_mutex);
3328 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3331 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3332 mutex_unlock(&dest_se_tpg->acl_node_mutex);
3334 if (!dest_node_acl) {
3335 pr_err("Unable to locate %s dest_node_acl for"
3336 " TransportID%s\n", dest_tf_ops->fabric_name,
3338 ret = TCM_INVALID_PARAMETER_LIST;
3342 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3343 pr_err("core_scsi3_nodeacl_depend_item() for"
3344 " dest_node_acl\n");
3345 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3346 dest_node_acl = NULL;
3347 ret = TCM_INVALID_PARAMETER_LIST;
3351 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3352 " %s from TransportID\n", dest_tf_ops->fabric_name,
3353 dest_node_acl->initiatorname);
3356 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3359 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3360 if (!dest_se_deve) {
3361 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3362 " %hu\n", dest_tf_ops->fabric_name, rtpi);
3363 ret = TCM_INVALID_PARAMETER_LIST;
3367 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3368 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3369 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3370 dest_se_deve = NULL;
3371 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3375 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3376 " ACL for dest_se_deve->mapped_lun: %llu\n",
3377 dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
3378 dest_se_deve->mapped_lun);
3381 * A persistent reservation needs to already existing in order to
3382 * successfully complete the REGISTER_AND_MOVE service action..
3384 spin_lock(&dev->dev_reservation_lock);
3385 pr_res_holder = dev->dev_pr_res_holder;
3386 if (!pr_res_holder) {
3387 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3388 " currently held\n");
3389 spin_unlock(&dev->dev_reservation_lock);
3390 ret = TCM_INVALID_CDB_FIELD;
3394 * The received on I_T Nexus must be the reservation holder.
3396 * From spc4r17 section 5.7.8 Table 50 --
3397 * Register behaviors for a REGISTER AND MOVE service action
3399 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3400 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3401 " Nexus is not reservation holder\n");
3402 spin_unlock(&dev->dev_reservation_lock);
3403 ret = TCM_RESERVATION_CONFLICT;
3407 * From spc4r17 section 5.7.8: registering and moving reservation
3409 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3410 * action is received and the established persistent reservation is a
3411 * Write Exclusive - All Registrants type or Exclusive Access -
3412 * All Registrants type reservation, then the command shall be completed
3413 * with RESERVATION CONFLICT status.
3415 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3416 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3417 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3418 " reservation for type: %s\n",
3419 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3420 spin_unlock(&dev->dev_reservation_lock);
3421 ret = TCM_RESERVATION_CONFLICT;
3424 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3426 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3428 type = pr_res_holder->pr_res_type;
3429 scope = pr_res_holder->pr_res_type;
3431 * c) Associate the reservation key specified in the SERVICE ACTION
3432 * RESERVATION KEY field with the I_T nexus specified as the
3433 * destination of the register and move, where:
3434 * A) The I_T nexus is specified by the TransportID and the
3435 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3436 * B) Regardless of the TransportID format used, the association for
3437 * the initiator port is based on either the initiator port name
3438 * (see 3.1.71) on SCSI transport protocols where port names are
3439 * required or the initiator port identifier (see 3.1.70) on SCSI
3440 * transport protocols where port names are not required;
3441 * d) Register the reservation key specified in the SERVICE ACTION
3442 * RESERVATION KEY field;
3444 * Also, It is not an error for a REGISTER AND MOVE service action to
3445 * register an I_T nexus that is already registered with the same
3446 * reservation key or a different reservation key.
3448 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3451 struct se_lun *dest_lun = dest_se_deve->se_lun;
3453 spin_unlock(&dev->dev_reservation_lock);
3454 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3455 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3456 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
3457 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3460 spin_lock(&dev->dev_reservation_lock);
3461 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3466 * e) Retain the reservation key specified in the SERVICE ACTION
3467 * RESERVATION KEY field and associated information;
3469 dest_pr_reg->pr_res_key = sa_res_key;
3472 * f) Release the persistent reservation for the persistent reservation
3473 * holder (i.e., the I_T nexus on which the
3475 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3476 dev->dev_pr_res_holder, 0, 0);
3478 * g) Move the persistent reservation to the specified I_T nexus using
3479 * the same scope and type as the persistent reservation released in
3482 dev->dev_pr_res_holder = dest_pr_reg;
3483 dest_pr_reg->pr_res_holder = 1;
3484 dest_pr_reg->pr_res_type = type;
3485 pr_reg->pr_res_scope = scope;
3486 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3488 * Increment PRGeneration for existing registrations..
3491 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3492 spin_unlock(&dev->dev_reservation_lock);
3494 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3495 " created new reservation holder TYPE: %s on object RTPI:"
3496 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
3497 core_scsi3_pr_dump_type(type), rtpi,
3498 dest_pr_reg->pr_res_generation);
3499 pr_debug("SPC-3 PR Successfully moved reservation from"
3500 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3501 tf_ops->fabric_name, pr_reg_nacl->initiatorname,
3502 i_buf, dest_tf_ops->fabric_name,
3503 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3506 * It is now safe to release configfs group dependencies for destination
3507 * of Transport ID Initiator Device/Port Identifier
3509 core_scsi3_lunacl_undepend_item(dest_se_deve);
3510 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3511 core_scsi3_tpg_undepend_item(dest_se_tpg);
3513 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3514 * nexus on which PERSISTENT RESERVE OUT command was received.
3517 spin_lock(&pr_tmpl->registration_lock);
3518 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3519 spin_unlock(&pr_tmpl->registration_lock);
3521 core_scsi3_put_pr_reg(pr_reg);
3523 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3525 core_scsi3_put_pr_reg(dest_pr_reg);
3529 transport_kunmap_data_sg(cmd);
3531 core_scsi3_lunacl_undepend_item(dest_se_deve);
3533 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3534 core_scsi3_tpg_undepend_item(dest_se_tpg);
3537 core_scsi3_put_pr_reg(pr_reg);
3542 * See spc4r17 section 6.14 Table 170
3545 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3547 struct se_device *dev = cmd->se_dev;
3548 unsigned char *cdb = &cmd->t_task_cdb[0];
3550 u64 res_key, sa_res_key;
3551 int sa, scope, type, aptpl;
3552 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3556 * Following spc2r20 5.5.1 Reservations overview:
3558 * If a logical unit has been reserved by any RESERVE command and is
3559 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3560 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3561 * initiator or service action and shall terminate with a RESERVATION
3564 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3565 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3566 " SPC-2 reservation is held, returning"
3567 " RESERVATION_CONFLICT\n");
3568 return TCM_RESERVATION_CONFLICT;
3572 * FIXME: A NULL struct se_session pointer means an this is not coming from
3573 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3576 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3578 if (cmd->data_length < 24) {
3579 pr_warn("SPC-PR: Received PR OUT parameter list"
3580 " length too small: %u\n", cmd->data_length);
3581 return TCM_PARAMETER_LIST_LENGTH_ERROR;
3585 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3587 sa = (cdb[1] & 0x1f);
3588 scope = (cdb[2] & 0xf0);
3589 type = (cdb[2] & 0x0f);
3591 buf = transport_kmap_data_sg(cmd);
3593 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3596 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3598 res_key = get_unaligned_be64(&buf[0]);
3599 sa_res_key = get_unaligned_be64(&buf[8]);
3601 * REGISTER_AND_MOVE uses a different SA parameter list containing
3602 * SCSI TransportIDs.
3604 if (sa != PRO_REGISTER_AND_MOVE) {
3605 spec_i_pt = (buf[20] & 0x08);
3606 all_tg_pt = (buf[20] & 0x04);
3607 aptpl = (buf[20] & 0x01);
3609 aptpl = (buf[17] & 0x01);
3610 unreg = (buf[17] & 0x02);
3613 * If the backend device has been configured to force APTPL metadata
3614 * write-out, go ahead and propigate aptpl=1 down now.
3616 if (dev->dev_attrib.force_pr_aptpl)
3619 transport_kunmap_data_sg(cmd);
3623 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3625 if (spec_i_pt && (sa != PRO_REGISTER))
3626 return TCM_INVALID_PARAMETER_LIST;
3629 * From spc4r17 section 6.14:
3631 * If the SPEC_I_PT bit is set to zero, the service action is not
3632 * REGISTER AND MOVE, and the parameter list length is not 24, then
3633 * the command shall be terminated with CHECK CONDITION status, with
3634 * the sense key set to ILLEGAL REQUEST, and the additional sense
3635 * code set to PARAMETER LIST LENGTH ERROR.
3637 if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
3638 (cmd->data_length != 24)) {
3639 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3640 " list length: %u\n", cmd->data_length);
3641 return TCM_PARAMETER_LIST_LENGTH_ERROR;
3645 * (core_scsi3_emulate_pro_* function parameters
3646 * are defined by spc4r17 Table 174:
3647 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3651 ret = core_scsi3_emulate_pro_register(cmd,
3652 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3655 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3658 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3661 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3664 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3665 res_key, sa_res_key, PREEMPT);
3667 case PRO_PREEMPT_AND_ABORT:
3668 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3669 res_key, sa_res_key, PREEMPT_AND_ABORT);
3671 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3672 ret = core_scsi3_emulate_pro_register(cmd,
3673 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3675 case PRO_REGISTER_AND_MOVE:
3676 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3677 sa_res_key, aptpl, unreg);
3680 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3681 " action: 0x%02x\n", sa);
3682 return TCM_INVALID_CDB_FIELD;
3686 target_complete_cmd(cmd, SAM_STAT_GOOD);
3691 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3693 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3695 static sense_reason_t
3696 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3698 struct se_device *dev = cmd->se_dev;
3699 struct t10_pr_registration *pr_reg;
3701 u32 add_len = 0, off = 8;
3703 if (cmd->data_length < 8) {
3704 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3705 " too small\n", cmd->data_length);
3706 return TCM_INVALID_CDB_FIELD;
3709 buf = transport_kmap_data_sg(cmd);
3711 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3713 put_unaligned_be32(dev->t10_pr.pr_generation, buf);
3715 spin_lock(&dev->t10_pr.registration_lock);
3716 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3719 * Check for overflow of 8byte PRI READ_KEYS payload and
3720 * next reservation key list descriptor.
3722 if (off + 8 <= cmd->data_length) {
3723 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3727 * SPC5r17: 6.16.2 READ KEYS service action
3728 * The ADDITIONAL LENGTH field indicates the number of bytes in
3729 * the Reservation key list. The contents of the ADDITIONAL
3730 * LENGTH field are not altered based on the allocation length
3734 spin_unlock(&dev->t10_pr.registration_lock);
3736 put_unaligned_be32(add_len, &buf[4]);
3737 target_set_cmd_data_length(cmd, 8 + add_len);
3739 transport_kunmap_data_sg(cmd);
3745 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3747 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3749 static sense_reason_t
3750 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3752 struct se_device *dev = cmd->se_dev;
3753 struct t10_pr_registration *pr_reg;
3758 if (cmd->data_length < 8) {
3759 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3760 " too small\n", cmd->data_length);
3761 return TCM_INVALID_CDB_FIELD;
3764 buf = transport_kmap_data_sg(cmd);
3766 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3768 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3770 spin_lock(&dev->dev_reservation_lock);
3771 pr_reg = dev->dev_pr_res_holder;
3774 * Set the Additional Length to 16 when a reservation is held
3777 put_unaligned_be32(add_len, &buf[4]);
3779 if (cmd->data_length < 22)
3783 * Set the Reservation key.
3785 * From spc4r17, section 5.7.10:
3786 * A persistent reservation holder has its reservation key
3787 * returned in the parameter data from a PERSISTENT
3788 * RESERVE IN command with READ RESERVATION service action as
3790 * a) For a persistent reservation of the type Write Exclusive
3791 * - All Registrants or Exclusive Access  All Regitrants,
3792 * the reservation key shall be set to zero; or
3793 * b) For all other persistent reservation types, the
3794 * reservation key shall be set to the registered
3795 * reservation key for the I_T nexus that holds the
3796 * persistent reservation.
3798 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3799 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3802 pr_res_key = pr_reg->pr_res_key;
3804 put_unaligned_be64(pr_res_key, &buf[8]);
3806 * Set the SCOPE and TYPE
3808 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3809 (pr_reg->pr_res_type & 0x0f);
3812 target_set_cmd_data_length(cmd, 8 + add_len);
3815 spin_unlock(&dev->dev_reservation_lock);
3816 transport_kunmap_data_sg(cmd);
3822 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3824 * See spc4r17 section 6.13.4 Table 165
3826 static sense_reason_t
3827 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3829 struct se_device *dev = cmd->se_dev;
3830 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3832 u16 len = 8; /* Hardcoded to 8. */
3834 if (cmd->data_length < 6) {
3835 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3836 " %u too small\n", cmd->data_length);
3837 return TCM_INVALID_CDB_FIELD;
3840 buf = transport_kmap_data_sg(cmd);
3842 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3844 put_unaligned_be16(len, &buf[0]);
3845 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3846 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3847 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3848 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3850 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3851 * set the TMV: Task Mask Valid bit.
3855 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3857 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3859 * PTPL_A: Persistence across Target Power Loss Active bit
3861 if (pr_tmpl->pr_aptpl_active)
3864 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3866 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3867 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3868 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3869 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3870 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3871 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3873 target_set_cmd_data_length(cmd, len);
3875 transport_kunmap_data_sg(cmd);
3881 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3883 * See spc4r17 section 6.13.5 Table 168 and 169
3885 static sense_reason_t
3886 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3888 struct se_device *dev = cmd->se_dev;
3889 struct se_node_acl *se_nacl;
3890 struct se_portal_group *se_tpg;
3891 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3892 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3894 u32 add_desc_len = 0, add_len = 0;
3895 u32 off = 8; /* off into first Full Status descriptor */
3896 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3897 int exp_desc_len, desc_len;
3898 bool all_reg = false;
3900 if (cmd->data_length < 8) {
3901 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3902 " too small\n", cmd->data_length);
3903 return TCM_INVALID_CDB_FIELD;
3906 buf = transport_kmap_data_sg(cmd);
3908 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3910 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3912 spin_lock(&dev->dev_reservation_lock);
3913 if (dev->dev_pr_res_holder) {
3914 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3916 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3917 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3919 pr_res_type = pr_holder->pr_res_type;
3920 pr_res_scope = pr_holder->pr_res_scope;
3923 spin_unlock(&dev->dev_reservation_lock);
3925 spin_lock(&pr_tmpl->registration_lock);
3926 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3927 &pr_tmpl->registration_list, pr_reg_list) {
3929 se_nacl = pr_reg->pr_reg_nacl;
3930 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3933 atomic_inc_mb(&pr_reg->pr_res_holders);
3934 spin_unlock(&pr_tmpl->registration_lock);
3936 * Determine expected length of $FABRIC_MOD specific
3937 * TransportID full status descriptor..
3939 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3941 if (exp_desc_len < 0 ||
3942 exp_desc_len + add_len > cmd->data_length) {
3943 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3944 " out of buffer: %d\n", cmd->data_length);
3945 spin_lock(&pr_tmpl->registration_lock);
3946 atomic_dec_mb(&pr_reg->pr_res_holders);
3950 * Set RESERVATION KEY
3952 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3954 off += 4; /* Skip Over Reserved area */
3957 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3959 if (pr_reg->pr_reg_all_tg_pt)
3962 * The struct se_lun pointer will be present for the
3963 * reservation holder for PR_HOLDER bit.
3965 * Also, if this registration is the reservation
3966 * holder or there is an All Registrants reservation
3967 * active, fill in SCOPE and TYPE in the next byte.
3969 if (pr_reg->pr_res_holder) {
3971 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3972 (pr_reg->pr_res_type & 0x0f);
3973 } else if (all_reg) {
3975 buf[off++] = (pr_res_scope & 0xf0) |
3976 (pr_res_type & 0x0f);
3981 off += 4; /* Skip over reserved area */
3983 * From spc4r17 6.3.15:
3985 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3986 * IDENTIFIER field contains the relative port identifier (see
3987 * 3.1.120) of the target port that is part of the I_T nexus
3988 * described by this full status descriptor. If the ALL_TG_PT
3989 * bit is set to one, the contents of the RELATIVE TARGET PORT
3990 * IDENTIFIER field are not defined by this standard.
3992 if (!pr_reg->pr_reg_all_tg_pt) {
3993 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
3995 put_unaligned_be16(sep_rtpi, &buf[off]);
3998 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4000 buf[off+4] = se_tpg->proto_id;
4003 * Now, have the $FABRIC_MOD fill in the transport ID.
4005 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4006 &format_code, &buf[off+4]);
4008 spin_lock(&pr_tmpl->registration_lock);
4009 atomic_dec_mb(&pr_reg->pr_res_holders);
4014 * Set the ADDITIONAL DESCRIPTOR LENGTH
4016 put_unaligned_be32(desc_len, &buf[off]);
4019 * Size of full desctipor header minus TransportID
4020 * containing $FABRIC_MOD specific) initiator device/port
4023 * See spc4r17 Section 6.13.5 Table 169
4025 add_desc_len = (24 + desc_len);
4028 add_len += add_desc_len;
4030 spin_unlock(&pr_tmpl->registration_lock);
4032 * Set ADDITIONAL_LENGTH
4034 put_unaligned_be32(add_len, &buf[4]);
4035 target_set_cmd_data_length(cmd, 8 + add_len);
4037 transport_kunmap_data_sg(cmd);
4043 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4048 * Following spc2r20 5.5.1 Reservations overview:
4050 * If a logical unit has been reserved by any RESERVE command and is
4051 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4052 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4053 * initiator or service action and shall terminate with a RESERVATION
4056 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4057 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4058 " SPC-2 reservation is held, returning"
4059 " RESERVATION_CONFLICT\n");
4060 return TCM_RESERVATION_CONFLICT;
4063 switch (cmd->t_task_cdb[1] & 0x1f) {
4065 ret = core_scsi3_pri_read_keys(cmd);
4067 case PRI_READ_RESERVATION:
4068 ret = core_scsi3_pri_read_reservation(cmd);
4070 case PRI_REPORT_CAPABILITIES:
4071 ret = core_scsi3_pri_report_capabilities(cmd);
4073 case PRI_READ_FULL_STATUS:
4074 ret = core_scsi3_pri_read_full_status(cmd);
4077 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4078 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4079 return TCM_INVALID_CDB_FIELD;
4083 target_complete_cmd(cmd, SAM_STAT_GOOD);
4088 target_check_reservation(struct se_cmd *cmd)
4090 struct se_device *dev = cmd->se_dev;
4095 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4097 if (!dev->dev_attrib.emulate_pr)
4099 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
4102 spin_lock(&dev->dev_reservation_lock);
4103 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4104 ret = target_scsi2_reservation_check(cmd);
4106 ret = target_scsi3_pr_reservation_check(cmd);
4107 spin_unlock(&dev->dev_reservation_lock);