1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * (c) Copyright 2009-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/vmalloc.h>
31 #include <linux/file.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <asm/unaligned.h>
36 #include <target/target_core_base.h>
37 #include <target/target_core_backend.h>
38 #include <target/target_core_fabric.h>
39 #include <target/target_core_configfs.h>
41 #include "target_core_internal.h"
42 #include "target_core_pr.h"
43 #include "target_core_ua.h"
46 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
48 struct pr_transport_id_holder {
50 struct t10_pr_registration *dest_pr_reg;
51 struct se_portal_group *dest_tpg;
52 struct se_node_acl *dest_node_acl;
53 struct se_dev_entry *dest_se_deve;
54 struct list_head dest_list;
57 void core_pr_dump_initiator_port(
58 struct t10_pr_registration *pr_reg,
62 if (!pr_reg->isid_present_at_reg)
65 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
70 REGISTER_AND_IGNORE_EXISTING_KEY,
79 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
80 struct t10_pr_registration *, int, int);
82 static int is_reservation_holder(
83 struct t10_pr_registration *pr_res_holder,
84 struct t10_pr_registration *pr_reg)
89 pr_res_type = pr_res_holder->pr_res_type;
91 return pr_res_holder == pr_reg ||
92 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
93 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
99 target_scsi2_reservation_check(struct se_cmd *cmd)
101 struct se_device *dev = cmd->se_dev;
102 struct se_session *sess = cmd->se_sess;
104 switch (cmd->t_task_cdb[0]) {
113 if (!dev->dev_reserved_node_acl || !sess)
116 if (dev->dev_reserved_node_acl != sess->se_node_acl)
117 return TCM_RESERVATION_CONFLICT;
119 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
120 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
121 return TCM_RESERVATION_CONFLICT;
127 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
128 struct se_node_acl *, struct se_session *);
129 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
131 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
133 struct se_session *se_sess = cmd->se_sess;
134 struct se_device *dev = cmd->se_dev;
135 struct t10_pr_registration *pr_reg;
136 struct t10_reservation *pr_tmpl = &dev->t10_pr;
139 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
143 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
146 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
147 * status, but no reservation shall be established and the
148 * persistent reservation shall not be changed, if the command
149 * is received from a) and b) below.
151 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
152 * status, but the persistent reservation shall not be released,
153 * if the command is received from a) and b)
155 * a) An I_T nexus that is a persistent reservation holder; or
156 * b) An I_T nexus that is registered if a registrants only or
157 * all registrants type persistent reservation is present.
159 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
160 * RELEASE(6) command, or RELEASE(10) command shall be processed
161 * as defined in SPC-2.
163 if (pr_reg->pr_res_holder) {
164 core_scsi3_put_pr_reg(pr_reg);
167 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
168 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
169 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
170 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
171 core_scsi3_put_pr_reg(pr_reg);
174 core_scsi3_put_pr_reg(pr_reg);
178 * Following spc2r20 5.5.1 Reservations overview:
180 * If a logical unit has executed a PERSISTENT RESERVE OUT
181 * command with the REGISTER or the REGISTER AND IGNORE
182 * EXISTING KEY service action and is still registered by any
183 * initiator, all RESERVE commands and all RELEASE commands
184 * regardless of initiator shall conflict and shall terminate
185 * with a RESERVATION CONFLICT status.
187 spin_lock(&pr_tmpl->registration_lock);
188 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
189 spin_unlock(&pr_tmpl->registration_lock);
193 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
194 " while active SPC-3 registrations exist,"
195 " returning RESERVATION_CONFLICT\n");
203 target_scsi2_reservation_release(struct se_cmd *cmd)
205 struct se_device *dev = cmd->se_dev;
206 struct se_session *sess = cmd->se_sess;
207 struct se_portal_group *tpg;
210 if (!sess || !sess->se_tpg)
212 rc = target_check_scsi2_reservation_conflict(cmd);
216 return TCM_RESERVATION_CONFLICT;
218 spin_lock(&dev->dev_reservation_lock);
219 if (!dev->dev_reserved_node_acl || !sess)
222 if (dev->dev_reserved_node_acl != sess->se_node_acl)
225 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
228 dev->dev_reserved_node_acl = NULL;
229 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
230 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
231 dev->dev_res_bin_isid = 0;
232 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
235 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
236 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
237 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
238 sess->se_node_acl->initiatorname);
241 spin_unlock(&dev->dev_reservation_lock);
243 target_complete_cmd(cmd, GOOD);
248 target_scsi2_reservation_reserve(struct se_cmd *cmd)
250 struct se_device *dev = cmd->se_dev;
251 struct se_session *sess = cmd->se_sess;
252 struct se_portal_group *tpg;
253 sense_reason_t ret = 0;
256 if ((cmd->t_task_cdb[1] & 0x01) &&
257 (cmd->t_task_cdb[1] & 0x02)) {
258 pr_err("LongIO and Obselete Bits set, returning"
259 " ILLEGAL_REQUEST\n");
260 return TCM_UNSUPPORTED_SCSI_OPCODE;
263 * This is currently the case for target_core_mod passthrough struct se_cmd
266 if (!sess || !sess->se_tpg)
268 rc = target_check_scsi2_reservation_conflict(cmd);
273 return TCM_RESERVATION_CONFLICT;
276 spin_lock(&dev->dev_reservation_lock);
277 if (dev->dev_reserved_node_acl &&
278 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
279 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
280 tpg->se_tpg_tfo->get_fabric_name());
281 pr_err("Original reserver LUN: %u %s\n",
282 cmd->se_lun->unpacked_lun,
283 dev->dev_reserved_node_acl->initiatorname);
284 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
285 " from %s \n", cmd->se_lun->unpacked_lun,
286 cmd->se_deve->mapped_lun,
287 sess->se_node_acl->initiatorname);
288 ret = TCM_RESERVATION_CONFLICT;
292 dev->dev_reserved_node_acl = sess->se_node_acl;
293 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
294 if (sess->sess_bin_isid != 0) {
295 dev->dev_res_bin_isid = sess->sess_bin_isid;
296 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
298 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
299 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
300 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
301 sess->se_node_acl->initiatorname);
304 spin_unlock(&dev->dev_reservation_lock);
307 target_complete_cmd(cmd, GOOD);
313 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
315 * This function is called by those initiator ports who are *NOT*
316 * the active PR reservation holder when a reservation is present.
318 static int core_scsi3_pr_seq_non_holder(
322 unsigned char *cdb = cmd->t_task_cdb;
323 struct se_dev_entry *se_deve;
324 struct se_session *se_sess = cmd->se_sess;
325 int other_cdb = 0, ignore_reg;
326 int registered_nexus = 0, ret = 1; /* Conflict by default */
327 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
328 int we = 0; /* Write Exclusive */
329 int legacy = 0; /* Act like a legacy device and return
330 * RESERVATION CONFLICT on some CDBs */
332 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
334 * Determine if the registration should be ignored due to
335 * non-matching ISIDs in target_scsi3_pr_reservation_check().
337 ignore_reg = (pr_reg_type & 0x80000000);
339 pr_reg_type &= ~0x80000000;
341 switch (pr_reg_type) {
342 case PR_TYPE_WRITE_EXCLUSIVE:
344 case PR_TYPE_EXCLUSIVE_ACCESS:
346 * Some commands are only allowed for the persistent reservation
349 if ((se_deve->def_pr_registered) && !(ignore_reg))
350 registered_nexus = 1;
352 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
354 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
356 * Some commands are only allowed for registered I_T Nexuses.
359 if ((se_deve->def_pr_registered) && !(ignore_reg))
360 registered_nexus = 1;
362 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
364 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
366 * Each registered I_T Nexus is a reservation holder.
369 if ((se_deve->def_pr_registered) && !(ignore_reg))
370 registered_nexus = 1;
376 * Referenced from spc4r17 table 45 for *NON* PR holder access
379 case SECURITY_PROTOCOL_IN:
380 if (registered_nexus)
388 case RECEIVE_DIAGNOSTIC:
393 if (registered_nexus) {
397 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
399 case PERSISTENT_RESERVE_OUT:
401 * This follows PERSISTENT_RESERVE_OUT service actions that
402 * are allowed in the presence of various reservations.
403 * See spc4r17, table 46
405 switch (cdb[1] & 0x1f) {
408 case PRO_PREEMPT_AND_ABORT:
409 ret = (registered_nexus) ? 0 : 1;
412 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
415 case PRO_REGISTER_AND_MOVE:
420 ret = (registered_nexus) ? 0 : 1;
423 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
424 " action: 0x%02x\n", cdb[1] & 0x1f);
430 /* Handled by CRH=1 in target_scsi2_reservation_release() */
435 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
438 case TEST_UNIT_READY:
439 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
442 switch (cdb[1] & 0x1f) {
443 case MI_MANAGEMENT_PROTOCOL_IN:
444 if (registered_nexus) {
448 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
450 case MI_REPORT_SUPPORTED_OPERATION_CODES:
451 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
456 if (registered_nexus) {
460 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
462 case MI_REPORT_ALIASES:
463 case MI_REPORT_IDENTIFYING_INFORMATION:
464 case MI_REPORT_PRIORITY:
465 case MI_REPORT_TARGET_PGS:
466 case MI_REPORT_TIMESTAMP:
467 ret = 0; /* Allowed */
470 pr_err("Unknown MI Service Action: 0x%02x\n",
475 case ACCESS_CONTROL_IN:
476 case ACCESS_CONTROL_OUT:
479 case SERVICE_ACTION_IN_12:
482 case PERSISTENT_RESERVE_IN:
483 ret = 0; /*/ Allowed CDBs */
490 * Case where the CDB is explicitly allowed in the above switch
493 if (!ret && !other_cdb) {
494 pr_debug("Allowing explicit CDB: 0x%02x for %s"
495 " reservation holder\n", cdb[0],
496 core_scsi3_pr_dump_type(pr_reg_type));
501 * Check if write exclusive initiator ports *NOT* holding the
502 * WRITE_EXCLUSIVE_* reservation.
504 if (we && !registered_nexus) {
505 if (cmd->data_direction == DMA_TO_DEVICE) {
507 * Conflict for write exclusive
509 pr_debug("%s Conflict for unregistered nexus"
510 " %s CDB: 0x%02x to %s reservation\n",
511 transport_dump_cmd_direction(cmd),
512 se_sess->se_node_acl->initiatorname, cdb[0],
513 core_scsi3_pr_dump_type(pr_reg_type));
517 * Allow non WRITE CDBs for all Write Exclusive
518 * PR TYPEs to pass for registered and
519 * non-registered_nexuxes NOT holding the reservation.
521 * We only make noise for the unregisterd nexuses,
522 * as we expect registered non-reservation holding
523 * nexuses to issue CDBs.
526 if (!registered_nexus) {
527 pr_debug("Allowing implicit CDB: 0x%02x"
528 " for %s reservation on unregistered"
530 core_scsi3_pr_dump_type(pr_reg_type));
535 } else if ((reg_only) || (all_reg)) {
536 if (registered_nexus) {
538 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
539 * allow commands from registered nexuses.
542 pr_debug("Allowing implicit CDB: 0x%02x for %s"
543 " reservation\n", cdb[0],
544 core_scsi3_pr_dump_type(pr_reg_type));
548 } else if (we && registered_nexus) {
550 * Reads are allowed for Write Exclusive locks
551 * from all registrants.
553 if (cmd->data_direction == DMA_FROM_DEVICE) {
554 pr_debug("Allowing READ CDB: 0x%02x for %s"
555 " reservation\n", cdb[0],
556 core_scsi3_pr_dump_type(pr_reg_type));
561 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
562 " for %s reservation\n", transport_dump_cmd_direction(cmd),
563 (registered_nexus) ? "" : "un",
564 se_sess->se_node_acl->initiatorname, cdb[0],
565 core_scsi3_pr_dump_type(pr_reg_type));
567 return 1; /* Conflict by default */
570 static sense_reason_t
571 target_scsi3_pr_reservation_check(struct se_cmd *cmd)
573 struct se_device *dev = cmd->se_dev;
574 struct se_session *sess = cmd->se_sess;
577 if (!dev->dev_pr_res_holder)
580 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
581 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
582 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
583 goto check_nonholder;
585 if (dev->dev_pr_res_holder->isid_present_at_reg) {
586 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
587 sess->sess_bin_isid) {
588 pr_reg_type |= 0x80000000;
589 goto check_nonholder;
596 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type))
597 return TCM_RESERVATION_CONFLICT;
601 static u32 core_scsi3_pr_generation(struct se_device *dev)
606 * PRGeneration field shall contain the value of a 32-bit wrapping
607 * counter mainted by the device server.
609 * Note that this is done regardless of Active Persist across
610 * Target PowerLoss (APTPL)
612 * See spc4r17 section 6.3.12 READ_KEYS service action
614 spin_lock(&dev->dev_reservation_lock);
615 prg = dev->t10_pr.pr_generation++;
616 spin_unlock(&dev->dev_reservation_lock);
621 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
622 struct se_device *dev,
623 struct se_node_acl *nacl,
624 struct se_dev_entry *deve,
630 struct t10_pr_registration *pr_reg;
632 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
634 pr_err("Unable to allocate struct t10_pr_registration\n");
638 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
639 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
640 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
641 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
642 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
643 atomic_set(&pr_reg->pr_res_holders, 0);
644 pr_reg->pr_reg_nacl = nacl;
645 pr_reg->pr_reg_deve = deve;
646 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
647 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
648 pr_reg->pr_res_key = sa_res_key;
649 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
650 pr_reg->pr_reg_aptpl = aptpl;
651 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
653 * If an ISID value for this SCSI Initiator Port exists,
654 * save it to the registration now.
657 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
658 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
659 pr_reg->isid_present_at_reg = 1;
665 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
666 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
669 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
672 static struct t10_pr_registration *__core_scsi3_alloc_registration(
673 struct se_device *dev,
674 struct se_node_acl *nacl,
675 struct se_dev_entry *deve,
681 struct se_dev_entry *deve_tmp;
682 struct se_node_acl *nacl_tmp;
683 struct se_port *port, *port_tmp;
684 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
685 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
688 * Create a registration for the I_T Nexus upon which the
689 * PROUT REGISTER was received.
691 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
692 sa_res_key, all_tg_pt, aptpl);
696 * Return pointer to pr_reg for ALL_TG_PT=0
701 * Create list of matching SCSI Initiator Port registrations
704 spin_lock(&dev->se_port_lock);
705 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
706 atomic_inc_mb(&port->sep_tg_pt_ref_cnt);
707 spin_unlock(&dev->se_port_lock);
709 spin_lock_bh(&port->sep_alua_lock);
710 list_for_each_entry(deve_tmp, &port->sep_alua_list,
713 * This pointer will be NULL for demo mode MappedLUNs
714 * that have not been make explicit via a ConfigFS
715 * MappedLUN group for the SCSI Initiator Node ACL.
717 if (!deve_tmp->se_lun_acl)
720 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
722 * Skip the matching struct se_node_acl that is allocated
725 if (nacl == nacl_tmp)
728 * Only perform PR registrations for target ports on
729 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
732 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
735 * Look for a matching Initiator Node ACL in ASCII format
737 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
740 atomic_inc_mb(&deve_tmp->pr_ref_count);
741 spin_unlock_bh(&port->sep_alua_lock);
743 * Grab a configfs group dependency that is released
744 * for the exception path at label out: below, or upon
745 * completion of adding ALL_TG_PT=1 registrations in
746 * __core_scsi3_add_registration()
748 ret = core_scsi3_lunacl_depend_item(deve_tmp);
750 pr_err("core_scsi3_lunacl_depend"
752 atomic_dec_mb(&port->sep_tg_pt_ref_cnt);
753 atomic_dec_mb(&deve_tmp->pr_ref_count);
757 * Located a matching SCSI Initiator Port on a different
758 * port, allocate the pr_reg_atp and attach it to the
759 * pr_reg->pr_reg_atp_list that will be processed once
760 * the original *pr_reg is processed in
761 * __core_scsi3_add_registration()
763 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
764 nacl_tmp, deve_tmp, NULL,
765 sa_res_key, all_tg_pt, aptpl);
767 atomic_dec_mb(&port->sep_tg_pt_ref_cnt);
768 atomic_dec_mb(&deve_tmp->pr_ref_count);
769 core_scsi3_lunacl_undepend_item(deve_tmp);
773 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
774 &pr_reg->pr_reg_atp_list);
775 spin_lock_bh(&port->sep_alua_lock);
777 spin_unlock_bh(&port->sep_alua_lock);
779 spin_lock(&dev->se_port_lock);
780 atomic_dec_mb(&port->sep_tg_pt_ref_cnt);
782 spin_unlock(&dev->se_port_lock);
786 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
787 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
788 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
789 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
790 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
792 kmem_cache_free(t10_pr_reg_cache, pr_reg);
796 int core_scsi3_alloc_aptpl_registration(
797 struct t10_reservation *pr_tmpl,
799 unsigned char *i_port,
802 unsigned char *t_port,
809 struct t10_pr_registration *pr_reg;
811 if (!i_port || !t_port || !sa_res_key) {
812 pr_err("Illegal parameters for APTPL registration\n");
816 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
818 pr_err("Unable to allocate struct t10_pr_registration\n");
822 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
823 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
824 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
825 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
826 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
827 atomic_set(&pr_reg->pr_res_holders, 0);
828 pr_reg->pr_reg_nacl = NULL;
829 pr_reg->pr_reg_deve = NULL;
830 pr_reg->pr_res_mapped_lun = mapped_lun;
831 pr_reg->pr_aptpl_target_lun = target_lun;
832 pr_reg->pr_res_key = sa_res_key;
833 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
834 pr_reg->pr_reg_aptpl = 1;
835 pr_reg->pr_reg_tg_pt_lun = NULL;
836 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
837 pr_reg->pr_res_type = type;
839 * If an ISID value had been saved in APTPL metadata for this
840 * SCSI Initiator Port, restore it now.
843 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
844 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
845 pr_reg->isid_present_at_reg = 1;
848 * Copy the i_port and t_port information from caller.
850 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
851 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
852 pr_reg->pr_reg_tpgt = tpgt;
854 * Set pr_res_holder from caller, the pr_reg who is the reservation
855 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
856 * the Initiator Node LUN ACL from the fabric module is created for
859 pr_reg->pr_res_holder = res_holder;
861 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
862 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
863 " metadata\n", (res_holder) ? "+reservation" : "");
867 static void core_scsi3_aptpl_reserve(
868 struct se_device *dev,
869 struct se_portal_group *tpg,
870 struct se_node_acl *node_acl,
871 struct t10_pr_registration *pr_reg)
873 char i_buf[PR_REG_ISID_ID_LEN];
875 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
876 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
878 spin_lock(&dev->dev_reservation_lock);
879 dev->dev_pr_res_holder = pr_reg;
880 spin_unlock(&dev->dev_reservation_lock);
882 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
883 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
884 tpg->se_tpg_tfo->get_fabric_name(),
885 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
886 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
887 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
888 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
892 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
893 struct t10_pr_registration *, enum register_type, int);
895 static int __core_scsi3_check_aptpl_registration(
896 struct se_device *dev,
897 struct se_portal_group *tpg,
900 struct se_node_acl *nacl,
901 struct se_dev_entry *deve)
903 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
904 struct t10_reservation *pr_tmpl = &dev->t10_pr;
905 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
906 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
909 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
910 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
912 * Copy Initiator Port information from struct se_node_acl
914 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
915 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
916 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
917 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
919 * Look for the matching registrations+reservation from those
920 * created from APTPL metadata. Note that multiple registrations
921 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
924 spin_lock(&pr_tmpl->aptpl_reg_lock);
925 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
928 if (!strcmp(pr_reg->pr_iport, i_port) &&
929 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
930 !(strcmp(pr_reg->pr_tport, t_port)) &&
931 (pr_reg->pr_reg_tpgt == tpgt) &&
932 (pr_reg->pr_aptpl_target_lun == target_lun)) {
934 pr_reg->pr_reg_nacl = nacl;
935 pr_reg->pr_reg_deve = deve;
936 pr_reg->pr_reg_tg_pt_lun = lun;
938 list_del(&pr_reg->pr_reg_aptpl_list);
939 spin_unlock(&pr_tmpl->aptpl_reg_lock);
941 * At this point all of the pointers in *pr_reg will
942 * be setup, so go ahead and add the registration.
945 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
947 * If this registration is the reservation holder,
948 * make that happen now..
950 if (pr_reg->pr_res_holder)
951 core_scsi3_aptpl_reserve(dev, tpg,
954 * Reenable pr_aptpl_active to accept new metadata
955 * updates once the SCSI device is active again..
957 spin_lock(&pr_tmpl->aptpl_reg_lock);
958 pr_tmpl->pr_aptpl_active = 1;
961 spin_unlock(&pr_tmpl->aptpl_reg_lock);
966 int core_scsi3_check_aptpl_registration(
967 struct se_device *dev,
968 struct se_portal_group *tpg,
970 struct se_node_acl *nacl,
973 struct se_dev_entry *deve = nacl->device_list[mapped_lun];
975 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
978 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
979 lun->unpacked_lun, nacl, deve);
982 static void __core_scsi3_dump_registration(
983 const struct target_core_fabric_ops *tfo,
984 struct se_device *dev,
985 struct se_node_acl *nacl,
986 struct t10_pr_registration *pr_reg,
987 enum register_type register_type)
989 struct se_portal_group *se_tpg = nacl->se_tpg;
990 char i_buf[PR_REG_ISID_ID_LEN];
992 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
993 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
995 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
996 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
997 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
998 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1000 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1001 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1002 tfo->tpg_get_tag(se_tpg));
1003 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1004 " Port(s)\n", tfo->get_fabric_name(),
1005 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1006 dev->transport->name);
1007 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1008 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1009 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1010 pr_reg->pr_reg_aptpl);
1014 * this function can be called with struct se_device->dev_reservation_lock
1015 * when register_move = 1
1017 static void __core_scsi3_add_registration(
1018 struct se_device *dev,
1019 struct se_node_acl *nacl,
1020 struct t10_pr_registration *pr_reg,
1021 enum register_type register_type,
1024 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1025 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1026 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1029 * Increment PRgeneration counter for struct se_device upon a successful
1030 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1032 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1033 * action, the struct se_device->dev_reservation_lock will already be held,
1034 * so we do not call core_scsi3_pr_generation() which grabs the lock
1037 pr_reg->pr_res_generation = (register_move) ?
1038 dev->t10_pr.pr_generation++ :
1039 core_scsi3_pr_generation(dev);
1041 spin_lock(&pr_tmpl->registration_lock);
1042 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1043 pr_reg->pr_reg_deve->def_pr_registered = 1;
1045 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1046 spin_unlock(&pr_tmpl->registration_lock);
1048 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1050 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1053 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1054 * allocated in __core_scsi3_alloc_registration()
1056 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1057 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1058 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1060 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1062 spin_lock(&pr_tmpl->registration_lock);
1063 list_add_tail(&pr_reg_tmp->pr_reg_list,
1064 &pr_tmpl->registration_list);
1065 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1067 __core_scsi3_dump_registration(tfo, dev,
1068 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1070 spin_unlock(&pr_tmpl->registration_lock);
1072 * Drop configfs group dependency reference from
1073 * __core_scsi3_alloc_registration()
1075 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1079 static int core_scsi3_alloc_registration(
1080 struct se_device *dev,
1081 struct se_node_acl *nacl,
1082 struct se_dev_entry *deve,
1083 unsigned char *isid,
1087 enum register_type register_type,
1090 struct t10_pr_registration *pr_reg;
1092 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1093 sa_res_key, all_tg_pt, aptpl);
1097 __core_scsi3_add_registration(dev, nacl, pr_reg,
1098 register_type, register_move);
1102 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1103 struct se_device *dev,
1104 struct se_node_acl *nacl,
1105 unsigned char *isid)
1107 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1108 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1109 struct se_portal_group *tpg;
1111 spin_lock(&pr_tmpl->registration_lock);
1112 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1113 &pr_tmpl->registration_list, pr_reg_list) {
1115 * First look for a matching struct se_node_acl
1117 if (pr_reg->pr_reg_nacl != nacl)
1120 tpg = pr_reg->pr_reg_nacl->se_tpg;
1122 * If this registration does NOT contain a fabric provided
1123 * ISID, then we have found a match.
1125 if (!pr_reg->isid_present_at_reg) {
1127 * Determine if this SCSI device server requires that
1128 * SCSI Intiatior TransportID w/ ISIDs is enforced
1129 * for fabric modules (iSCSI) requiring them.
1131 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1132 if (dev->dev_attrib.enforce_pr_isids)
1135 atomic_inc_mb(&pr_reg->pr_res_holders);
1136 spin_unlock(&pr_tmpl->registration_lock);
1140 * If the *pr_reg contains a fabric defined ISID for multi-value
1141 * SCSI Initiator Port TransportIDs, then we expect a valid
1142 * matching ISID to be provided by the local SCSI Initiator Port.
1146 if (strcmp(isid, pr_reg->pr_reg_isid))
1149 atomic_inc_mb(&pr_reg->pr_res_holders);
1150 spin_unlock(&pr_tmpl->registration_lock);
1153 spin_unlock(&pr_tmpl->registration_lock);
1158 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1159 struct se_device *dev,
1160 struct se_node_acl *nacl,
1161 struct se_session *sess)
1163 struct se_portal_group *tpg = nacl->se_tpg;
1164 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1166 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1167 memset(&buf[0], 0, PR_REG_ISID_LEN);
1168 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1173 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1176 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1178 atomic_dec_mb(&pr_reg->pr_res_holders);
1181 static int core_scsi3_check_implicit_release(
1182 struct se_device *dev,
1183 struct t10_pr_registration *pr_reg)
1185 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1186 struct t10_pr_registration *pr_res_holder;
1189 spin_lock(&dev->dev_reservation_lock);
1190 pr_res_holder = dev->dev_pr_res_holder;
1191 if (!pr_res_holder) {
1192 spin_unlock(&dev->dev_reservation_lock);
1195 if (pr_res_holder == pr_reg) {
1197 * Perform an implicit RELEASE if the registration that
1198 * is being released is holding the reservation.
1200 * From spc4r17, section 5.7.11.1:
1202 * e) If the I_T nexus is the persistent reservation holder
1203 * and the persistent reservation is not an all registrants
1204 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1205 * service action or REGISTER AND IGNORE EXISTING KEY
1206 * service action with the SERVICE ACTION RESERVATION KEY
1207 * field set to zero (see 5.7.11.3).
1209 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1212 * For 'All Registrants' reservation types, all existing
1213 * registrations are still processed as reservation holders
1214 * in core_scsi3_pr_seq_non_holder() after the initial
1215 * reservation holder is implicitly released here.
1217 } else if (pr_reg->pr_reg_all_tg_pt &&
1218 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1219 pr_reg->pr_reg_nacl->initiatorname)) &&
1220 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1221 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1222 " UNREGISTER while existing reservation with matching"
1223 " key 0x%016Lx is present from another SCSI Initiator"
1224 " Port\n", pr_reg->pr_res_key);
1227 spin_unlock(&dev->dev_reservation_lock);
1233 * Called with struct t10_reservation->registration_lock held.
1235 static void __core_scsi3_free_registration(
1236 struct se_device *dev,
1237 struct t10_pr_registration *pr_reg,
1238 struct list_head *preempt_and_abort_list,
1240 __releases(&pr_tmpl->registration_lock)
1241 __acquires(&pr_tmpl->registration_lock)
1243 const struct target_core_fabric_ops *tfo =
1244 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1245 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1246 char i_buf[PR_REG_ISID_ID_LEN];
1248 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1249 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1251 pr_reg->pr_reg_deve->def_pr_registered = 0;
1252 pr_reg->pr_reg_deve->pr_res_key = 0;
1253 if (!list_empty(&pr_reg->pr_reg_list))
1254 list_del(&pr_reg->pr_reg_list);
1256 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1257 * so call core_scsi3_put_pr_reg() to decrement our reference.
1260 core_scsi3_put_pr_reg(pr_reg);
1262 * Wait until all reference from any other I_T nexuses for this
1263 * *pr_reg have been released. Because list_del() is called above,
1264 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1265 * count back to zero, and we release *pr_reg.
1267 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1268 spin_unlock(&pr_tmpl->registration_lock);
1269 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1270 tfo->get_fabric_name());
1272 spin_lock(&pr_tmpl->registration_lock);
1275 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1276 " Node: %s%s\n", tfo->get_fabric_name(),
1277 pr_reg->pr_reg_nacl->initiatorname,
1279 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1280 " Port(s)\n", tfo->get_fabric_name(),
1281 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1282 dev->transport->name);
1283 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1284 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1285 pr_reg->pr_res_generation);
1287 if (!preempt_and_abort_list) {
1288 pr_reg->pr_reg_deve = NULL;
1289 pr_reg->pr_reg_nacl = NULL;
1290 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1294 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1295 * are released once the ABORT_TASK_SET has completed..
1297 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1300 void core_scsi3_free_pr_reg_from_nacl(
1301 struct se_device *dev,
1302 struct se_node_acl *nacl)
1304 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1305 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1306 bool free_reg = false;
1308 * If the passed se_node_acl matches the reservation holder,
1309 * release the reservation.
1311 spin_lock(&dev->dev_reservation_lock);
1312 pr_res_holder = dev->dev_pr_res_holder;
1313 if ((pr_res_holder != NULL) &&
1314 (pr_res_holder->pr_reg_nacl == nacl)) {
1315 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1318 spin_unlock(&dev->dev_reservation_lock);
1320 * Release any registration associated with the struct se_node_acl.
1322 spin_lock(&pr_tmpl->registration_lock);
1323 if (pr_res_holder && free_reg)
1324 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1326 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1327 &pr_tmpl->registration_list, pr_reg_list) {
1329 if (pr_reg->pr_reg_nacl != nacl)
1332 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1334 spin_unlock(&pr_tmpl->registration_lock);
1337 void core_scsi3_free_all_registrations(
1338 struct se_device *dev)
1340 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1341 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1343 spin_lock(&dev->dev_reservation_lock);
1344 pr_res_holder = dev->dev_pr_res_holder;
1345 if (pr_res_holder != NULL) {
1346 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1347 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1348 pr_res_holder, 0, 0);
1350 spin_unlock(&dev->dev_reservation_lock);
1352 spin_lock(&pr_tmpl->registration_lock);
1353 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1354 &pr_tmpl->registration_list, pr_reg_list) {
1356 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1358 spin_unlock(&pr_tmpl->registration_lock);
1360 spin_lock(&pr_tmpl->aptpl_reg_lock);
1361 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1362 pr_reg_aptpl_list) {
1363 list_del(&pr_reg->pr_reg_aptpl_list);
1364 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1366 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1369 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1371 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1372 &tpg->tpg_group.cg_item);
1375 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1377 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1378 &tpg->tpg_group.cg_item);
1380 atomic_dec_mb(&tpg->tpg_pr_ref_count);
1383 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1385 struct se_portal_group *tpg = nacl->se_tpg;
1387 if (nacl->dynamic_node_acl)
1390 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1391 &nacl->acl_group.cg_item);
1394 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1396 struct se_portal_group *tpg = nacl->se_tpg;
1398 if (nacl->dynamic_node_acl) {
1399 atomic_dec_mb(&nacl->acl_pr_ref_count);
1403 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1404 &nacl->acl_group.cg_item);
1406 atomic_dec_mb(&nacl->acl_pr_ref_count);
1409 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1411 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1412 struct se_node_acl *nacl;
1413 struct se_portal_group *tpg;
1415 * For nacl->dynamic_node_acl=1
1420 nacl = lun_acl->se_lun_nacl;
1423 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1424 &lun_acl->se_lun_group.cg_item);
1427 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1429 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1430 struct se_node_acl *nacl;
1431 struct se_portal_group *tpg;
1433 * For nacl->dynamic_node_acl=1
1436 atomic_dec_mb(&se_deve->pr_ref_count);
1439 nacl = lun_acl->se_lun_nacl;
1442 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1443 &lun_acl->se_lun_group.cg_item);
1445 atomic_dec_mb(&se_deve->pr_ref_count);
1448 static sense_reason_t
1449 core_scsi3_decode_spec_i_port(
1451 struct se_portal_group *tpg,
1452 unsigned char *l_isid,
1457 struct se_device *dev = cmd->se_dev;
1458 struct se_port *tmp_port;
1459 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1460 struct se_session *se_sess = cmd->se_sess;
1461 struct se_node_acl *dest_node_acl = NULL;
1462 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1463 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1464 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1465 LIST_HEAD(tid_dest_list);
1466 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1467 const struct target_core_fabric_ops *tmp_tf_ops;
1469 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1470 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1472 u32 tpdl, tid_len = 0;
1473 int dest_local_nexus;
1476 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1478 * Allocate a struct pr_transport_id_holder and setup the
1479 * local_node_acl and local_se_deve pointers and add to
1480 * struct list_head tid_dest_list for add registration
1481 * processing in the loop of tid_dest_list below.
1483 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1485 pr_err("Unable to allocate tidh_new\n");
1486 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1488 INIT_LIST_HEAD(&tidh_new->dest_list);
1489 tidh_new->dest_tpg = tpg;
1490 tidh_new->dest_node_acl = se_sess->se_node_acl;
1491 tidh_new->dest_se_deve = local_se_deve;
1493 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1494 se_sess->se_node_acl, local_se_deve, l_isid,
1495 sa_res_key, all_tg_pt, aptpl);
1496 if (!local_pr_reg) {
1498 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1500 tidh_new->dest_pr_reg = local_pr_reg;
1502 * The local I_T nexus does not hold any configfs dependances,
1503 * so we set tid_h->dest_local_nexus=1 to prevent the
1504 * configfs_undepend_item() calls in the tid_dest_list loops below.
1506 tidh_new->dest_local_nexus = 1;
1507 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1509 if (cmd->data_length < 28) {
1510 pr_warn("SPC-PR: Received PR OUT parameter list"
1511 " length too small: %u\n", cmd->data_length);
1512 ret = TCM_INVALID_PARAMETER_LIST;
1516 buf = transport_kmap_data_sg(cmd);
1518 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1523 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1524 * first extract TransportID Parameter Data Length, and make sure
1525 * the value matches up to the SCSI expected data transfer length.
1527 tpdl = (buf[24] & 0xff) << 24;
1528 tpdl |= (buf[25] & 0xff) << 16;
1529 tpdl |= (buf[26] & 0xff) << 8;
1530 tpdl |= buf[27] & 0xff;
1532 if ((tpdl + 28) != cmd->data_length) {
1533 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1534 " does not equal CDB data_length: %u\n", tpdl,
1536 ret = TCM_INVALID_PARAMETER_LIST;
1540 * Start processing the received transport IDs using the
1541 * receiving I_T Nexus portal's fabric dependent methods to
1542 * obtain the SCSI Initiator Port/Device Identifiers.
1547 proto_ident = (ptr[0] & 0x0f);
1550 spin_lock(&dev->se_port_lock);
1551 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1552 tmp_tpg = tmp_port->sep_tpg;
1555 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1558 if (!tmp_tf_ops->get_fabric_proto_ident ||
1559 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1562 * Look for the matching proto_ident provided by
1563 * the received TransportID
1565 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1566 if (tmp_proto_ident != proto_ident)
1568 dest_rtpi = tmp_port->sep_rtpi;
1570 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1571 tmp_tpg, (const char *)ptr, &tid_len,
1576 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1577 spin_unlock(&dev->se_port_lock);
1579 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1580 pr_err(" core_scsi3_tpg_depend_item()"
1582 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1583 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1587 * Locate the destination initiator ACL to be registered
1588 * from the decoded fabric module specific TransportID
1591 spin_lock_irq(&tmp_tpg->acl_node_lock);
1592 dest_node_acl = __core_tpg_get_initiator_node_acl(
1595 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1596 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1598 if (!dest_node_acl) {
1599 core_scsi3_tpg_undepend_item(tmp_tpg);
1600 spin_lock(&dev->se_port_lock);
1604 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1605 pr_err("configfs_depend_item() failed"
1606 " for dest_node_acl->acl_group\n");
1607 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1608 core_scsi3_tpg_undepend_item(tmp_tpg);
1609 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1614 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1615 " %s Port RTPI: %hu\n",
1616 dest_tpg->se_tpg_tfo->get_fabric_name(),
1617 dest_node_acl->initiatorname, dest_rtpi);
1619 spin_lock(&dev->se_port_lock);
1622 spin_unlock(&dev->se_port_lock);
1625 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1627 ret = TCM_INVALID_PARAMETER_LIST;
1631 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1632 " tid_len: %d for %s + %s\n",
1633 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1634 tpdl, tid_len, i_str, iport_ptr);
1636 if (tid_len > tpdl) {
1637 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1638 " %u for Transport ID: %s\n", tid_len, ptr);
1639 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1640 core_scsi3_tpg_undepend_item(dest_tpg);
1641 ret = TCM_INVALID_PARAMETER_LIST;
1645 * Locate the desintation struct se_dev_entry pointer for matching
1646 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1649 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1651 if (!dest_se_deve) {
1652 pr_err("Unable to locate %s dest_se_deve"
1653 " from destination RTPI: %hu\n",
1654 dest_tpg->se_tpg_tfo->get_fabric_name(),
1657 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1658 core_scsi3_tpg_undepend_item(dest_tpg);
1659 ret = TCM_INVALID_PARAMETER_LIST;
1663 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1664 pr_err("core_scsi3_lunacl_depend_item()"
1666 atomic_dec_mb(&dest_se_deve->pr_ref_count);
1667 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1668 core_scsi3_tpg_undepend_item(dest_tpg);
1669 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1673 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1674 " dest_se_deve mapped_lun: %u\n",
1675 dest_tpg->se_tpg_tfo->get_fabric_name(),
1676 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1679 * Skip any TransportIDs that already have a registration for
1682 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1685 core_scsi3_put_pr_reg(pr_reg_e);
1686 core_scsi3_lunacl_undepend_item(dest_se_deve);
1687 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1688 core_scsi3_tpg_undepend_item(dest_tpg);
1695 * Allocate a struct pr_transport_id_holder and setup
1696 * the dest_node_acl and dest_se_deve pointers for the
1699 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1702 pr_err("Unable to allocate tidh_new\n");
1703 core_scsi3_lunacl_undepend_item(dest_se_deve);
1704 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1705 core_scsi3_tpg_undepend_item(dest_tpg);
1706 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1709 INIT_LIST_HEAD(&tidh_new->dest_list);
1710 tidh_new->dest_tpg = dest_tpg;
1711 tidh_new->dest_node_acl = dest_node_acl;
1712 tidh_new->dest_se_deve = dest_se_deve;
1715 * Allocate, but do NOT add the registration for the
1716 * TransportID referenced SCSI Initiator port. This
1717 * done because of the following from spc4r17 in section
1718 * 6.14.3 wrt SPEC_I_PT:
1720 * "If a registration fails for any initiator port (e.g., if th
1721 * logical unit does not have enough resources available to
1722 * hold the registration information), no registrations shall be
1723 * made, and the command shall be terminated with
1724 * CHECK CONDITION status."
1726 * That means we call __core_scsi3_alloc_registration() here,
1727 * and then call __core_scsi3_add_registration() in the
1728 * 2nd loop which will never fail.
1730 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1731 dest_node_acl, dest_se_deve, iport_ptr,
1732 sa_res_key, all_tg_pt, aptpl);
1734 core_scsi3_lunacl_undepend_item(dest_se_deve);
1735 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1736 core_scsi3_tpg_undepend_item(dest_tpg);
1738 ret = TCM_INVALID_PARAMETER_LIST;
1741 tidh_new->dest_pr_reg = dest_pr_reg;
1742 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1750 transport_kunmap_data_sg(cmd);
1753 * Go ahead and create a registrations from tid_dest_list for the
1754 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1757 * The SA Reservation Key from the PROUT is set for the
1758 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1759 * means that the TransportID Initiator port will be
1760 * registered on all of the target ports in the SCSI target device
1761 * ALL_TG_PT=0 means the registration will only be for the
1762 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1765 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1766 dest_tpg = tidh->dest_tpg;
1767 dest_node_acl = tidh->dest_node_acl;
1768 dest_se_deve = tidh->dest_se_deve;
1769 dest_pr_reg = tidh->dest_pr_reg;
1770 dest_local_nexus = tidh->dest_local_nexus;
1772 list_del(&tidh->dest_list);
1775 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1776 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1778 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1781 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1782 " registered Transport ID for Node: %s%s Mapped LUN:"
1783 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1784 dest_node_acl->initiatorname, i_buf, dest_se_deve->mapped_lun);
1786 if (dest_local_nexus)
1789 core_scsi3_lunacl_undepend_item(dest_se_deve);
1790 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1791 core_scsi3_tpg_undepend_item(dest_tpg);
1796 transport_kunmap_data_sg(cmd);
1799 * For the failure case, release everything from tid_dest_list
1800 * including *dest_pr_reg and the configfs dependances..
1802 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1803 dest_tpg = tidh->dest_tpg;
1804 dest_node_acl = tidh->dest_node_acl;
1805 dest_se_deve = tidh->dest_se_deve;
1806 dest_pr_reg = tidh->dest_pr_reg;
1807 dest_local_nexus = tidh->dest_local_nexus;
1809 list_del(&tidh->dest_list);
1812 * Release any extra ALL_TG_PT=1 registrations for
1813 * the SPEC_I_PT=1 case.
1815 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1816 &dest_pr_reg->pr_reg_atp_list,
1817 pr_reg_atp_mem_list) {
1818 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1819 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1820 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1823 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1825 if (dest_local_nexus)
1828 core_scsi3_lunacl_undepend_item(dest_se_deve);
1829 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1830 core_scsi3_tpg_undepend_item(dest_tpg);
1835 static int core_scsi3_update_aptpl_buf(
1836 struct se_device *dev,
1838 u32 pr_aptpl_buf_len)
1841 struct se_portal_group *tpg;
1842 struct t10_pr_registration *pr_reg;
1843 unsigned char tmp[512], isid_buf[32];
1848 spin_lock(&dev->dev_reservation_lock);
1849 spin_lock(&dev->t10_pr.registration_lock);
1851 * Walk the registration list..
1853 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1858 tpg = pr_reg->pr_reg_nacl->se_tpg;
1859 lun = pr_reg->pr_reg_tg_pt_lun;
1861 * Write out any ISID value to APTPL metadata that was included
1862 * in the original registration.
1864 if (pr_reg->isid_present_at_reg)
1865 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1866 pr_reg->pr_reg_isid);
1868 * Include special metadata if the pr_reg matches the
1869 * reservation holder.
1871 if (dev->dev_pr_res_holder == pr_reg) {
1872 snprintf(tmp, 512, "PR_REG_START: %d"
1873 "\ninitiator_fabric=%s\n"
1874 "initiator_node=%s\n%s"
1876 "res_holder=1\nres_type=%02x\n"
1877 "res_scope=%02x\nres_all_tg_pt=%d\n"
1878 "mapped_lun=%u\n", reg_count,
1879 tpg->se_tpg_tfo->get_fabric_name(),
1880 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1881 pr_reg->pr_res_key, pr_reg->pr_res_type,
1882 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1883 pr_reg->pr_res_mapped_lun);
1885 snprintf(tmp, 512, "PR_REG_START: %d\n"
1886 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1887 "sa_res_key=%llu\nres_holder=0\n"
1888 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1889 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1890 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1891 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1892 pr_reg->pr_res_mapped_lun);
1895 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1896 pr_err("Unable to update renaming APTPL metadata,"
1897 " reallocating larger buffer\n");
1901 len += sprintf(buf+len, "%s", tmp);
1904 * Include information about the associated SCSI target port.
1906 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1907 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1908 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1909 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1910 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1911 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
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);
1924 len += sprintf(buf+len, "No Registrations or Reservations");
1927 spin_unlock(&dev->t10_pr.registration_lock);
1928 spin_unlock(&dev->dev_reservation_lock);
1933 static int __core_scsi3_write_aptpl_to_file(
1934 struct se_device *dev,
1937 struct t10_wwn *wwn = &dev->t10_wwn;
1939 int flags = O_RDWR | O_CREAT | O_TRUNC;
1941 u32 pr_aptpl_buf_len;
1944 memset(path, 0, 512);
1946 if (strlen(&wwn->unit_serial[0]) >= 512) {
1947 pr_err("WWN value for struct se_device does not fit"
1948 " into path buffer\n");
1952 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1953 file = filp_open(path, flags, 0600);
1955 pr_err("filp_open(%s) for APTPL metadata"
1957 return PTR_ERR(file);
1960 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
1962 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
1965 pr_debug("Error writing APTPL metadata file: %s\n", path);
1968 return (ret < 0) ? -EIO : 0;
1972 * Clear the APTPL metadata if APTPL has been disabled, otherwise
1973 * write out the updated metadata to struct file for this SCSI device.
1975 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
1978 int rc, len = PR_APTPL_BUF_LEN;
1981 char *null_buf = "No Registrations or Reservations\n";
1983 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
1984 dev->t10_pr.pr_aptpl_active = 0;
1985 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
1988 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1995 return TCM_OUT_OF_RESOURCES;
1997 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
2004 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2006 pr_err("SPC-3 PR: Could not update APTPL\n");
2008 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2010 dev->t10_pr.pr_aptpl_active = 1;
2012 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2016 static sense_reason_t
2017 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2018 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2020 struct se_session *se_sess = cmd->se_sess;
2021 struct se_device *dev = cmd->se_dev;
2022 struct se_dev_entry *se_deve;
2023 struct se_lun *se_lun = cmd->se_lun;
2024 struct se_portal_group *se_tpg;
2025 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2026 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2027 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2028 sense_reason_t ret = TCM_NO_SENSE;
2029 int pr_holder = 0, type;
2031 if (!se_sess || !se_lun) {
2032 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2033 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2035 se_tpg = se_sess->se_tpg;
2036 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2038 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2039 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2040 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2042 isid_ptr = &isid_buf[0];
2045 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2047 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2050 pr_warn("SPC-3 PR: Reservation Key non-zero"
2051 " for SA REGISTER, returning CONFLICT\n");
2052 return TCM_RESERVATION_CONFLICT;
2055 * Do nothing but return GOOD status.
2062 * Perform the Service Action REGISTER on the Initiator
2063 * Port Endpoint that the PRO was received from on the
2064 * Logical Unit of the SCSI device server.
2066 if (core_scsi3_alloc_registration(cmd->se_dev,
2067 se_sess->se_node_acl, se_deve, isid_ptr,
2068 sa_res_key, all_tg_pt, aptpl,
2069 register_type, 0)) {
2070 pr_err("Unable to allocate"
2071 " struct t10_pr_registration\n");
2072 return TCM_INVALID_PARAMETER_LIST;
2076 * Register both the Initiator port that received
2077 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2078 * TransportID from Parameter list and loop through
2079 * fabric dependent parameter list while calling
2080 * logic from of core_scsi3_alloc_registration() for
2081 * each TransportID provided SCSI Initiator Port/Device
2083 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2084 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2089 return core_scsi3_update_and_write_aptpl(dev, aptpl);
2092 /* ok, existing registration */
2094 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2095 pr_err("SPC-3 PR REGISTER: Received"
2096 " res_key: 0x%016Lx does not match"
2097 " existing SA REGISTER res_key:"
2098 " 0x%016Lx\n", res_key,
2099 pr_reg->pr_res_key);
2100 ret = TCM_RESERVATION_CONFLICT;
2105 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2106 " set on a registered nexus\n");
2107 ret = TCM_INVALID_PARAMETER_LIST;
2112 * An existing ALL_TG_PT=1 registration being released
2113 * must also set ALL_TG_PT=1 in the incoming PROUT.
2115 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2116 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2117 " registration exists, but ALL_TG_PT=1 bit not"
2118 " present in received PROUT\n");
2119 ret = TCM_INVALID_CDB_FIELD;
2124 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2128 * Increment PRgeneration counter for struct se_device"
2129 * upon a successful REGISTER, see spc4r17 section 6.3.2
2130 * READ_KEYS service action.
2132 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2133 pr_reg->pr_res_key = sa_res_key;
2134 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2135 " Key for %s to: 0x%016Lx PRgeneration:"
2136 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2137 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2138 pr_reg->pr_reg_nacl->initiatorname,
2139 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2143 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2145 type = pr_reg->pr_res_type;
2146 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2148 if (pr_holder < 0) {
2149 ret = TCM_RESERVATION_CONFLICT;
2153 spin_lock(&pr_tmpl->registration_lock);
2155 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2156 * and matching pr_res_key.
2158 if (pr_reg->pr_reg_all_tg_pt) {
2159 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2160 &pr_tmpl->registration_list,
2163 if (!pr_reg_p->pr_reg_all_tg_pt)
2165 if (pr_reg_p->pr_res_key != res_key)
2167 if (pr_reg == pr_reg_p)
2169 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2170 pr_reg_p->pr_reg_nacl->initiatorname))
2173 __core_scsi3_free_registration(dev,
2179 * Release the calling I_T Nexus registration now..
2181 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2185 * From spc4r17, section 5.7.11.3 Unregistering
2187 * If the persistent reservation is a registrants only
2188 * type, the device server shall establish a unit
2189 * attention condition for the initiator port associated
2190 * with every registered I_T nexus except for the I_T
2191 * nexus on which the PERSISTENT RESERVE OUT command was
2192 * received, with the additional sense code set to
2193 * RESERVATIONS RELEASED.
2196 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2197 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2198 list_for_each_entry(pr_reg_p,
2199 &pr_tmpl->registration_list,
2202 core_scsi3_ua_allocate(
2203 pr_reg_p->pr_reg_nacl,
2204 pr_reg_p->pr_res_mapped_lun,
2206 ASCQ_2AH_RESERVATIONS_RELEASED);
2210 spin_unlock(&pr_tmpl->registration_lock);
2213 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2217 core_scsi3_put_pr_reg(pr_reg);
2221 unsigned char *core_scsi3_pr_dump_type(int type)
2224 case PR_TYPE_WRITE_EXCLUSIVE:
2225 return "Write Exclusive Access";
2226 case PR_TYPE_EXCLUSIVE_ACCESS:
2227 return "Exclusive Access";
2228 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2229 return "Write Exclusive Access, Registrants Only";
2230 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2231 return "Exclusive Access, Registrants Only";
2232 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2233 return "Write Exclusive Access, All Registrants";
2234 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2235 return "Exclusive Access, All Registrants";
2240 return "Unknown SPC-3 PR Type";
2243 static sense_reason_t
2244 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2246 struct se_device *dev = cmd->se_dev;
2247 struct se_session *se_sess = cmd->se_sess;
2248 struct se_lun *se_lun = cmd->se_lun;
2249 struct t10_pr_registration *pr_reg, *pr_res_holder;
2250 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2251 char i_buf[PR_REG_ISID_ID_LEN];
2254 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2256 if (!se_sess || !se_lun) {
2257 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2258 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2261 * Locate the existing *pr_reg via struct se_node_acl pointers
2263 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2266 pr_err("SPC-3 PR: Unable to locate"
2267 " PR_REGISTERED *pr_reg for RESERVE\n");
2268 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2271 * From spc4r17 Section 5.7.9: Reserving:
2273 * An application client creates a persistent reservation by issuing
2274 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2275 * a registered I_T nexus with the following parameters:
2276 * a) RESERVATION KEY set to the value of the reservation key that is
2277 * registered with the logical unit for the I_T nexus; and
2279 if (res_key != pr_reg->pr_res_key) {
2280 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2281 " does not match existing SA REGISTER res_key:"
2282 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2283 ret = TCM_RESERVATION_CONFLICT;
2284 goto out_put_pr_reg;
2287 * From spc4r17 Section 5.7.9: Reserving:
2290 * b) TYPE field and SCOPE field set to the persistent reservation
2293 * Only one persistent reservation is allowed at a time per logical unit
2294 * and that persistent reservation has a scope of LU_SCOPE.
2296 if (scope != PR_SCOPE_LU_SCOPE) {
2297 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2298 ret = TCM_INVALID_PARAMETER_LIST;
2299 goto out_put_pr_reg;
2302 * See if we have an existing PR reservation holder pointer at
2303 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2306 spin_lock(&dev->dev_reservation_lock);
2307 pr_res_holder = dev->dev_pr_res_holder;
2308 if (pr_res_holder) {
2310 * From spc4r17 Section 5.7.9: Reserving:
2312 * If the device server receives a PERSISTENT RESERVE OUT
2313 * command from an I_T nexus other than a persistent reservation
2314 * holder (see 5.7.10) that attempts to create a persistent
2315 * reservation when a persistent reservation already exists for
2316 * the logical unit, then the command shall be completed with
2317 * RESERVATION CONFLICT status.
2319 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2320 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2321 pr_err("SPC-3 PR: Attempted RESERVE from"
2322 " [%s]: %s while reservation already held by"
2323 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2324 cmd->se_tfo->get_fabric_name(),
2325 se_sess->se_node_acl->initiatorname,
2326 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2327 pr_res_holder->pr_reg_nacl->initiatorname);
2329 spin_unlock(&dev->dev_reservation_lock);
2330 ret = TCM_RESERVATION_CONFLICT;
2331 goto out_put_pr_reg;
2334 * From spc4r17 Section 5.7.9: Reserving:
2336 * If a persistent reservation holder attempts to modify the
2337 * type or scope of an existing persistent reservation, the
2338 * command shall be completed with RESERVATION CONFLICT status.
2340 if ((pr_res_holder->pr_res_type != type) ||
2341 (pr_res_holder->pr_res_scope != scope)) {
2342 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2343 pr_err("SPC-3 PR: Attempted RESERVE from"
2344 " [%s]: %s trying to change TYPE and/or SCOPE,"
2345 " while reservation already held by [%s]: %s,"
2346 " returning RESERVATION_CONFLICT\n",
2347 cmd->se_tfo->get_fabric_name(),
2348 se_sess->se_node_acl->initiatorname,
2349 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2350 pr_res_holder->pr_reg_nacl->initiatorname);
2352 spin_unlock(&dev->dev_reservation_lock);
2353 ret = TCM_RESERVATION_CONFLICT;
2354 goto out_put_pr_reg;
2357 * From spc4r17 Section 5.7.9: Reserving:
2359 * If the device server receives a PERSISTENT RESERVE OUT
2360 * command with RESERVE service action where the TYPE field and
2361 * the SCOPE field contain the same values as the existing type
2362 * and scope from a persistent reservation holder, it shall not
2363 * make any change to the existing persistent reservation and
2364 * shall completethe command with GOOD status.
2366 spin_unlock(&dev->dev_reservation_lock);
2368 goto out_put_pr_reg;
2371 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2372 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2374 pr_reg->pr_res_scope = scope;
2375 pr_reg->pr_res_type = type;
2376 pr_reg->pr_res_holder = 1;
2377 dev->dev_pr_res_holder = pr_reg;
2378 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2380 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2381 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2382 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2383 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2384 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2385 cmd->se_tfo->get_fabric_name(),
2386 se_sess->se_node_acl->initiatorname,
2388 spin_unlock(&dev->dev_reservation_lock);
2390 if (pr_tmpl->pr_aptpl_active)
2391 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2395 core_scsi3_put_pr_reg(pr_reg);
2399 static sense_reason_t
2400 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2404 case PR_TYPE_WRITE_EXCLUSIVE:
2405 case PR_TYPE_EXCLUSIVE_ACCESS:
2406 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2407 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2408 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2409 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2410 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2412 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2414 return TCM_INVALID_CDB_FIELD;
2419 * Called with struct se_device->dev_reservation_lock held.
2421 static void __core_scsi3_complete_pro_release(
2422 struct se_device *dev,
2423 struct se_node_acl *se_nacl,
2424 struct t10_pr_registration *pr_reg,
2428 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2429 char i_buf[PR_REG_ISID_ID_LEN];
2430 int pr_res_type = 0, pr_res_scope = 0;
2432 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2433 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2435 * Go ahead and release the current PR reservation holder.
2436 * If an All Registrants reservation is currently active and
2437 * a unregister operation is requested, replace the current
2438 * dev_pr_res_holder with another active registration.
2440 if (dev->dev_pr_res_holder) {
2441 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2442 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2443 dev->dev_pr_res_holder->pr_res_type = 0;
2444 dev->dev_pr_res_holder->pr_res_scope = 0;
2445 dev->dev_pr_res_holder->pr_res_holder = 0;
2446 dev->dev_pr_res_holder = NULL;
2451 spin_lock(&dev->t10_pr.registration_lock);
2452 list_del_init(&pr_reg->pr_reg_list);
2454 * If the I_T nexus is a reservation holder, the persistent reservation
2455 * is of an all registrants type, and the I_T nexus is the last remaining
2456 * registered I_T nexus, then the device server shall also release the
2457 * persistent reservation.
2459 if (!list_empty(&dev->t10_pr.registration_list) &&
2460 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2461 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2462 dev->dev_pr_res_holder =
2463 list_entry(dev->t10_pr.registration_list.next,
2464 struct t10_pr_registration, pr_reg_list);
2465 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2466 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2467 dev->dev_pr_res_holder->pr_res_holder = 1;
2469 spin_unlock(&dev->t10_pr.registration_lock);
2471 if (!dev->dev_pr_res_holder) {
2472 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2473 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2474 tfo->get_fabric_name(), (explicit) ? "explicit" :
2475 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2476 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2478 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2479 tfo->get_fabric_name(), se_nacl->initiatorname,
2482 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2484 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2487 static sense_reason_t
2488 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2491 struct se_device *dev = cmd->se_dev;
2492 struct se_session *se_sess = cmd->se_sess;
2493 struct se_lun *se_lun = cmd->se_lun;
2494 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2495 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2496 sense_reason_t ret = 0;
2498 if (!se_sess || !se_lun) {
2499 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2500 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2503 * Locate the existing *pr_reg via struct se_node_acl pointers
2505 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2507 pr_err("SPC-3 PR: Unable to locate"
2508 " PR_REGISTERED *pr_reg for RELEASE\n");
2509 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2512 * From spc4r17 Section 5.7.11.2 Releasing:
2514 * If there is no persistent reservation or in response to a persistent
2515 * reservation release request from a registered I_T nexus that is not a
2516 * persistent reservation holder (see 5.7.10), the device server shall
2519 * a) Not release the persistent reservation, if any;
2520 * b) Not remove any registrations; and
2521 * c) Complete the command with GOOD status.
2523 spin_lock(&dev->dev_reservation_lock);
2524 pr_res_holder = dev->dev_pr_res_holder;
2525 if (!pr_res_holder) {
2527 * No persistent reservation, return GOOD status.
2529 spin_unlock(&dev->dev_reservation_lock);
2530 goto out_put_pr_reg;
2533 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2535 * Release request from a registered I_T nexus that is not a
2536 * persistent reservation holder. return GOOD status.
2538 spin_unlock(&dev->dev_reservation_lock);
2539 goto out_put_pr_reg;
2543 * From spc4r17 Section 5.7.11.2 Releasing:
2545 * Only the persistent reservation holder (see 5.7.10) is allowed to
2546 * release a persistent reservation.
2548 * An application client releases the persistent reservation by issuing
2549 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2550 * an I_T nexus that is a persistent reservation holder with the
2551 * following parameters:
2553 * a) RESERVATION KEY field set to the value of the reservation key
2554 * that is registered with the logical unit for the I_T nexus;
2556 if (res_key != pr_reg->pr_res_key) {
2557 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2558 " does not match existing SA REGISTER res_key:"
2559 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2560 spin_unlock(&dev->dev_reservation_lock);
2561 ret = TCM_RESERVATION_CONFLICT;
2562 goto out_put_pr_reg;
2565 * From spc4r17 Section 5.7.11.2 Releasing and above:
2567 * b) TYPE field and SCOPE field set to match the persistent
2568 * reservation being released.
2570 if ((pr_res_holder->pr_res_type != type) ||
2571 (pr_res_holder->pr_res_scope != scope)) {
2572 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2573 pr_err("SPC-3 PR RELEASE: Attempted to release"
2574 " reservation from [%s]: %s with different TYPE "
2575 "and/or SCOPE while reservation already held by"
2576 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2577 cmd->se_tfo->get_fabric_name(),
2578 se_sess->se_node_acl->initiatorname,
2579 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2580 pr_res_holder->pr_reg_nacl->initiatorname);
2582 spin_unlock(&dev->dev_reservation_lock);
2583 ret = TCM_RESERVATION_CONFLICT;
2584 goto out_put_pr_reg;
2587 * In response to a persistent reservation release request from the
2588 * persistent reservation holder the device server shall perform a
2589 * release by doing the following as an uninterrupted series of actions:
2590 * a) Release the persistent reservation;
2591 * b) Not remove any registration(s);
2592 * c) If the released persistent reservation is a registrants only type
2593 * or all registrants type persistent reservation,
2594 * the device server shall establish a unit attention condition for
2595 * the initiator port associated with every regis-
2596 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2597 * RESERVE OUT command with RELEASE service action was received,
2598 * with the additional sense code set to RESERVATIONS RELEASED; and
2599 * d) If the persistent reservation is of any other type, the device
2600 * server shall not establish a unit attention condition.
2602 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2605 spin_unlock(&dev->dev_reservation_lock);
2607 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2608 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2609 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2610 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2612 * If no UNIT ATTENTION conditions will be established for
2613 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2614 * go ahead and check for APTPL=1 update+write below
2619 spin_lock(&pr_tmpl->registration_lock);
2620 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2623 * Do not establish a UNIT ATTENTION condition
2624 * for the calling I_T Nexus
2626 if (pr_reg_p == pr_reg)
2629 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2630 pr_reg_p->pr_res_mapped_lun,
2631 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2633 spin_unlock(&pr_tmpl->registration_lock);
2636 if (pr_tmpl->pr_aptpl_active)
2637 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2640 core_scsi3_put_pr_reg(pr_reg);
2644 static sense_reason_t
2645 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2647 struct se_device *dev = cmd->se_dev;
2648 struct se_node_acl *pr_reg_nacl;
2649 struct se_session *se_sess = cmd->se_sess;
2650 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2651 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2652 u32 pr_res_mapped_lun = 0;
2653 int calling_it_nexus = 0;
2655 * Locate the existing *pr_reg via struct se_node_acl pointers
2657 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2658 se_sess->se_node_acl, se_sess);
2660 pr_err("SPC-3 PR: Unable to locate"
2661 " PR_REGISTERED *pr_reg for CLEAR\n");
2662 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2665 * From spc4r17 section 5.7.11.6, Clearing:
2667 * Any application client may release the persistent reservation and
2668 * remove all registrations from a device server by issuing a
2669 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2670 * registered I_T nexus with the following parameter:
2672 * a) RESERVATION KEY field set to the value of the reservation key
2673 * that is registered with the logical unit for the I_T nexus.
2675 if (res_key != pr_reg_n->pr_res_key) {
2676 pr_err("SPC-3 PR REGISTER: Received"
2677 " res_key: 0x%016Lx does not match"
2678 " existing SA REGISTER res_key:"
2679 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2680 core_scsi3_put_pr_reg(pr_reg_n);
2681 return TCM_RESERVATION_CONFLICT;
2684 * a) Release the persistent reservation, if any;
2686 spin_lock(&dev->dev_reservation_lock);
2687 pr_res_holder = dev->dev_pr_res_holder;
2688 if (pr_res_holder) {
2689 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2690 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2691 pr_res_holder, 0, 0);
2693 spin_unlock(&dev->dev_reservation_lock);
2695 * b) Remove all registration(s) (see spc4r17 5.7.7);
2697 spin_lock(&pr_tmpl->registration_lock);
2698 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2699 &pr_tmpl->registration_list, pr_reg_list) {
2701 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2702 pr_reg_nacl = pr_reg->pr_reg_nacl;
2703 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2704 __core_scsi3_free_registration(dev, pr_reg, NULL,
2707 * e) Establish a unit attention condition for the initiator
2708 * port associated with every registered I_T nexus other
2709 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2710 * command with CLEAR service action was received, with the
2711 * additional sense code set to RESERVATIONS PREEMPTED.
2713 if (!calling_it_nexus)
2714 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2715 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2717 spin_unlock(&pr_tmpl->registration_lock);
2719 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2720 cmd->se_tfo->get_fabric_name());
2722 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2724 core_scsi3_pr_generation(dev);
2729 * Called with struct se_device->dev_reservation_lock held.
2731 static void __core_scsi3_complete_pro_preempt(
2732 struct se_device *dev,
2733 struct t10_pr_registration *pr_reg,
2734 struct list_head *preempt_and_abort_list,
2737 enum preempt_type preempt_type)
2739 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2740 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2741 char i_buf[PR_REG_ISID_ID_LEN];
2743 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2744 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2746 * Do an implicit RELEASE of the existing reservation.
2748 if (dev->dev_pr_res_holder)
2749 __core_scsi3_complete_pro_release(dev, nacl,
2750 dev->dev_pr_res_holder, 0, 0);
2752 dev->dev_pr_res_holder = pr_reg;
2753 pr_reg->pr_res_holder = 1;
2754 pr_reg->pr_res_type = type;
2755 pr_reg->pr_res_scope = scope;
2757 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2758 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2759 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2760 core_scsi3_pr_dump_type(type),
2761 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2762 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2763 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2764 nacl->initiatorname, i_buf);
2766 * For PREEMPT_AND_ABORT, add the preempting reservation's
2767 * struct t10_pr_registration to the list that will be compared
2768 * against received CDBs..
2770 if (preempt_and_abort_list)
2771 list_add_tail(&pr_reg->pr_reg_abort_list,
2772 preempt_and_abort_list);
2775 static void core_scsi3_release_preempt_and_abort(
2776 struct list_head *preempt_and_abort_list,
2777 struct t10_pr_registration *pr_reg_holder)
2779 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2781 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2782 pr_reg_abort_list) {
2784 list_del(&pr_reg->pr_reg_abort_list);
2785 if (pr_reg_holder == pr_reg)
2787 if (pr_reg->pr_res_holder) {
2788 pr_warn("pr_reg->pr_res_holder still set\n");
2792 pr_reg->pr_reg_deve = NULL;
2793 pr_reg->pr_reg_nacl = NULL;
2794 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2798 static sense_reason_t
2799 core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2800 u64 sa_res_key, enum preempt_type preempt_type)
2802 struct se_device *dev = cmd->se_dev;
2803 struct se_node_acl *pr_reg_nacl;
2804 struct se_session *se_sess = cmd->se_sess;
2805 LIST_HEAD(preempt_and_abort_list);
2806 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2807 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2808 u32 pr_res_mapped_lun = 0;
2809 int all_reg = 0, calling_it_nexus = 0;
2810 bool sa_res_key_unmatched = sa_res_key != 0;
2811 int prh_type = 0, prh_scope = 0;
2814 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2816 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2819 pr_err("SPC-3 PR: Unable to locate"
2820 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2821 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2822 return TCM_RESERVATION_CONFLICT;
2824 if (pr_reg_n->pr_res_key != res_key) {
2825 core_scsi3_put_pr_reg(pr_reg_n);
2826 return TCM_RESERVATION_CONFLICT;
2828 if (scope != PR_SCOPE_LU_SCOPE) {
2829 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2830 core_scsi3_put_pr_reg(pr_reg_n);
2831 return TCM_INVALID_PARAMETER_LIST;
2834 spin_lock(&dev->dev_reservation_lock);
2835 pr_res_holder = dev->dev_pr_res_holder;
2836 if (pr_res_holder &&
2837 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2838 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2841 if (!all_reg && !sa_res_key) {
2842 spin_unlock(&dev->dev_reservation_lock);
2843 core_scsi3_put_pr_reg(pr_reg_n);
2844 return TCM_INVALID_PARAMETER_LIST;
2847 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2849 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2850 * persistent reservation holder or there is no persistent reservation
2851 * holder (i.e., there is no persistent reservation), then the device
2852 * server shall perform a preempt by doing the following in an
2853 * uninterrupted series of actions. (See below..)
2855 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2857 * No existing or SA Reservation Key matching reservations..
2859 * PROUT SA PREEMPT with All Registrant type reservations are
2860 * allowed to be processed without a matching SA Reservation Key
2862 spin_lock(&pr_tmpl->registration_lock);
2863 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2864 &pr_tmpl->registration_list, pr_reg_list) {
2866 * Removing of registrations in non all registrants
2867 * type reservations without a matching SA reservation
2870 * a) Remove the registrations for all I_T nexuses
2871 * specified by the SERVICE ACTION RESERVATION KEY
2873 * b) Ignore the contents of the SCOPE and TYPE fields;
2874 * c) Process tasks as defined in 5.7.1; and
2875 * d) Establish a unit attention condition for the
2876 * initiator port associated with every I_T nexus
2877 * that lost its registration other than the I_T
2878 * nexus on which the PERSISTENT RESERVE OUT command
2879 * was received, with the additional sense code set
2880 * to REGISTRATIONS PREEMPTED.
2883 if (pr_reg->pr_res_key != sa_res_key)
2885 sa_res_key_unmatched = false;
2887 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2888 pr_reg_nacl = pr_reg->pr_reg_nacl;
2889 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2890 __core_scsi3_free_registration(dev, pr_reg,
2891 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2892 NULL, calling_it_nexus);
2895 * Case for any existing all registrants type
2896 * reservation, follow logic in spc4r17 section
2897 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2899 * For a ZERO SA Reservation key, release
2900 * all other registrations and do an implicit
2901 * release of active persistent reservation.
2903 * For a non-ZERO SA Reservation key, only
2904 * release the matching reservation key from
2908 (pr_reg->pr_res_key != sa_res_key))
2910 sa_res_key_unmatched = false;
2912 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2913 if (calling_it_nexus)
2916 pr_reg_nacl = pr_reg->pr_reg_nacl;
2917 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2918 __core_scsi3_free_registration(dev, pr_reg,
2919 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2922 if (!calling_it_nexus)
2923 core_scsi3_ua_allocate(pr_reg_nacl,
2924 pr_res_mapped_lun, 0x2A,
2925 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2927 spin_unlock(&pr_tmpl->registration_lock);
2929 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2930 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2931 * RESERVATION KEY field to a value that does not match any
2932 * registered reservation key, then the device server shall
2933 * complete the command with RESERVATION CONFLICT status.
2935 if (sa_res_key_unmatched) {
2936 spin_unlock(&dev->dev_reservation_lock);
2937 core_scsi3_put_pr_reg(pr_reg_n);
2938 return TCM_RESERVATION_CONFLICT;
2941 * For an existing all registrants type reservation
2942 * with a zero SA rservation key, preempt the existing
2943 * reservation with the new PR type and scope.
2945 if (pr_res_holder && all_reg && !(sa_res_key)) {
2946 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
2947 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2948 type, scope, preempt_type);
2950 if (preempt_type == PREEMPT_AND_ABORT)
2951 core_scsi3_release_preempt_and_abort(
2952 &preempt_and_abort_list, pr_reg_n);
2954 spin_unlock(&dev->dev_reservation_lock);
2956 if (pr_tmpl->pr_aptpl_active)
2957 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2959 core_scsi3_put_pr_reg(pr_reg_n);
2960 core_scsi3_pr_generation(cmd->se_dev);
2964 * The PREEMPTing SA reservation key matches that of the
2965 * existing persistent reservation, first, we check if
2966 * we are preempting our own reservation.
2967 * From spc4r17, section 5.7.11.4.3 Preempting
2968 * persistent reservations and registration handling
2970 * If an all registrants persistent reservation is not
2971 * present, it is not an error for the persistent
2972 * reservation holder to preempt itself (i.e., a
2973 * PERSISTENT RESERVE OUT with a PREEMPT service action
2974 * or a PREEMPT AND ABORT service action with the
2975 * SERVICE ACTION RESERVATION KEY value equal to the
2976 * persistent reservation holder's reservation key that
2977 * is received from the persistent reservation holder).
2978 * In that case, the device server shall establish the
2979 * new persistent reservation and maintain the
2982 prh_type = pr_res_holder->pr_res_type;
2983 prh_scope = pr_res_holder->pr_res_scope;
2985 * If the SERVICE ACTION RESERVATION KEY field identifies a
2986 * persistent reservation holder (see 5.7.10), the device
2987 * server shall perform a preempt by doing the following as
2988 * an uninterrupted series of actions:
2990 * a) Release the persistent reservation for the holder
2991 * identified by the SERVICE ACTION RESERVATION KEY field;
2993 if (pr_reg_n != pr_res_holder)
2994 __core_scsi3_complete_pro_release(dev,
2995 pr_res_holder->pr_reg_nacl,
2996 dev->dev_pr_res_holder, 0, 0);
2998 * b) Remove the registrations for all I_T nexuses identified
2999 * by the SERVICE ACTION RESERVATION KEY field, except the
3000 * I_T nexus that is being used for the PERSISTENT RESERVE
3001 * OUT command. If an all registrants persistent reservation
3002 * is present and the SERVICE ACTION RESERVATION KEY field
3003 * is set to zero, then all registrations shall be removed
3004 * except for that of the I_T nexus that is being used for
3005 * the PERSISTENT RESERVE OUT command;
3007 spin_lock(&pr_tmpl->registration_lock);
3008 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3009 &pr_tmpl->registration_list, pr_reg_list) {
3011 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3012 if (calling_it_nexus)
3015 if (pr_reg->pr_res_key != sa_res_key)
3018 pr_reg_nacl = pr_reg->pr_reg_nacl;
3019 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3020 __core_scsi3_free_registration(dev, pr_reg,
3021 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3024 * e) Establish a unit attention condition for the initiator
3025 * port associated with every I_T nexus that lost its
3026 * persistent reservation and/or registration, with the
3027 * additional sense code set to REGISTRATIONS PREEMPTED;
3029 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3030 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3032 spin_unlock(&pr_tmpl->registration_lock);
3034 * c) Establish a persistent reservation for the preempting
3035 * I_T nexus using the contents of the SCOPE and TYPE fields;
3037 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3038 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3039 type, scope, preempt_type);
3041 * d) Process tasks as defined in 5.7.1;
3043 * f) If the type or scope has changed, then for every I_T nexus
3044 * whose reservation key was not removed, except for the I_T
3045 * nexus on which the PERSISTENT RESERVE OUT command was
3046 * received, the device server shall establish a unit
3047 * attention condition for the initiator port associated with
3048 * that I_T nexus, with the additional sense code set to
3049 * RESERVATIONS RELEASED. If the type or scope have not
3050 * changed, then no unit attention condition(s) shall be
3051 * established for this reason.
3053 if ((prh_type != type) || (prh_scope != scope)) {
3054 spin_lock(&pr_tmpl->registration_lock);
3055 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3056 &pr_tmpl->registration_list, pr_reg_list) {
3058 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3059 if (calling_it_nexus)
3062 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3063 pr_reg->pr_res_mapped_lun, 0x2A,
3064 ASCQ_2AH_RESERVATIONS_RELEASED);
3066 spin_unlock(&pr_tmpl->registration_lock);
3068 spin_unlock(&dev->dev_reservation_lock);
3070 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3071 * All received CDBs for the matching existing reservation and
3072 * registrations undergo ABORT_TASK logic.
3074 * From there, core_scsi3_release_preempt_and_abort() will
3075 * release every registration in the list (which have already
3076 * been removed from the primary pr_reg list), except the
3077 * new persistent reservation holder, the calling Initiator Port.
3079 if (preempt_type == PREEMPT_AND_ABORT) {
3080 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3081 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3085 if (pr_tmpl->pr_aptpl_active)
3086 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3088 core_scsi3_put_pr_reg(pr_reg_n);
3089 core_scsi3_pr_generation(cmd->se_dev);
3093 static sense_reason_t
3094 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3095 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3098 case PR_TYPE_WRITE_EXCLUSIVE:
3099 case PR_TYPE_EXCLUSIVE_ACCESS:
3100 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3101 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3102 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3103 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3104 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3105 sa_res_key, preempt_type);
3107 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3108 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3109 return TCM_INVALID_CDB_FIELD;
3114 static sense_reason_t
3115 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3116 u64 sa_res_key, int aptpl, int unreg)
3118 struct se_session *se_sess = cmd->se_sess;
3119 struct se_device *dev = cmd->se_dev;
3120 struct se_dev_entry *dest_se_deve = NULL;
3121 struct se_lun *se_lun = cmd->se_lun;
3122 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3123 struct se_port *se_port;
3124 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3125 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3126 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3127 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3129 unsigned char *initiator_str;
3130 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
3131 u32 tid_len, tmp_tid_len;
3132 int new_reg = 0, type, scope, matching_iname;
3134 unsigned short rtpi;
3135 unsigned char proto_ident;
3137 if (!se_sess || !se_lun) {
3138 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3139 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3142 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3143 se_tpg = se_sess->se_tpg;
3144 tf_ops = se_tpg->se_tpg_tfo;
3146 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3147 * Register behaviors for a REGISTER AND MOVE service action
3149 * Locate the existing *pr_reg via struct se_node_acl pointers
3151 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3154 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3155 " *pr_reg for REGISTER_AND_MOVE\n");
3156 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3159 * The provided reservation key much match the existing reservation key
3160 * provided during this initiator's I_T nexus registration.
3162 if (res_key != pr_reg->pr_res_key) {
3163 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3164 " res_key: 0x%016Lx does not match existing SA REGISTER"
3165 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3166 ret = TCM_RESERVATION_CONFLICT;
3167 goto out_put_pr_reg;
3170 * The service active reservation key needs to be non zero
3173 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3175 ret = TCM_INVALID_PARAMETER_LIST;
3176 goto out_put_pr_reg;
3180 * Determine the Relative Target Port Identifier where the reservation
3181 * will be moved to for the TransportID containing SCSI initiator WWN
3184 buf = transport_kmap_data_sg(cmd);
3186 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3187 goto out_put_pr_reg;
3190 rtpi = (buf[18] & 0xff) << 8;
3191 rtpi |= buf[19] & 0xff;
3192 tid_len = (buf[20] & 0xff) << 24;
3193 tid_len |= (buf[21] & 0xff) << 16;
3194 tid_len |= (buf[22] & 0xff) << 8;
3195 tid_len |= buf[23] & 0xff;
3196 transport_kunmap_data_sg(cmd);
3199 if ((tid_len + 24) != cmd->data_length) {
3200 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3201 " does not equal CDB data_length: %u\n", tid_len,
3203 ret = TCM_INVALID_PARAMETER_LIST;
3204 goto out_put_pr_reg;
3207 spin_lock(&dev->se_port_lock);
3208 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3209 if (se_port->sep_rtpi != rtpi)
3211 dest_se_tpg = se_port->sep_tpg;
3214 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3218 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3219 spin_unlock(&dev->se_port_lock);
3221 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3222 pr_err("core_scsi3_tpg_depend_item() failed"
3223 " for dest_se_tpg\n");
3224 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3225 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3226 goto out_put_pr_reg;
3229 spin_lock(&dev->se_port_lock);
3232 spin_unlock(&dev->se_port_lock);
3234 if (!dest_se_tpg || !dest_tf_ops) {
3235 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3236 " fabric ops from Relative Target Port Identifier:"
3238 ret = TCM_INVALID_PARAMETER_LIST;
3239 goto out_put_pr_reg;
3242 buf = transport_kmap_data_sg(cmd);
3244 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3245 goto out_put_pr_reg;
3247 proto_ident = (buf[24] & 0x0f);
3249 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3250 " 0x%02x\n", proto_ident);
3252 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3253 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3254 " proto_ident: 0x%02x does not match ident: 0x%02x"
3255 " from fabric: %s\n", proto_ident,
3256 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3257 dest_tf_ops->get_fabric_name());
3258 ret = TCM_INVALID_PARAMETER_LIST;
3261 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3262 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3263 " containg a valid tpg_parse_pr_out_transport_id"
3264 " function pointer\n");
3265 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3268 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3269 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3270 if (!initiator_str) {
3271 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3272 " initiator_str from Transport ID\n");
3273 ret = TCM_INVALID_PARAMETER_LIST;
3277 transport_kunmap_data_sg(cmd);
3280 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3281 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3282 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3285 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3286 * action specifies a TransportID that is the same as the initiator port
3287 * of the I_T nexus for the command received, then the command shall
3288 * be terminated with CHECK CONDITION status, with the sense key set to
3289 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3290 * IN PARAMETER LIST.
3292 pr_reg_nacl = pr_reg->pr_reg_nacl;
3293 matching_iname = (!strcmp(initiator_str,
3294 pr_reg_nacl->initiatorname)) ? 1 : 0;
3295 if (!matching_iname)
3296 goto after_iport_check;
3298 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3299 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3300 " matches: %s on received I_T Nexus\n", initiator_str,
3301 pr_reg_nacl->initiatorname);
3302 ret = TCM_INVALID_PARAMETER_LIST;
3305 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3306 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3307 " matches: %s %s on received I_T Nexus\n",
3308 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3309 pr_reg->pr_reg_isid);
3310 ret = TCM_INVALID_PARAMETER_LIST;
3315 * Locate the destination struct se_node_acl from the received Transport ID
3317 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3318 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3321 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3322 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3324 if (!dest_node_acl) {
3325 pr_err("Unable to locate %s dest_node_acl for"
3326 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3328 ret = TCM_INVALID_PARAMETER_LIST;
3332 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3333 pr_err("core_scsi3_nodeacl_depend_item() for"
3334 " dest_node_acl\n");
3335 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3336 dest_node_acl = NULL;
3337 ret = TCM_INVALID_PARAMETER_LIST;
3341 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3342 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3343 dest_node_acl->initiatorname);
3346 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3349 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3350 if (!dest_se_deve) {
3351 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3352 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3353 ret = TCM_INVALID_PARAMETER_LIST;
3357 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3358 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3359 atomic_dec_mb(&dest_se_deve->pr_ref_count);
3360 dest_se_deve = NULL;
3361 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3365 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3366 " ACL for dest_se_deve->mapped_lun: %u\n",
3367 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3368 dest_se_deve->mapped_lun);
3371 * A persistent reservation needs to already existing in order to
3372 * successfully complete the REGISTER_AND_MOVE service action..
3374 spin_lock(&dev->dev_reservation_lock);
3375 pr_res_holder = dev->dev_pr_res_holder;
3376 if (!pr_res_holder) {
3377 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3378 " currently held\n");
3379 spin_unlock(&dev->dev_reservation_lock);
3380 ret = TCM_INVALID_CDB_FIELD;
3384 * The received on I_T Nexus must be the reservation holder.
3386 * From spc4r17 section 5.7.8 Table 50 --
3387 * Register behaviors for a REGISTER AND MOVE service action
3389 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3390 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3391 " Nexus is not reservation holder\n");
3392 spin_unlock(&dev->dev_reservation_lock);
3393 ret = TCM_RESERVATION_CONFLICT;
3397 * From spc4r17 section 5.7.8: registering and moving reservation
3399 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3400 * action is received and the established persistent reservation is a
3401 * Write Exclusive - All Registrants type or Exclusive Access -
3402 * All Registrants type reservation, then the command shall be completed
3403 * with RESERVATION CONFLICT status.
3405 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3406 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3407 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3408 " reservation for type: %s\n",
3409 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3410 spin_unlock(&dev->dev_reservation_lock);
3411 ret = TCM_RESERVATION_CONFLICT;
3414 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3416 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3418 type = pr_res_holder->pr_res_type;
3419 scope = pr_res_holder->pr_res_type;
3421 * c) Associate the reservation key specified in the SERVICE ACTION
3422 * RESERVATION KEY field with the I_T nexus specified as the
3423 * destination of the register and move, where:
3424 * A) The I_T nexus is specified by the TransportID and the
3425 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3426 * B) Regardless of the TransportID format used, the association for
3427 * the initiator port is based on either the initiator port name
3428 * (see 3.1.71) on SCSI transport protocols where port names are
3429 * required or the initiator port identifier (see 3.1.70) on SCSI
3430 * transport protocols where port names are not required;
3431 * d) Register the reservation key specified in the SERVICE ACTION
3432 * RESERVATION KEY field;
3433 * e) Retain the reservation key specified in the SERVICE ACTION
3434 * RESERVATION KEY field and associated information;
3436 * Also, It is not an error for a REGISTER AND MOVE service action to
3437 * register an I_T nexus that is already registered with the same
3438 * reservation key or a different reservation key.
3440 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3443 if (core_scsi3_alloc_registration(cmd->se_dev,
3444 dest_node_acl, dest_se_deve, iport_ptr,
3445 sa_res_key, 0, aptpl, 2, 1)) {
3446 spin_unlock(&dev->dev_reservation_lock);
3447 ret = TCM_INVALID_PARAMETER_LIST;
3450 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3455 * f) Release the persistent reservation for the persistent reservation
3456 * holder (i.e., the I_T nexus on which the
3458 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3459 dev->dev_pr_res_holder, 0, 0);
3461 * g) Move the persistent reservation to the specified I_T nexus using
3462 * the same scope and type as the persistent reservation released in
3465 dev->dev_pr_res_holder = dest_pr_reg;
3466 dest_pr_reg->pr_res_holder = 1;
3467 dest_pr_reg->pr_res_type = type;
3468 pr_reg->pr_res_scope = scope;
3469 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3471 * Increment PRGeneration for existing registrations..
3474 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3475 spin_unlock(&dev->dev_reservation_lock);
3477 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3478 " created new reservation holder TYPE: %s on object RTPI:"
3479 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3480 core_scsi3_pr_dump_type(type), rtpi,
3481 dest_pr_reg->pr_res_generation);
3482 pr_debug("SPC-3 PR Successfully moved reservation from"
3483 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3484 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3485 i_buf, dest_tf_ops->get_fabric_name(),
3486 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3489 * It is now safe to release configfs group dependencies for destination
3490 * of Transport ID Initiator Device/Port Identifier
3492 core_scsi3_lunacl_undepend_item(dest_se_deve);
3493 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3494 core_scsi3_tpg_undepend_item(dest_se_tpg);
3496 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3497 * nexus on which PERSISTENT RESERVE OUT command was received.
3500 spin_lock(&pr_tmpl->registration_lock);
3501 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3502 spin_unlock(&pr_tmpl->registration_lock);
3504 core_scsi3_put_pr_reg(pr_reg);
3506 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3508 transport_kunmap_data_sg(cmd);
3510 core_scsi3_put_pr_reg(dest_pr_reg);
3514 transport_kunmap_data_sg(cmd);
3516 core_scsi3_lunacl_undepend_item(dest_se_deve);
3518 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3519 core_scsi3_tpg_undepend_item(dest_se_tpg);
3522 core_scsi3_put_pr_reg(pr_reg);
3526 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3528 unsigned int __v1, __v2;
3530 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3531 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3533 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3537 * See spc4r17 section 6.14 Table 170
3540 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3542 struct se_device *dev = cmd->se_dev;
3543 unsigned char *cdb = &cmd->t_task_cdb[0];
3545 u64 res_key, sa_res_key;
3546 int sa, scope, type, aptpl;
3547 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3551 * Following spc2r20 5.5.1 Reservations overview:
3553 * If a logical unit has been reserved by any RESERVE command and is
3554 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3555 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3556 * initiator or service action and shall terminate with a RESERVATION
3559 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3560 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3561 " SPC-2 reservation is held, returning"
3562 " RESERVATION_CONFLICT\n");
3563 return TCM_RESERVATION_CONFLICT;
3567 * FIXME: A NULL struct se_session pointer means an this is not coming from
3568 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3571 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3573 if (cmd->data_length < 24) {
3574 pr_warn("SPC-PR: Received PR OUT parameter list"
3575 " length too small: %u\n", cmd->data_length);
3576 return TCM_INVALID_PARAMETER_LIST;
3580 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3582 sa = (cdb[1] & 0x1f);
3583 scope = (cdb[2] & 0xf0);
3584 type = (cdb[2] & 0x0f);
3586 buf = transport_kmap_data_sg(cmd);
3588 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3591 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3593 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3594 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3596 * REGISTER_AND_MOVE uses a different SA parameter list containing
3597 * SCSI TransportIDs.
3599 if (sa != PRO_REGISTER_AND_MOVE) {
3600 spec_i_pt = (buf[20] & 0x08);
3601 all_tg_pt = (buf[20] & 0x04);
3602 aptpl = (buf[20] & 0x01);
3604 aptpl = (buf[17] & 0x01);
3605 unreg = (buf[17] & 0x02);
3608 * If the backend device has been configured to force APTPL metadata
3609 * write-out, go ahead and propigate aptpl=1 down now.
3611 if (dev->dev_attrib.force_pr_aptpl)
3614 transport_kunmap_data_sg(cmd);
3618 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3620 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3621 return TCM_INVALID_PARAMETER_LIST;
3624 * From spc4r17 section 6.14:
3626 * If the SPEC_I_PT bit is set to zero, the service action is not
3627 * REGISTER AND MOVE, and the parameter list length is not 24, then
3628 * the command shall be terminated with CHECK CONDITION status, with
3629 * the sense key set to ILLEGAL REQUEST, and the additional sense
3630 * code set to PARAMETER LIST LENGTH ERROR.
3632 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3633 (cmd->data_length != 24)) {
3634 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3635 " list length: %u\n", cmd->data_length);
3636 return TCM_INVALID_PARAMETER_LIST;
3640 * (core_scsi3_emulate_pro_* function parameters
3641 * are defined by spc4r17 Table 174:
3642 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3646 ret = core_scsi3_emulate_pro_register(cmd,
3647 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3650 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3653 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3656 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3659 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3660 res_key, sa_res_key, PREEMPT);
3662 case PRO_PREEMPT_AND_ABORT:
3663 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3664 res_key, sa_res_key, PREEMPT_AND_ABORT);
3666 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3667 ret = core_scsi3_emulate_pro_register(cmd,
3668 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3670 case PRO_REGISTER_AND_MOVE:
3671 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3672 sa_res_key, aptpl, unreg);
3675 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3676 " action: 0x%02x\n", cdb[1] & 0x1f);
3677 return TCM_INVALID_CDB_FIELD;
3681 target_complete_cmd(cmd, GOOD);
3686 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3688 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3690 static sense_reason_t
3691 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3693 struct se_device *dev = cmd->se_dev;
3694 struct t10_pr_registration *pr_reg;
3696 u32 add_len = 0, off = 8;
3698 if (cmd->data_length < 8) {
3699 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3700 " too small\n", cmd->data_length);
3701 return TCM_INVALID_CDB_FIELD;
3704 buf = transport_kmap_data_sg(cmd);
3706 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3708 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3709 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3710 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3711 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3713 spin_lock(&dev->t10_pr.registration_lock);
3714 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3717 * Check for overflow of 8byte PRI READ_KEYS payload and
3718 * next reservation key list descriptor.
3720 if ((add_len + 8) > (cmd->data_length - 8))
3723 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3724 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3725 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3726 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3727 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3728 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3729 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3730 buf[off++] = (pr_reg->pr_res_key & 0xff);
3734 spin_unlock(&dev->t10_pr.registration_lock);
3736 buf[4] = ((add_len >> 24) & 0xff);
3737 buf[5] = ((add_len >> 16) & 0xff);
3738 buf[6] = ((add_len >> 8) & 0xff);
3739 buf[7] = (add_len & 0xff);
3741 transport_kunmap_data_sg(cmd);
3747 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3749 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3751 static sense_reason_t
3752 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3754 struct se_device *dev = cmd->se_dev;
3755 struct t10_pr_registration *pr_reg;
3758 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3760 if (cmd->data_length < 8) {
3761 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3762 " too small\n", cmd->data_length);
3763 return TCM_INVALID_CDB_FIELD;
3766 buf = transport_kmap_data_sg(cmd);
3768 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3770 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3771 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3772 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3773 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3775 spin_lock(&dev->dev_reservation_lock);
3776 pr_reg = dev->dev_pr_res_holder;
3779 * Set the hardcoded Additional Length
3781 buf[4] = ((add_len >> 24) & 0xff);
3782 buf[5] = ((add_len >> 16) & 0xff);
3783 buf[6] = ((add_len >> 8) & 0xff);
3784 buf[7] = (add_len & 0xff);
3786 if (cmd->data_length < 22)
3790 * Set the Reservation key.
3792 * From spc4r17, section 5.7.10:
3793 * A persistent reservation holder has its reservation key
3794 * returned in the parameter data from a PERSISTENT
3795 * RESERVE IN command with READ RESERVATION service action as
3797 * a) For a persistent reservation of the type Write Exclusive
3798 * - All Registrants or Exclusive Access  All Regitrants,
3799 * the reservation key shall be set to zero; or
3800 * b) For all other persistent reservation types, the
3801 * reservation key shall be set to the registered
3802 * reservation key for the I_T nexus that holds the
3803 * persistent reservation.
3805 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3806 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3809 pr_res_key = pr_reg->pr_res_key;
3811 buf[8] = ((pr_res_key >> 56) & 0xff);
3812 buf[9] = ((pr_res_key >> 48) & 0xff);
3813 buf[10] = ((pr_res_key >> 40) & 0xff);
3814 buf[11] = ((pr_res_key >> 32) & 0xff);
3815 buf[12] = ((pr_res_key >> 24) & 0xff);
3816 buf[13] = ((pr_res_key >> 16) & 0xff);
3817 buf[14] = ((pr_res_key >> 8) & 0xff);
3818 buf[15] = (pr_res_key & 0xff);
3820 * Set the SCOPE and TYPE
3822 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3823 (pr_reg->pr_res_type & 0x0f);
3827 spin_unlock(&dev->dev_reservation_lock);
3828 transport_kunmap_data_sg(cmd);
3834 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3836 * See spc4r17 section 6.13.4 Table 165
3838 static sense_reason_t
3839 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3841 struct se_device *dev = cmd->se_dev;
3842 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3844 u16 add_len = 8; /* Hardcoded to 8. */
3846 if (cmd->data_length < 6) {
3847 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3848 " %u too small\n", cmd->data_length);
3849 return TCM_INVALID_CDB_FIELD;
3852 buf = transport_kmap_data_sg(cmd);
3854 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3856 buf[0] = ((add_len >> 8) & 0xff);
3857 buf[1] = (add_len & 0xff);
3858 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3859 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3860 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3861 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3863 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3864 * set the TMV: Task Mask Valid bit.
3868 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3870 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3872 * PTPL_A: Persistence across Target Power Loss Active bit
3874 if (pr_tmpl->pr_aptpl_active)
3877 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3879 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3880 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3881 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3882 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3883 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3884 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3886 transport_kunmap_data_sg(cmd);
3892 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3894 * See spc4r17 section 6.13.5 Table 168 and 169
3896 static sense_reason_t
3897 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3899 struct se_device *dev = cmd->se_dev;
3900 struct se_node_acl *se_nacl;
3901 struct se_portal_group *se_tpg;
3902 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3903 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3905 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
3906 u32 off = 8; /* off into first Full Status descriptor */
3907 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3908 bool all_reg = false;
3910 if (cmd->data_length < 8) {
3911 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3912 " too small\n", cmd->data_length);
3913 return TCM_INVALID_CDB_FIELD;
3916 buf = transport_kmap_data_sg(cmd);
3918 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3920 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3921 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3922 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3923 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3925 spin_lock(&dev->dev_reservation_lock);
3926 if (dev->dev_pr_res_holder) {
3927 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3929 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3930 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3932 pr_res_type = pr_holder->pr_res_type;
3933 pr_res_scope = pr_holder->pr_res_scope;
3936 spin_unlock(&dev->dev_reservation_lock);
3938 spin_lock(&pr_tmpl->registration_lock);
3939 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3940 &pr_tmpl->registration_list, pr_reg_list) {
3942 se_nacl = pr_reg->pr_reg_nacl;
3943 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3946 atomic_inc_mb(&pr_reg->pr_res_holders);
3947 spin_unlock(&pr_tmpl->registration_lock);
3949 * Determine expected length of $FABRIC_MOD specific
3950 * TransportID full status descriptor..
3952 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
3953 se_tpg, se_nacl, pr_reg, &format_code);
3955 if ((exp_desc_len + add_len) > cmd->data_length) {
3956 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3957 " out of buffer: %d\n", cmd->data_length);
3958 spin_lock(&pr_tmpl->registration_lock);
3959 atomic_dec_mb(&pr_reg->pr_res_holders);
3963 * Set RESERVATION KEY
3965 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3966 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3967 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3968 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3969 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3970 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3971 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3972 buf[off++] = (pr_reg->pr_res_key & 0xff);
3973 off += 4; /* Skip Over Reserved area */
3976 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3978 if (pr_reg->pr_reg_all_tg_pt)
3981 * The struct se_lun pointer will be present for the
3982 * reservation holder for PR_HOLDER bit.
3984 * Also, if this registration is the reservation
3985 * holder or there is an All Registrants reservation
3986 * active, fill in SCOPE and TYPE in the next byte.
3988 if (pr_reg->pr_res_holder) {
3990 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3991 (pr_reg->pr_res_type & 0x0f);
3992 } else if (all_reg) {
3994 buf[off++] = (pr_res_scope & 0xf0) |
3995 (pr_res_type & 0x0f);
4000 off += 4; /* Skip over reserved area */
4002 * From spc4r17 6.3.15:
4004 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4005 * IDENTIFIER field contains the relative port identifier (see
4006 * 3.1.120) of the target port that is part of the I_T nexus
4007 * described by this full status descriptor. If the ALL_TG_PT
4008 * bit is set to one, the contents of the RELATIVE TARGET PORT
4009 * IDENTIFIER field are not defined by this standard.
4011 if (!pr_reg->pr_reg_all_tg_pt) {
4012 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4014 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4015 buf[off++] = (port->sep_rtpi & 0xff);
4017 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4020 * Now, have the $FABRIC_MOD fill in the protocol identifier
4022 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4023 se_nacl, pr_reg, &format_code, &buf[off+4]);
4025 spin_lock(&pr_tmpl->registration_lock);
4026 atomic_dec_mb(&pr_reg->pr_res_holders);
4028 * Set the ADDITIONAL DESCRIPTOR LENGTH
4030 buf[off++] = ((desc_len >> 24) & 0xff);
4031 buf[off++] = ((desc_len >> 16) & 0xff);
4032 buf[off++] = ((desc_len >> 8) & 0xff);
4033 buf[off++] = (desc_len & 0xff);
4035 * Size of full desctipor header minus TransportID
4036 * containing $FABRIC_MOD specific) initiator device/port
4039 * See spc4r17 Section 6.13.5 Table 169
4041 add_desc_len = (24 + desc_len);
4044 add_len += add_desc_len;
4046 spin_unlock(&pr_tmpl->registration_lock);
4048 * Set ADDITIONAL_LENGTH
4050 buf[4] = ((add_len >> 24) & 0xff);
4051 buf[5] = ((add_len >> 16) & 0xff);
4052 buf[6] = ((add_len >> 8) & 0xff);
4053 buf[7] = (add_len & 0xff);
4055 transport_kunmap_data_sg(cmd);
4061 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4066 * Following spc2r20 5.5.1 Reservations overview:
4068 * If a logical unit has been reserved by any RESERVE command and is
4069 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4070 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4071 * initiator or service action and shall terminate with a RESERVATION
4074 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4075 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4076 " SPC-2 reservation is held, returning"
4077 " RESERVATION_CONFLICT\n");
4078 return TCM_RESERVATION_CONFLICT;
4081 switch (cmd->t_task_cdb[1] & 0x1f) {
4083 ret = core_scsi3_pri_read_keys(cmd);
4085 case PRI_READ_RESERVATION:
4086 ret = core_scsi3_pri_read_reservation(cmd);
4088 case PRI_REPORT_CAPABILITIES:
4089 ret = core_scsi3_pri_report_capabilities(cmd);
4091 case PRI_READ_FULL_STATUS:
4092 ret = core_scsi3_pri_read_full_status(cmd);
4095 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4096 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4097 return TCM_INVALID_CDB_FIELD;
4101 target_complete_cmd(cmd, GOOD);
4106 target_check_reservation(struct se_cmd *cmd)
4108 struct se_device *dev = cmd->se_dev;
4113 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4115 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
4118 spin_lock(&dev->dev_reservation_lock);
4119 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4120 ret = target_scsi2_reservation_check(cmd);
4122 ret = target_scsi3_pr_reservation_check(cmd);
4123 spin_unlock(&dev->dev_reservation_lock);