1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@kernel.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ******************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_pr.h"
42 #include "target_core_ua.h"
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 struct pr_transport_id_holder {
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
56 int core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
61 if (!pr_reg->isid_present_at_reg)
64 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
68 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69 struct t10_pr_registration *, int);
71 static int core_scsi2_reservation_seq_non_holder(
88 static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
90 struct se_device *dev = cmd->se_dev;
91 struct se_session *sess = cmd->se_sess;
97 spin_lock(&dev->dev_reservation_lock);
98 if (!dev->dev_reserved_node_acl || !sess) {
99 spin_unlock(&dev->dev_reservation_lock);
102 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103 spin_unlock(&dev->dev_reservation_lock);
106 if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
107 spin_unlock(&dev->dev_reservation_lock);
110 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
111 spin_unlock(&dev->dev_reservation_lock);
116 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117 struct se_node_acl *, struct se_session *);
118 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
120 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
122 struct se_session *se_sess = cmd->se_sess;
123 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
124 struct t10_pr_registration *pr_reg;
125 struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
126 int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
132 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
156 if (pr_reg->pr_res_holder) {
157 core_scsi3_put_pr_reg(pr_reg);
160 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
161 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
162 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
163 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
164 core_scsi3_put_pr_reg(pr_reg);
167 core_scsi3_put_pr_reg(pr_reg);
171 * Following spc2r20 5.5.1 Reservations overview:
173 * If a logical unit has executed a PERSISTENT RESERVE OUT
174 * command with the REGISTER or the REGISTER AND IGNORE
175 * EXISTING KEY service action and is still registered by any
176 * initiator, all RESERVE commands and all RELEASE commands
177 * regardless of initiator shall conflict and shall terminate
178 * with a RESERVATION CONFLICT status.
180 spin_lock(&pr_tmpl->registration_lock);
181 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
182 spin_unlock(&pr_tmpl->registration_lock);
186 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
187 " while active SPC-3 registrations exist,"
188 " returning RESERVATION_CONFLICT\n");
189 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
196 int target_scsi2_reservation_release(struct se_task *task)
198 struct se_cmd *cmd = task->task_se_cmd;
199 struct se_device *dev = cmd->se_dev;
200 struct se_session *sess = cmd->se_sess;
201 struct se_portal_group *tpg = sess->se_tpg;
206 rc = target_check_scsi2_reservation_conflict(cmd);
210 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
216 spin_lock(&dev->dev_reservation_lock);
217 if (!dev->dev_reserved_node_acl || !sess)
220 if (dev->dev_reserved_node_acl != sess->se_node_acl)
223 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
226 dev->dev_reserved_node_acl = NULL;
227 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
228 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
229 dev->dev_res_bin_isid = 0;
230 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
232 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
233 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
234 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
235 sess->se_node_acl->initiatorname);
238 spin_unlock(&dev->dev_reservation_lock);
241 task->task_scsi_status = GOOD;
242 transport_complete_task(task, 1);
247 int target_scsi2_reservation_reserve(struct se_task *task)
249 struct se_cmd *cmd = task->task_se_cmd;
250 struct se_device *dev = cmd->se_dev;
251 struct se_session *sess = cmd->se_sess;
252 struct se_portal_group *tpg = sess->se_tpg;
255 if ((cmd->t_task_cdb[1] & 0x01) &&
256 (cmd->t_task_cdb[1] & 0x02)) {
257 pr_err("LongIO and Obselete Bits set, returning"
258 " ILLEGAL_REQUEST\n");
259 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
264 * This is currently the case for target_core_mod passthrough struct se_cmd
269 rc = target_check_scsi2_reservation_conflict(cmd);
273 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
279 spin_lock(&dev->dev_reservation_lock);
280 if (dev->dev_reserved_node_acl &&
281 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
282 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
283 tpg->se_tpg_tfo->get_fabric_name());
284 pr_err("Original reserver LUN: %u %s\n",
285 cmd->se_lun->unpacked_lun,
286 dev->dev_reserved_node_acl->initiatorname);
287 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
288 " from %s \n", cmd->se_lun->unpacked_lun,
289 cmd->se_deve->mapped_lun,
290 sess->se_node_acl->initiatorname);
291 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
296 dev->dev_reserved_node_acl = sess->se_node_acl;
297 dev->dev_flags |= DF_SPC2_RESERVATIONS;
298 if (sess->sess_bin_isid != 0) {
299 dev->dev_res_bin_isid = sess->sess_bin_isid;
300 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
302 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
303 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
304 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
305 sess->se_node_acl->initiatorname);
308 spin_unlock(&dev->dev_reservation_lock);
311 task->task_scsi_status = GOOD;
312 transport_complete_task(task, 1);
319 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
321 * This function is called by those initiator ports who are *NOT*
322 * the active PR reservation holder when a reservation is present.
324 static int core_scsi3_pr_seq_non_holder(
329 struct se_dev_entry *se_deve;
330 struct se_session *se_sess = cmd->se_sess;
331 int other_cdb = 0, ignore_reg;
332 int registered_nexus = 0, ret = 1; /* Conflict by default */
333 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
334 int we = 0; /* Write Exclusive */
335 int legacy = 0; /* Act like a legacy device and return
336 * RESERVATION CONFLICT on some CDBs */
338 * A legacy SPC-2 reservation is being held.
340 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
341 return core_scsi2_reservation_seq_non_holder(cmd,
344 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
346 * Determine if the registration should be ignored due to
347 * non-matching ISIDs in core_scsi3_pr_reservation_check().
349 ignore_reg = (pr_reg_type & 0x80000000);
351 pr_reg_type &= ~0x80000000;
353 switch (pr_reg_type) {
354 case PR_TYPE_WRITE_EXCLUSIVE:
356 case PR_TYPE_EXCLUSIVE_ACCESS:
358 * Some commands are only allowed for the persistent reservation
361 if ((se_deve->def_pr_registered) && !(ignore_reg))
362 registered_nexus = 1;
364 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
366 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
368 * Some commands are only allowed for registered I_T Nexuses.
371 if ((se_deve->def_pr_registered) && !(ignore_reg))
372 registered_nexus = 1;
374 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
376 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
378 * Each registered I_T Nexus is a reservation holder.
381 if ((se_deve->def_pr_registered) && !(ignore_reg))
382 registered_nexus = 1;
388 * Referenced from spc4r17 table 45 for *NON* PR holder access
391 case SECURITY_PROTOCOL_IN:
392 if (registered_nexus)
400 case RECEIVE_DIAGNOSTIC:
405 if (registered_nexus) {
409 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
411 case PERSISTENT_RESERVE_OUT:
413 * This follows PERSISTENT_RESERVE_OUT service actions that
414 * are allowed in the presence of various reservations.
415 * See spc4r17, table 46
417 switch (cdb[1] & 0x1f) {
420 case PRO_PREEMPT_AND_ABORT:
421 ret = (registered_nexus) ? 0 : 1;
424 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
427 case PRO_REGISTER_AND_MOVE:
432 ret = (registered_nexus) ? 0 : 1;
435 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
436 " action: 0x%02x\n", cdb[1] & 0x1f);
442 /* Handled by CRH=1 in target_scsi2_reservation_release() */
447 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
450 case TEST_UNIT_READY:
451 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
454 switch (cdb[1] & 0x1f) {
455 case MI_MANAGEMENT_PROTOCOL_IN:
456 if (registered_nexus) {
460 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
462 case MI_REPORT_SUPPORTED_OPERATION_CODES:
463 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
468 if (registered_nexus) {
472 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
474 case MI_REPORT_ALIASES:
475 case MI_REPORT_IDENTIFYING_INFORMATION:
476 case MI_REPORT_PRIORITY:
477 case MI_REPORT_TARGET_PGS:
478 case MI_REPORT_TIMESTAMP:
479 ret = 0; /* Allowed */
482 pr_err("Unknown MI Service Action: 0x%02x\n",
487 case ACCESS_CONTROL_IN:
488 case ACCESS_CONTROL_OUT:
491 case READ_MEDIA_SERIAL_NUMBER:
494 case PERSISTENT_RESERVE_IN:
495 ret = 0; /*/ Allowed CDBs */
502 * Case where the CDB is explicitly allowed in the above switch
505 if (!ret && !other_cdb) {
507 pr_debug("Allowing explict CDB: 0x%02x for %s"
508 " reservation holder\n", cdb[0],
509 core_scsi3_pr_dump_type(pr_reg_type));
514 * Check if write exclusive initiator ports *NOT* holding the
515 * WRITE_EXCLUSIVE_* reservation.
517 if ((we) && !(registered_nexus)) {
518 if (cmd->data_direction == DMA_TO_DEVICE) {
520 * Conflict for write exclusive
522 pr_debug("%s Conflict for unregistered nexus"
523 " %s CDB: 0x%02x to %s reservation\n",
524 transport_dump_cmd_direction(cmd),
525 se_sess->se_node_acl->initiatorname, cdb[0],
526 core_scsi3_pr_dump_type(pr_reg_type));
530 * Allow non WRITE CDBs for all Write Exclusive
531 * PR TYPEs to pass for registered and
532 * non-registered_nexuxes NOT holding the reservation.
534 * We only make noise for the unregisterd nexuses,
535 * as we expect registered non-reservation holding
536 * nexuses to issue CDBs.
539 if (!registered_nexus) {
540 pr_debug("Allowing implict CDB: 0x%02x"
541 " for %s reservation on unregistered"
543 core_scsi3_pr_dump_type(pr_reg_type));
548 } else if ((reg_only) || (all_reg)) {
549 if (registered_nexus) {
551 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
552 * allow commands from registered nexuses.
555 pr_debug("Allowing implict CDB: 0x%02x for %s"
556 " reservation\n", cdb[0],
557 core_scsi3_pr_dump_type(pr_reg_type));
562 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
563 " for %s reservation\n", transport_dump_cmd_direction(cmd),
564 (registered_nexus) ? "" : "un",
565 se_sess->se_node_acl->initiatorname, cdb[0],
566 core_scsi3_pr_dump_type(pr_reg_type));
568 return 1; /* Conflict by default */
571 static u32 core_scsi3_pr_generation(struct se_device *dev)
573 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
576 * PRGeneration field shall contain the value of a 32-bit wrapping
577 * counter mainted by the device server.
579 * Note that this is done regardless of Active Persist across
580 * Target PowerLoss (APTPL)
582 * See spc4r17 section 6.3.12 READ_KEYS service action
584 spin_lock(&dev->dev_reservation_lock);
585 prg = su_dev->t10_pr.pr_generation++;
586 spin_unlock(&dev->dev_reservation_lock);
591 static int core_scsi3_pr_reservation_check(
595 struct se_device *dev = cmd->se_dev;
596 struct se_session *sess = cmd->se_sess;
602 * A legacy SPC-2 reservation is being held.
604 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
605 return core_scsi2_reservation_check(cmd, pr_reg_type);
607 spin_lock(&dev->dev_reservation_lock);
608 if (!dev->dev_pr_res_holder) {
609 spin_unlock(&dev->dev_reservation_lock);
612 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
613 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
614 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
615 spin_unlock(&dev->dev_reservation_lock);
618 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
619 spin_unlock(&dev->dev_reservation_lock);
622 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
623 sess->sess_bin_isid) ? 0 : -EINVAL;
625 * Use bit in *pr_reg_type to notify ISID mismatch in
626 * core_scsi3_pr_seq_non_holder().
629 *pr_reg_type |= 0x80000000;
630 spin_unlock(&dev->dev_reservation_lock);
635 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
636 struct se_device *dev,
637 struct se_node_acl *nacl,
638 struct se_dev_entry *deve,
644 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
645 struct t10_pr_registration *pr_reg;
647 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
649 pr_err("Unable to allocate struct t10_pr_registration\n");
653 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
655 if (!pr_reg->pr_aptpl_buf) {
656 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
657 kmem_cache_free(t10_pr_reg_cache, pr_reg);
661 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
662 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
663 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
664 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
665 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
666 atomic_set(&pr_reg->pr_res_holders, 0);
667 pr_reg->pr_reg_nacl = nacl;
668 pr_reg->pr_reg_deve = deve;
669 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
670 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
671 pr_reg->pr_res_key = sa_res_key;
672 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
673 pr_reg->pr_reg_aptpl = aptpl;
674 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
676 * If an ISID value for this SCSI Initiator Port exists,
677 * save it to the registration now.
680 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
681 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
682 pr_reg->isid_present_at_reg = 1;
688 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
689 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
692 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
695 static struct t10_pr_registration *__core_scsi3_alloc_registration(
696 struct se_device *dev,
697 struct se_node_acl *nacl,
698 struct se_dev_entry *deve,
704 struct se_dev_entry *deve_tmp;
705 struct se_node_acl *nacl_tmp;
706 struct se_port *port, *port_tmp;
707 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
708 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
711 * Create a registration for the I_T Nexus upon which the
712 * PROUT REGISTER was received.
714 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
715 sa_res_key, all_tg_pt, aptpl);
719 * Return pointer to pr_reg for ALL_TG_PT=0
724 * Create list of matching SCSI Initiator Port registrations
727 spin_lock(&dev->se_port_lock);
728 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
729 atomic_inc(&port->sep_tg_pt_ref_cnt);
730 smp_mb__after_atomic_inc();
731 spin_unlock(&dev->se_port_lock);
733 spin_lock_bh(&port->sep_alua_lock);
734 list_for_each_entry(deve_tmp, &port->sep_alua_list,
737 * This pointer will be NULL for demo mode MappedLUNs
738 * that have not been make explict via a ConfigFS
739 * MappedLUN group for the SCSI Initiator Node ACL.
741 if (!deve_tmp->se_lun_acl)
744 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
746 * Skip the matching struct se_node_acl that is allocated
749 if (nacl == nacl_tmp)
752 * Only perform PR registrations for target ports on
753 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
756 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
759 * Look for a matching Initiator Node ACL in ASCII format
761 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
764 atomic_inc(&deve_tmp->pr_ref_count);
765 smp_mb__after_atomic_inc();
766 spin_unlock_bh(&port->sep_alua_lock);
768 * Grab a configfs group dependency that is released
769 * for the exception path at label out: below, or upon
770 * completion of adding ALL_TG_PT=1 registrations in
771 * __core_scsi3_add_registration()
773 ret = core_scsi3_lunacl_depend_item(deve_tmp);
775 pr_err("core_scsi3_lunacl_depend"
777 atomic_dec(&port->sep_tg_pt_ref_cnt);
778 smp_mb__after_atomic_dec();
779 atomic_dec(&deve_tmp->pr_ref_count);
780 smp_mb__after_atomic_dec();
784 * Located a matching SCSI Initiator Port on a different
785 * port, allocate the pr_reg_atp and attach it to the
786 * pr_reg->pr_reg_atp_list that will be processed once
787 * the original *pr_reg is processed in
788 * __core_scsi3_add_registration()
790 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
791 nacl_tmp, deve_tmp, NULL,
792 sa_res_key, all_tg_pt, aptpl);
794 atomic_dec(&port->sep_tg_pt_ref_cnt);
795 smp_mb__after_atomic_dec();
796 atomic_dec(&deve_tmp->pr_ref_count);
797 smp_mb__after_atomic_dec();
798 core_scsi3_lunacl_undepend_item(deve_tmp);
802 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
803 &pr_reg->pr_reg_atp_list);
804 spin_lock_bh(&port->sep_alua_lock);
806 spin_unlock_bh(&port->sep_alua_lock);
808 spin_lock(&dev->se_port_lock);
809 atomic_dec(&port->sep_tg_pt_ref_cnt);
810 smp_mb__after_atomic_dec();
812 spin_unlock(&dev->se_port_lock);
816 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
817 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
818 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
819 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
820 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
822 kmem_cache_free(t10_pr_reg_cache, pr_reg);
826 int core_scsi3_alloc_aptpl_registration(
827 struct t10_reservation *pr_tmpl,
829 unsigned char *i_port,
832 unsigned char *t_port,
839 struct t10_pr_registration *pr_reg;
841 if (!i_port || !t_port || !sa_res_key) {
842 pr_err("Illegal parameters for APTPL registration\n");
846 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
848 pr_err("Unable to allocate struct t10_pr_registration\n");
851 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
853 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
854 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
855 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
856 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
857 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
858 atomic_set(&pr_reg->pr_res_holders, 0);
859 pr_reg->pr_reg_nacl = NULL;
860 pr_reg->pr_reg_deve = NULL;
861 pr_reg->pr_res_mapped_lun = mapped_lun;
862 pr_reg->pr_aptpl_target_lun = target_lun;
863 pr_reg->pr_res_key = sa_res_key;
864 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
865 pr_reg->pr_reg_aptpl = 1;
866 pr_reg->pr_reg_tg_pt_lun = NULL;
867 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
868 pr_reg->pr_res_type = type;
870 * If an ISID value had been saved in APTPL metadata for this
871 * SCSI Initiator Port, restore it now.
874 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
875 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
876 pr_reg->isid_present_at_reg = 1;
879 * Copy the i_port and t_port information from caller.
881 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
882 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
883 pr_reg->pr_reg_tpgt = tpgt;
885 * Set pr_res_holder from caller, the pr_reg who is the reservation
886 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
887 * the Initiator Node LUN ACL from the fabric module is created for
890 pr_reg->pr_res_holder = res_holder;
892 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
893 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
894 " metadata\n", (res_holder) ? "+reservation" : "");
898 static void core_scsi3_aptpl_reserve(
899 struct se_device *dev,
900 struct se_portal_group *tpg,
901 struct se_node_acl *node_acl,
902 struct t10_pr_registration *pr_reg)
904 char i_buf[PR_REG_ISID_ID_LEN];
907 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
908 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
911 spin_lock(&dev->dev_reservation_lock);
912 dev->dev_pr_res_holder = pr_reg;
913 spin_unlock(&dev->dev_reservation_lock);
915 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
916 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
917 tpg->se_tpg_tfo->get_fabric_name(),
918 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
919 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
920 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
921 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
922 (prf_isid) ? &i_buf[0] : "");
925 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
926 struct t10_pr_registration *, int, int);
928 static int __core_scsi3_check_aptpl_registration(
929 struct se_device *dev,
930 struct se_portal_group *tpg,
933 struct se_node_acl *nacl,
934 struct se_dev_entry *deve)
936 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
937 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
938 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
939 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
942 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
943 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
945 * Copy Initiator Port information from struct se_node_acl
947 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
948 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
949 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
950 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
952 * Look for the matching registrations+reservation from those
953 * created from APTPL metadata. Note that multiple registrations
954 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
957 spin_lock(&pr_tmpl->aptpl_reg_lock);
958 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
960 if (!strcmp(pr_reg->pr_iport, i_port) &&
961 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
962 !(strcmp(pr_reg->pr_tport, t_port)) &&
963 (pr_reg->pr_reg_tpgt == tpgt) &&
964 (pr_reg->pr_aptpl_target_lun == target_lun)) {
966 pr_reg->pr_reg_nacl = nacl;
967 pr_reg->pr_reg_deve = deve;
968 pr_reg->pr_reg_tg_pt_lun = lun;
970 list_del(&pr_reg->pr_reg_aptpl_list);
971 spin_unlock(&pr_tmpl->aptpl_reg_lock);
973 * At this point all of the pointers in *pr_reg will
974 * be setup, so go ahead and add the registration.
977 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
979 * If this registration is the reservation holder,
980 * make that happen now..
982 if (pr_reg->pr_res_holder)
983 core_scsi3_aptpl_reserve(dev, tpg,
986 * Reenable pr_aptpl_active to accept new metadata
987 * updates once the SCSI device is active again..
989 spin_lock(&pr_tmpl->aptpl_reg_lock);
990 pr_tmpl->pr_aptpl_active = 1;
993 spin_unlock(&pr_tmpl->aptpl_reg_lock);
998 int core_scsi3_check_aptpl_registration(
999 struct se_device *dev,
1000 struct se_portal_group *tpg,
1002 struct se_lun_acl *lun_acl)
1004 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1005 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
1006 struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
1008 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1011 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1012 lun->unpacked_lun, nacl, deve);
1015 static void __core_scsi3_dump_registration(
1016 struct target_core_fabric_ops *tfo,
1017 struct se_device *dev,
1018 struct se_node_acl *nacl,
1019 struct t10_pr_registration *pr_reg,
1022 struct se_portal_group *se_tpg = nacl->se_tpg;
1023 char i_buf[PR_REG_ISID_ID_LEN];
1026 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1027 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1028 PR_REG_ISID_ID_LEN);
1030 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1031 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1032 "_AND_MOVE" : (register_type == 1) ?
1033 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1034 (prf_isid) ? i_buf : "");
1035 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1036 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1037 tfo->tpg_get_tag(se_tpg));
1038 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1039 " Port(s)\n", tfo->get_fabric_name(),
1040 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1041 dev->transport->name);
1042 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1043 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1044 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1045 pr_reg->pr_reg_aptpl);
1049 * this function can be called with struct se_device->dev_reservation_lock
1050 * when register_move = 1
1052 static void __core_scsi3_add_registration(
1053 struct se_device *dev,
1054 struct se_node_acl *nacl,
1055 struct t10_pr_registration *pr_reg,
1059 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1060 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1061 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1062 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1065 * Increment PRgeneration counter for struct se_device upon a successful
1066 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1068 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1069 * action, the struct se_device->dev_reservation_lock will already be held,
1070 * so we do not call core_scsi3_pr_generation() which grabs the lock
1073 pr_reg->pr_res_generation = (register_move) ?
1074 su_dev->t10_pr.pr_generation++ :
1075 core_scsi3_pr_generation(dev);
1077 spin_lock(&pr_tmpl->registration_lock);
1078 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1079 pr_reg->pr_reg_deve->def_pr_registered = 1;
1081 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1082 spin_unlock(&pr_tmpl->registration_lock);
1084 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1086 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1089 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1090 * allocated in __core_scsi3_alloc_registration()
1092 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1093 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1094 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1096 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1098 spin_lock(&pr_tmpl->registration_lock);
1099 list_add_tail(&pr_reg_tmp->pr_reg_list,
1100 &pr_tmpl->registration_list);
1101 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1103 __core_scsi3_dump_registration(tfo, dev,
1104 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1106 spin_unlock(&pr_tmpl->registration_lock);
1108 * Drop configfs group dependency reference from
1109 * __core_scsi3_alloc_registration()
1111 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1115 static int core_scsi3_alloc_registration(
1116 struct se_device *dev,
1117 struct se_node_acl *nacl,
1118 struct se_dev_entry *deve,
1119 unsigned char *isid,
1126 struct t10_pr_registration *pr_reg;
1128 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1129 sa_res_key, all_tg_pt, aptpl);
1133 __core_scsi3_add_registration(dev, nacl, pr_reg,
1134 register_type, register_move);
1138 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1139 struct se_device *dev,
1140 struct se_node_acl *nacl,
1141 unsigned char *isid)
1143 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1144 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1145 struct se_portal_group *tpg;
1147 spin_lock(&pr_tmpl->registration_lock);
1148 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1149 &pr_tmpl->registration_list, pr_reg_list) {
1151 * First look for a matching struct se_node_acl
1153 if (pr_reg->pr_reg_nacl != nacl)
1156 tpg = pr_reg->pr_reg_nacl->se_tpg;
1158 * If this registration does NOT contain a fabric provided
1159 * ISID, then we have found a match.
1161 if (!pr_reg->isid_present_at_reg) {
1163 * Determine if this SCSI device server requires that
1164 * SCSI Intiatior TransportID w/ ISIDs is enforced
1165 * for fabric modules (iSCSI) requiring them.
1167 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1168 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
1171 atomic_inc(&pr_reg->pr_res_holders);
1172 smp_mb__after_atomic_inc();
1173 spin_unlock(&pr_tmpl->registration_lock);
1177 * If the *pr_reg contains a fabric defined ISID for multi-value
1178 * SCSI Initiator Port TransportIDs, then we expect a valid
1179 * matching ISID to be provided by the local SCSI Initiator Port.
1183 if (strcmp(isid, pr_reg->pr_reg_isid))
1186 atomic_inc(&pr_reg->pr_res_holders);
1187 smp_mb__after_atomic_inc();
1188 spin_unlock(&pr_tmpl->registration_lock);
1191 spin_unlock(&pr_tmpl->registration_lock);
1196 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1197 struct se_device *dev,
1198 struct se_node_acl *nacl,
1199 struct se_session *sess)
1201 struct se_portal_group *tpg = nacl->se_tpg;
1202 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1204 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1205 memset(&buf[0], 0, PR_REG_ISID_LEN);
1206 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1211 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1214 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1216 atomic_dec(&pr_reg->pr_res_holders);
1217 smp_mb__after_atomic_dec();
1220 static int core_scsi3_check_implict_release(
1221 struct se_device *dev,
1222 struct t10_pr_registration *pr_reg)
1224 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1225 struct t10_pr_registration *pr_res_holder;
1228 spin_lock(&dev->dev_reservation_lock);
1229 pr_res_holder = dev->dev_pr_res_holder;
1230 if (!pr_res_holder) {
1231 spin_unlock(&dev->dev_reservation_lock);
1234 if (pr_res_holder == pr_reg) {
1236 * Perform an implict RELEASE if the registration that
1237 * is being released is holding the reservation.
1239 * From spc4r17, section 5.7.11.1:
1241 * e) If the I_T nexus is the persistent reservation holder
1242 * and the persistent reservation is not an all registrants
1243 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1244 * service action or REGISTER AND IGNORE EXISTING KEY
1245 * service action with the SERVICE ACTION RESERVATION KEY
1246 * field set to zero (see 5.7.11.3).
1248 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1251 * For 'All Registrants' reservation types, all existing
1252 * registrations are still processed as reservation holders
1253 * in core_scsi3_pr_seq_non_holder() after the initial
1254 * reservation holder is implictly released here.
1256 } else if (pr_reg->pr_reg_all_tg_pt &&
1257 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1258 pr_reg->pr_reg_nacl->initiatorname)) &&
1259 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1260 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1261 " UNREGISTER while existing reservation with matching"
1262 " key 0x%016Lx is present from another SCSI Initiator"
1263 " Port\n", pr_reg->pr_res_key);
1266 spin_unlock(&dev->dev_reservation_lock);
1272 * Called with struct t10_reservation->registration_lock held.
1274 static void __core_scsi3_free_registration(
1275 struct se_device *dev,
1276 struct t10_pr_registration *pr_reg,
1277 struct list_head *preempt_and_abort_list,
1280 struct target_core_fabric_ops *tfo =
1281 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1282 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1283 char i_buf[PR_REG_ISID_ID_LEN];
1286 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1287 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1288 PR_REG_ISID_ID_LEN);
1290 pr_reg->pr_reg_deve->def_pr_registered = 0;
1291 pr_reg->pr_reg_deve->pr_res_key = 0;
1292 list_del(&pr_reg->pr_reg_list);
1294 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1295 * so call core_scsi3_put_pr_reg() to decrement our reference.
1298 core_scsi3_put_pr_reg(pr_reg);
1300 * Wait until all reference from any other I_T nexuses for this
1301 * *pr_reg have been released. Because list_del() is called above,
1302 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1303 * count back to zero, and we release *pr_reg.
1305 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1306 spin_unlock(&pr_tmpl->registration_lock);
1307 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1308 tfo->get_fabric_name());
1310 spin_lock(&pr_tmpl->registration_lock);
1313 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1314 " Node: %s%s\n", tfo->get_fabric_name(),
1315 pr_reg->pr_reg_nacl->initiatorname,
1316 (prf_isid) ? &i_buf[0] : "");
1317 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1318 " Port(s)\n", tfo->get_fabric_name(),
1319 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1320 dev->transport->name);
1321 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1322 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1323 pr_reg->pr_res_generation);
1325 if (!preempt_and_abort_list) {
1326 pr_reg->pr_reg_deve = NULL;
1327 pr_reg->pr_reg_nacl = NULL;
1328 kfree(pr_reg->pr_aptpl_buf);
1329 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1333 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1334 * are released once the ABORT_TASK_SET has completed..
1336 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1339 void core_scsi3_free_pr_reg_from_nacl(
1340 struct se_device *dev,
1341 struct se_node_acl *nacl)
1343 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1344 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1346 * If the passed se_node_acl matches the reservation holder,
1347 * release the reservation.
1349 spin_lock(&dev->dev_reservation_lock);
1350 pr_res_holder = dev->dev_pr_res_holder;
1351 if ((pr_res_holder != NULL) &&
1352 (pr_res_holder->pr_reg_nacl == nacl))
1353 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1354 spin_unlock(&dev->dev_reservation_lock);
1356 * Release any registration associated with the struct se_node_acl.
1358 spin_lock(&pr_tmpl->registration_lock);
1359 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1360 &pr_tmpl->registration_list, pr_reg_list) {
1362 if (pr_reg->pr_reg_nacl != nacl)
1365 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1367 spin_unlock(&pr_tmpl->registration_lock);
1370 void core_scsi3_free_all_registrations(
1371 struct se_device *dev)
1373 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1374 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1376 spin_lock(&dev->dev_reservation_lock);
1377 pr_res_holder = dev->dev_pr_res_holder;
1378 if (pr_res_holder != NULL) {
1379 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1380 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1383 spin_unlock(&dev->dev_reservation_lock);
1385 spin_lock(&pr_tmpl->registration_lock);
1386 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1387 &pr_tmpl->registration_list, pr_reg_list) {
1389 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1391 spin_unlock(&pr_tmpl->registration_lock);
1393 spin_lock(&pr_tmpl->aptpl_reg_lock);
1394 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1395 pr_reg_aptpl_list) {
1396 list_del(&pr_reg->pr_reg_aptpl_list);
1397 kfree(pr_reg->pr_aptpl_buf);
1398 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1400 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1403 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1405 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1406 &tpg->tpg_group.cg_item);
1409 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1411 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1412 &tpg->tpg_group.cg_item);
1414 atomic_dec(&tpg->tpg_pr_ref_count);
1415 smp_mb__after_atomic_dec();
1418 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1420 struct se_portal_group *tpg = nacl->se_tpg;
1422 if (nacl->dynamic_node_acl)
1425 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1426 &nacl->acl_group.cg_item);
1429 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1431 struct se_portal_group *tpg = nacl->se_tpg;
1433 if (nacl->dynamic_node_acl) {
1434 atomic_dec(&nacl->acl_pr_ref_count);
1435 smp_mb__after_atomic_dec();
1439 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1440 &nacl->acl_group.cg_item);
1442 atomic_dec(&nacl->acl_pr_ref_count);
1443 smp_mb__after_atomic_dec();
1446 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1448 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1449 struct se_node_acl *nacl;
1450 struct se_portal_group *tpg;
1452 * For nacl->dynamic_node_acl=1
1457 nacl = lun_acl->se_lun_nacl;
1460 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1461 &lun_acl->se_lun_group.cg_item);
1464 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1466 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1467 struct se_node_acl *nacl;
1468 struct se_portal_group *tpg;
1470 * For nacl->dynamic_node_acl=1
1473 atomic_dec(&se_deve->pr_ref_count);
1474 smp_mb__after_atomic_dec();
1477 nacl = lun_acl->se_lun_nacl;
1480 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1481 &lun_acl->se_lun_group.cg_item);
1483 atomic_dec(&se_deve->pr_ref_count);
1484 smp_mb__after_atomic_dec();
1487 static int core_scsi3_decode_spec_i_port(
1489 struct se_portal_group *tpg,
1490 unsigned char *l_isid,
1495 struct se_device *dev = cmd->se_dev;
1496 struct se_port *tmp_port;
1497 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1498 struct se_session *se_sess = cmd->se_sess;
1499 struct se_node_acl *dest_node_acl = NULL;
1500 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1501 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1502 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1503 LIST_HEAD(tid_dest_list);
1504 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1505 struct target_core_fabric_ops *tmp_tf_ops;
1507 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1508 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1509 u32 tpdl, tid_len = 0;
1510 int ret, dest_local_nexus, prf_isid;
1513 memset(dest_iport, 0, 64);
1515 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1517 * Allocate a struct pr_transport_id_holder and setup the
1518 * local_node_acl and local_se_deve pointers and add to
1519 * struct list_head tid_dest_list for add registration
1520 * processing in the loop of tid_dest_list below.
1522 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1524 pr_err("Unable to allocate tidh_new\n");
1525 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1528 INIT_LIST_HEAD(&tidh_new->dest_list);
1529 tidh_new->dest_tpg = tpg;
1530 tidh_new->dest_node_acl = se_sess->se_node_acl;
1531 tidh_new->dest_se_deve = local_se_deve;
1533 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1534 se_sess->se_node_acl, local_se_deve, l_isid,
1535 sa_res_key, all_tg_pt, aptpl);
1536 if (!local_pr_reg) {
1538 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1541 tidh_new->dest_pr_reg = local_pr_reg;
1543 * The local I_T nexus does not hold any configfs dependances,
1544 * so we set tid_h->dest_local_nexus=1 to prevent the
1545 * configfs_undepend_item() calls in the tid_dest_list loops below.
1547 tidh_new->dest_local_nexus = 1;
1548 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1550 buf = transport_kmap_data_sg(cmd);
1552 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1553 * first extract TransportID Parameter Data Length, and make sure
1554 * the value matches up to the SCSI expected data transfer length.
1556 tpdl = (buf[24] & 0xff) << 24;
1557 tpdl |= (buf[25] & 0xff) << 16;
1558 tpdl |= (buf[26] & 0xff) << 8;
1559 tpdl |= buf[27] & 0xff;
1561 if ((tpdl + 28) != cmd->data_length) {
1562 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1563 " does not equal CDB data_length: %u\n", tpdl,
1565 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1570 * Start processing the received transport IDs using the
1571 * receiving I_T Nexus portal's fabric dependent methods to
1572 * obtain the SCSI Initiator Port/Device Identifiers.
1577 proto_ident = (ptr[0] & 0x0f);
1580 spin_lock(&dev->se_port_lock);
1581 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1582 tmp_tpg = tmp_port->sep_tpg;
1585 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1588 if (!tmp_tf_ops->get_fabric_proto_ident ||
1589 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1592 * Look for the matching proto_ident provided by
1593 * the received TransportID
1595 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1596 if (tmp_proto_ident != proto_ident)
1598 dest_rtpi = tmp_port->sep_rtpi;
1600 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1601 tmp_tpg, (const char *)ptr, &tid_len,
1606 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1607 smp_mb__after_atomic_inc();
1608 spin_unlock(&dev->se_port_lock);
1610 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1612 pr_err(" core_scsi3_tpg_depend_item()"
1614 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1615 smp_mb__after_atomic_dec();
1616 cmd->scsi_sense_reason =
1617 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1622 * Locate the desination initiator ACL to be registered
1623 * from the decoded fabric module specific TransportID
1626 spin_lock_irq(&tmp_tpg->acl_node_lock);
1627 dest_node_acl = __core_tpg_get_initiator_node_acl(
1629 if (dest_node_acl) {
1630 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1631 smp_mb__after_atomic_inc();
1633 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1635 if (!dest_node_acl) {
1636 core_scsi3_tpg_undepend_item(tmp_tpg);
1637 spin_lock(&dev->se_port_lock);
1641 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1643 pr_err("configfs_depend_item() failed"
1644 " for dest_node_acl->acl_group\n");
1645 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1646 smp_mb__after_atomic_dec();
1647 core_scsi3_tpg_undepend_item(tmp_tpg);
1648 cmd->scsi_sense_reason =
1649 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1655 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1656 " %s Port RTPI: %hu\n",
1657 dest_tpg->se_tpg_tfo->get_fabric_name(),
1658 dest_node_acl->initiatorname, dest_rtpi);
1660 spin_lock(&dev->se_port_lock);
1663 spin_unlock(&dev->se_port_lock);
1666 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1668 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1673 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1674 " tid_len: %d for %s + %s\n",
1675 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1676 tpdl, tid_len, i_str, iport_ptr);
1678 if (tid_len > tpdl) {
1679 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1680 " %u for Transport ID: %s\n", tid_len, ptr);
1681 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1682 core_scsi3_tpg_undepend_item(dest_tpg);
1683 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1688 * Locate the desintation struct se_dev_entry pointer for matching
1689 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1692 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1694 if (!dest_se_deve) {
1695 pr_err("Unable to locate %s dest_se_deve"
1696 " from destination RTPI: %hu\n",
1697 dest_tpg->se_tpg_tfo->get_fabric_name(),
1700 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1701 core_scsi3_tpg_undepend_item(dest_tpg);
1702 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1707 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1709 pr_err("core_scsi3_lunacl_depend_item()"
1711 atomic_dec(&dest_se_deve->pr_ref_count);
1712 smp_mb__after_atomic_dec();
1713 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1714 core_scsi3_tpg_undepend_item(dest_tpg);
1715 cmd->scsi_sense_reason =
1716 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1721 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1722 " dest_se_deve mapped_lun: %u\n",
1723 dest_tpg->se_tpg_tfo->get_fabric_name(),
1724 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1727 * Skip any TransportIDs that already have a registration for
1730 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1733 core_scsi3_put_pr_reg(pr_reg_e);
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);
1743 * Allocate a struct pr_transport_id_holder and setup
1744 * the dest_node_acl and dest_se_deve pointers for the
1747 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1750 pr_err("Unable to allocate tidh_new\n");
1751 core_scsi3_lunacl_undepend_item(dest_se_deve);
1752 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1753 core_scsi3_tpg_undepend_item(dest_tpg);
1754 cmd->scsi_sense_reason =
1755 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1759 INIT_LIST_HEAD(&tidh_new->dest_list);
1760 tidh_new->dest_tpg = dest_tpg;
1761 tidh_new->dest_node_acl = dest_node_acl;
1762 tidh_new->dest_se_deve = dest_se_deve;
1765 * Allocate, but do NOT add the registration for the
1766 * TransportID referenced SCSI Initiator port. This
1767 * done because of the following from spc4r17 in section
1768 * 6.14.3 wrt SPEC_I_PT:
1770 * "If a registration fails for any initiator port (e.g., if th
1771 * logical unit does not have enough resources available to
1772 * hold the registration information), no registrations shall be
1773 * made, and the command shall be terminated with
1774 * CHECK CONDITION status."
1776 * That means we call __core_scsi3_alloc_registration() here,
1777 * and then call __core_scsi3_add_registration() in the
1778 * 2nd loop which will never fail.
1780 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1781 dest_node_acl, dest_se_deve, iport_ptr,
1782 sa_res_key, all_tg_pt, aptpl);
1784 core_scsi3_lunacl_undepend_item(dest_se_deve);
1785 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1786 core_scsi3_tpg_undepend_item(dest_tpg);
1788 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1792 tidh_new->dest_pr_reg = dest_pr_reg;
1793 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1801 transport_kunmap_data_sg(cmd);
1804 * Go ahead and create a registrations from tid_dest_list for the
1805 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1808 * The SA Reservation Key from the PROUT is set for the
1809 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1810 * means that the TransportID Initiator port will be
1811 * registered on all of the target ports in the SCSI target device
1812 * ALL_TG_PT=0 means the registration will only be for the
1813 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1816 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1817 dest_tpg = tidh->dest_tpg;
1818 dest_node_acl = tidh->dest_node_acl;
1819 dest_se_deve = tidh->dest_se_deve;
1820 dest_pr_reg = tidh->dest_pr_reg;
1821 dest_local_nexus = tidh->dest_local_nexus;
1823 list_del(&tidh->dest_list);
1826 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1827 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1828 PR_REG_ISID_ID_LEN);
1830 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1833 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1834 " registered Transport ID for Node: %s%s Mapped LUN:"
1835 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1836 dest_node_acl->initiatorname, (prf_isid) ?
1837 &i_buf[0] : "", dest_se_deve->mapped_lun);
1839 if (dest_local_nexus)
1842 core_scsi3_lunacl_undepend_item(dest_se_deve);
1843 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1844 core_scsi3_tpg_undepend_item(dest_tpg);
1849 transport_kunmap_data_sg(cmd);
1851 * For the failure case, release everything from tid_dest_list
1852 * including *dest_pr_reg and the configfs dependances..
1854 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1855 dest_tpg = tidh->dest_tpg;
1856 dest_node_acl = tidh->dest_node_acl;
1857 dest_se_deve = tidh->dest_se_deve;
1858 dest_pr_reg = tidh->dest_pr_reg;
1859 dest_local_nexus = tidh->dest_local_nexus;
1861 list_del(&tidh->dest_list);
1864 * Release any extra ALL_TG_PT=1 registrations for
1865 * the SPEC_I_PT=1 case.
1867 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1868 &dest_pr_reg->pr_reg_atp_list,
1869 pr_reg_atp_mem_list) {
1870 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1871 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1872 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1875 kfree(dest_pr_reg->pr_aptpl_buf);
1876 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1878 if (dest_local_nexus)
1881 core_scsi3_lunacl_undepend_item(dest_se_deve);
1882 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1883 core_scsi3_tpg_undepend_item(dest_tpg);
1889 * Called with struct se_device->dev_reservation_lock held
1891 static int __core_scsi3_update_aptpl_buf(
1892 struct se_device *dev,
1894 u32 pr_aptpl_buf_len,
1895 int clear_aptpl_metadata)
1898 struct se_portal_group *tpg;
1899 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1900 struct t10_pr_registration *pr_reg;
1901 unsigned char tmp[512], isid_buf[32];
1905 memset(buf, 0, pr_aptpl_buf_len);
1907 * Called to clear metadata once APTPL has been deactivated.
1909 if (clear_aptpl_metadata) {
1910 snprintf(buf, pr_aptpl_buf_len,
1911 "No Registrations or Reservations\n");
1915 * Walk the registration list..
1917 spin_lock(&su_dev->t10_pr.registration_lock);
1918 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1923 tpg = pr_reg->pr_reg_nacl->se_tpg;
1924 lun = pr_reg->pr_reg_tg_pt_lun;
1926 * Write out any ISID value to APTPL metadata that was included
1927 * in the original registration.
1929 if (pr_reg->isid_present_at_reg)
1930 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1931 pr_reg->pr_reg_isid);
1933 * Include special metadata if the pr_reg matches the
1934 * reservation holder.
1936 if (dev->dev_pr_res_holder == pr_reg) {
1937 snprintf(tmp, 512, "PR_REG_START: %d"
1938 "\ninitiator_fabric=%s\n"
1939 "initiator_node=%s\n%s"
1941 "res_holder=1\nres_type=%02x\n"
1942 "res_scope=%02x\nres_all_tg_pt=%d\n"
1943 "mapped_lun=%u\n", reg_count,
1944 tpg->se_tpg_tfo->get_fabric_name(),
1945 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1946 pr_reg->pr_res_key, pr_reg->pr_res_type,
1947 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1948 pr_reg->pr_res_mapped_lun);
1950 snprintf(tmp, 512, "PR_REG_START: %d\n"
1951 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1952 "sa_res_key=%llu\nres_holder=0\n"
1953 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1954 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1955 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1956 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1957 pr_reg->pr_res_mapped_lun);
1960 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1961 pr_err("Unable to update renaming"
1962 " APTPL metadata\n");
1963 spin_unlock(&su_dev->t10_pr.registration_lock);
1966 len += sprintf(buf+len, "%s", tmp);
1969 * Include information about the associated SCSI target port.
1971 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1972 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1973 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1974 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1975 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1976 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1978 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1979 pr_err("Unable to update renaming"
1980 " APTPL metadata\n");
1981 spin_unlock(&su_dev->t10_pr.registration_lock);
1984 len += sprintf(buf+len, "%s", tmp);
1987 spin_unlock(&su_dev->t10_pr.registration_lock);
1990 len += sprintf(buf+len, "No Registrations or Reservations");
1995 static int core_scsi3_update_aptpl_buf(
1996 struct se_device *dev,
1998 u32 pr_aptpl_buf_len,
1999 int clear_aptpl_metadata)
2003 spin_lock(&dev->dev_reservation_lock);
2004 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2005 clear_aptpl_metadata);
2006 spin_unlock(&dev->dev_reservation_lock);
2012 * Called with struct se_device->aptpl_file_mutex held
2014 static int __core_scsi3_write_aptpl_to_file(
2015 struct se_device *dev,
2017 u32 pr_aptpl_buf_len)
2019 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
2021 struct iovec iov[1];
2022 mm_segment_t old_fs;
2023 int flags = O_RDWR | O_CREAT | O_TRUNC;
2027 memset(iov, 0, sizeof(struct iovec));
2028 memset(path, 0, 512);
2030 if (strlen(&wwn->unit_serial[0]) >= 512) {
2031 pr_err("WWN value for struct se_device does not fit"
2032 " into path buffer\n");
2036 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2037 file = filp_open(path, flags, 0600);
2038 if (IS_ERR(file) || !file || !file->f_dentry) {
2039 pr_err("filp_open(%s) for APTPL metadata"
2041 return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
2044 iov[0].iov_base = &buf[0];
2045 if (!pr_aptpl_buf_len)
2046 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2048 iov[0].iov_len = pr_aptpl_buf_len;
2052 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2056 pr_debug("Error writing APTPL metadata file: %s\n", path);
2057 filp_close(file, NULL);
2060 filp_close(file, NULL);
2065 static int core_scsi3_update_and_write_aptpl(
2066 struct se_device *dev,
2067 unsigned char *in_buf,
2068 u32 in_pr_aptpl_buf_len)
2070 unsigned char null_buf[64], *buf;
2071 u32 pr_aptpl_buf_len;
2072 int ret, clear_aptpl_metadata = 0;
2074 * Can be called with a NULL pointer from PROUT service action CLEAR
2077 memset(null_buf, 0, 64);
2080 * This will clear the APTPL metadata to:
2081 * "No Registrations or Reservations" status
2083 pr_aptpl_buf_len = 64;
2084 clear_aptpl_metadata = 1;
2087 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2090 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2091 clear_aptpl_metadata);
2095 * __core_scsi3_write_aptpl_to_file() will call strlen()
2096 * on the passed buf to determine pr_aptpl_buf_len.
2098 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2105 static int core_scsi3_emulate_pro_register(
2114 struct se_session *se_sess = cmd->se_sess;
2115 struct se_device *dev = cmd->se_dev;
2116 struct se_dev_entry *se_deve;
2117 struct se_lun *se_lun = cmd->se_lun;
2118 struct se_portal_group *se_tpg;
2119 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2120 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2121 /* Used for APTPL metadata w/ UNREGISTER */
2122 unsigned char *pr_aptpl_buf = NULL;
2123 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2124 int pr_holder = 0, ret = 0, type;
2126 if (!se_sess || !se_lun) {
2127 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2128 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2131 se_tpg = se_sess->se_tpg;
2132 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2134 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2135 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2136 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2138 isid_ptr = &isid_buf[0];
2141 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2143 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2146 pr_warn("SPC-3 PR: Reservation Key non-zero"
2147 " for SA REGISTER, returning CONFLICT\n");
2148 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2152 * Do nothing but return GOOD status.
2159 * Perform the Service Action REGISTER on the Initiator
2160 * Port Endpoint that the PRO was received from on the
2161 * Logical Unit of the SCSI device server.
2163 ret = core_scsi3_alloc_registration(cmd->se_dev,
2164 se_sess->se_node_acl, se_deve, isid_ptr,
2165 sa_res_key, all_tg_pt, aptpl,
2168 pr_err("Unable to allocate"
2169 " struct t10_pr_registration\n");
2170 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2175 * Register both the Initiator port that received
2176 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2177 * TransportID from Parameter list and loop through
2178 * fabric dependent parameter list while calling
2179 * logic from of core_scsi3_alloc_registration() for
2180 * each TransportID provided SCSI Initiator Port/Device
2182 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2183 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2188 * Nothing left to do for the APTPL=0 case.
2191 pr_tmpl->pr_aptpl_active = 0;
2192 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2193 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2198 * Locate the newly allocated local I_T Nexus *pr_reg, and
2199 * update the APTPL metadata information using its
2200 * preallocated *pr_reg->pr_aptpl_buf.
2202 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2203 se_sess->se_node_acl, se_sess);
2205 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2206 &pr_reg->pr_aptpl_buf[0],
2207 pr_tmpl->pr_aptpl_buf_len);
2209 pr_tmpl->pr_aptpl_active = 1;
2210 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2213 core_scsi3_put_pr_reg(pr_reg);
2217 * Locate the existing *pr_reg via struct se_node_acl pointers
2220 type = pr_reg->pr_res_type;
2223 if (res_key != pr_reg->pr_res_key) {
2224 pr_err("SPC-3 PR REGISTER: Received"
2225 " res_key: 0x%016Lx does not match"
2226 " existing SA REGISTER res_key:"
2227 " 0x%016Lx\n", res_key,
2228 pr_reg->pr_res_key);
2229 core_scsi3_put_pr_reg(pr_reg);
2230 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2235 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2236 " set while sa_res_key=0\n");
2237 core_scsi3_put_pr_reg(pr_reg);
2238 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2242 * An existing ALL_TG_PT=1 registration being released
2243 * must also set ALL_TG_PT=1 in the incoming PROUT.
2245 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2246 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2247 " registration exists, but ALL_TG_PT=1 bit not"
2248 " present in received PROUT\n");
2249 core_scsi3_put_pr_reg(pr_reg);
2250 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2254 * Allocate APTPL metadata buffer used for UNREGISTER ops
2257 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2259 if (!pr_aptpl_buf) {
2260 pr_err("Unable to allocate"
2262 core_scsi3_put_pr_reg(pr_reg);
2263 cmd->scsi_sense_reason =
2264 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2269 * sa_res_key=0 Unregister Reservation Key for registered I_T
2270 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2274 pr_holder = core_scsi3_check_implict_release(
2275 cmd->se_dev, pr_reg);
2276 if (pr_holder < 0) {
2277 kfree(pr_aptpl_buf);
2278 core_scsi3_put_pr_reg(pr_reg);
2279 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2283 spin_lock(&pr_tmpl->registration_lock);
2285 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2286 * and matching pr_res_key.
2288 if (pr_reg->pr_reg_all_tg_pt) {
2289 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2290 &pr_tmpl->registration_list,
2293 if (!pr_reg_p->pr_reg_all_tg_pt)
2296 if (pr_reg_p->pr_res_key != res_key)
2299 if (pr_reg == pr_reg_p)
2302 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2303 pr_reg_p->pr_reg_nacl->initiatorname))
2306 __core_scsi3_free_registration(dev,
2311 * Release the calling I_T Nexus registration now..
2313 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2316 * From spc4r17, section 5.7.11.3 Unregistering
2318 * If the persistent reservation is a registrants only
2319 * type, the device server shall establish a unit
2320 * attention condition for the initiator port associated
2321 * with every registered I_T nexus except for the I_T
2322 * nexus on which the PERSISTENT RESERVE OUT command was
2323 * received, with the additional sense code set to
2324 * RESERVATIONS RELEASED.
2327 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2328 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2329 list_for_each_entry(pr_reg_p,
2330 &pr_tmpl->registration_list,
2333 core_scsi3_ua_allocate(
2334 pr_reg_p->pr_reg_nacl,
2335 pr_reg_p->pr_res_mapped_lun,
2337 ASCQ_2AH_RESERVATIONS_RELEASED);
2340 spin_unlock(&pr_tmpl->registration_lock);
2343 pr_tmpl->pr_aptpl_active = 0;
2344 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2345 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2346 " for UNREGISTER\n");
2350 ret = core_scsi3_update_and_write_aptpl(dev,
2352 pr_tmpl->pr_aptpl_buf_len);
2354 pr_tmpl->pr_aptpl_active = 1;
2355 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2356 " for UNREGISTER\n");
2359 kfree(pr_aptpl_buf);
2363 * Increment PRgeneration counter for struct se_device"
2364 * upon a successful REGISTER, see spc4r17 section 6.3.2
2365 * READ_KEYS service action.
2367 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2369 pr_reg->pr_res_key = sa_res_key;
2370 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2371 " Key for %s to: 0x%016Lx PRgeneration:"
2372 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2373 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2374 pr_reg->pr_reg_nacl->initiatorname,
2375 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2378 pr_tmpl->pr_aptpl_active = 0;
2379 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2380 core_scsi3_put_pr_reg(pr_reg);
2381 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2386 ret = core_scsi3_update_and_write_aptpl(dev,
2388 pr_tmpl->pr_aptpl_buf_len);
2390 pr_tmpl->pr_aptpl_active = 1;
2391 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2395 kfree(pr_aptpl_buf);
2396 core_scsi3_put_pr_reg(pr_reg);
2402 unsigned char *core_scsi3_pr_dump_type(int type)
2405 case PR_TYPE_WRITE_EXCLUSIVE:
2406 return "Write Exclusive Access";
2407 case PR_TYPE_EXCLUSIVE_ACCESS:
2408 return "Exclusive Access";
2409 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2410 return "Write Exclusive Access, Registrants Only";
2411 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2412 return "Exclusive Access, Registrants Only";
2413 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2414 return "Write Exclusive Access, All Registrants";
2415 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2416 return "Exclusive Access, All Registrants";
2421 return "Unknown SPC-3 PR Type";
2424 static int core_scsi3_pro_reserve(
2426 struct se_device *dev,
2431 struct se_session *se_sess = cmd->se_sess;
2432 struct se_lun *se_lun = cmd->se_lun;
2433 struct t10_pr_registration *pr_reg, *pr_res_holder;
2434 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2435 char i_buf[PR_REG_ISID_ID_LEN];
2438 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2440 if (!se_sess || !se_lun) {
2441 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2442 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2446 * Locate the existing *pr_reg via struct se_node_acl pointers
2448 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2451 pr_err("SPC-3 PR: Unable to locate"
2452 " PR_REGISTERED *pr_reg for RESERVE\n");
2453 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2457 * From spc4r17 Section 5.7.9: Reserving:
2459 * An application client creates a persistent reservation by issuing
2460 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2461 * a registered I_T nexus with the following parameters:
2462 * a) RESERVATION KEY set to the value of the reservation key that is
2463 * registered with the logical unit for the I_T nexus; and
2465 if (res_key != pr_reg->pr_res_key) {
2466 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2467 " does not match existing SA REGISTER res_key:"
2468 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2469 core_scsi3_put_pr_reg(pr_reg);
2470 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2474 * From spc4r17 Section 5.7.9: Reserving:
2477 * b) TYPE field and SCOPE field set to the persistent reservation
2480 * Only one persistent reservation is allowed at a time per logical unit
2481 * and that persistent reservation has a scope of LU_SCOPE.
2483 if (scope != PR_SCOPE_LU_SCOPE) {
2484 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2485 core_scsi3_put_pr_reg(pr_reg);
2486 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2490 * See if we have an existing PR reservation holder pointer at
2491 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2494 spin_lock(&dev->dev_reservation_lock);
2495 pr_res_holder = dev->dev_pr_res_holder;
2496 if ((pr_res_holder)) {
2498 * From spc4r17 Section 5.7.9: Reserving:
2500 * If the device server receives a PERSISTENT RESERVE OUT
2501 * command from an I_T nexus other than a persistent reservation
2502 * holder (see 5.7.10) that attempts to create a persistent
2503 * reservation when a persistent reservation already exists for
2504 * the logical unit, then the command shall be completed with
2505 * RESERVATION CONFLICT status.
2507 if (pr_res_holder != pr_reg) {
2508 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2509 pr_err("SPC-3 PR: Attempted RESERVE from"
2510 " [%s]: %s while reservation already held by"
2511 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2512 cmd->se_tfo->get_fabric_name(),
2513 se_sess->se_node_acl->initiatorname,
2514 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2515 pr_res_holder->pr_reg_nacl->initiatorname);
2517 spin_unlock(&dev->dev_reservation_lock);
2518 core_scsi3_put_pr_reg(pr_reg);
2519 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2523 * From spc4r17 Section 5.7.9: Reserving:
2525 * If a persistent reservation holder attempts to modify the
2526 * type or scope of an existing persistent reservation, the
2527 * command shall be completed with RESERVATION CONFLICT status.
2529 if ((pr_res_holder->pr_res_type != type) ||
2530 (pr_res_holder->pr_res_scope != scope)) {
2531 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2532 pr_err("SPC-3 PR: Attempted RESERVE from"
2533 " [%s]: %s trying to change TYPE and/or SCOPE,"
2534 " while reservation already held by [%s]: %s,"
2535 " returning RESERVATION_CONFLICT\n",
2536 cmd->se_tfo->get_fabric_name(),
2537 se_sess->se_node_acl->initiatorname,
2538 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2539 pr_res_holder->pr_reg_nacl->initiatorname);
2541 spin_unlock(&dev->dev_reservation_lock);
2542 core_scsi3_put_pr_reg(pr_reg);
2543 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2547 * From spc4r17 Section 5.7.9: Reserving:
2549 * If the device server receives a PERSISTENT RESERVE OUT
2550 * command with RESERVE service action where the TYPE field and
2551 * the SCOPE field contain the same values as the existing type
2552 * and scope from a persistent reservation holder, it shall not
2553 * make any change to the existing persistent reservation and
2554 * shall completethe command with GOOD status.
2556 spin_unlock(&dev->dev_reservation_lock);
2557 core_scsi3_put_pr_reg(pr_reg);
2561 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2562 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2564 pr_reg->pr_res_scope = scope;
2565 pr_reg->pr_res_type = type;
2566 pr_reg->pr_res_holder = 1;
2567 dev->dev_pr_res_holder = pr_reg;
2568 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2569 PR_REG_ISID_ID_LEN);
2571 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2572 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2573 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2574 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2575 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2576 cmd->se_tfo->get_fabric_name(),
2577 se_sess->se_node_acl->initiatorname,
2578 (prf_isid) ? &i_buf[0] : "");
2579 spin_unlock(&dev->dev_reservation_lock);
2581 if (pr_tmpl->pr_aptpl_active) {
2582 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2583 &pr_reg->pr_aptpl_buf[0],
2584 pr_tmpl->pr_aptpl_buf_len);
2586 pr_debug("SPC-3 PR: Updated APTPL metadata"
2590 core_scsi3_put_pr_reg(pr_reg);
2594 static int core_scsi3_emulate_pro_reserve(
2600 struct se_device *dev = cmd->se_dev;
2604 case PR_TYPE_WRITE_EXCLUSIVE:
2605 case PR_TYPE_EXCLUSIVE_ACCESS:
2606 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2607 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2608 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2609 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2610 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2613 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2615 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2623 * Called with struct se_device->dev_reservation_lock held.
2625 static void __core_scsi3_complete_pro_release(
2626 struct se_device *dev,
2627 struct se_node_acl *se_nacl,
2628 struct t10_pr_registration *pr_reg,
2631 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2632 char i_buf[PR_REG_ISID_ID_LEN];
2635 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2636 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2637 PR_REG_ISID_ID_LEN);
2639 * Go ahead and release the current PR reservation holder.
2641 dev->dev_pr_res_holder = NULL;
2643 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2644 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2645 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2646 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2647 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2648 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2649 tfo->get_fabric_name(), se_nacl->initiatorname,
2650 (prf_isid) ? &i_buf[0] : "");
2652 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2654 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2657 static int core_scsi3_emulate_pro_release(
2663 struct se_device *dev = cmd->se_dev;
2664 struct se_session *se_sess = cmd->se_sess;
2665 struct se_lun *se_lun = cmd->se_lun;
2666 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2667 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2668 int ret, all_reg = 0;
2670 if (!se_sess || !se_lun) {
2671 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2672 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2676 * Locate the existing *pr_reg via struct se_node_acl pointers
2678 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2680 pr_err("SPC-3 PR: Unable to locate"
2681 " PR_REGISTERED *pr_reg for RELEASE\n");
2682 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2686 * From spc4r17 Section 5.7.11.2 Releasing:
2688 * If there is no persistent reservation or in response to a persistent
2689 * reservation release request from a registered I_T nexus that is not a
2690 * persistent reservation holder (see 5.7.10), the device server shall
2693 * a) Not release the persistent reservation, if any;
2694 * b) Not remove any registrations; and
2695 * c) Complete the command with GOOD status.
2697 spin_lock(&dev->dev_reservation_lock);
2698 pr_res_holder = dev->dev_pr_res_holder;
2699 if (!pr_res_holder) {
2701 * No persistent reservation, return GOOD status.
2703 spin_unlock(&dev->dev_reservation_lock);
2704 core_scsi3_put_pr_reg(pr_reg);
2707 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2708 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2711 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2713 * Non 'All Registrants' PR Type cases..
2714 * Release request from a registered I_T nexus that is not a
2715 * persistent reservation holder. return GOOD status.
2717 spin_unlock(&dev->dev_reservation_lock);
2718 core_scsi3_put_pr_reg(pr_reg);
2722 * From spc4r17 Section 5.7.11.2 Releasing:
2724 * Only the persistent reservation holder (see 5.7.10) is allowed to
2725 * release a persistent reservation.
2727 * An application client releases the persistent reservation by issuing
2728 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2729 * an I_T nexus that is a persistent reservation holder with the
2730 * following parameters:
2732 * a) RESERVATION KEY field set to the value of the reservation key
2733 * that is registered with the logical unit for the I_T nexus;
2735 if (res_key != pr_reg->pr_res_key) {
2736 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2737 " does not match existing SA REGISTER res_key:"
2738 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2739 spin_unlock(&dev->dev_reservation_lock);
2740 core_scsi3_put_pr_reg(pr_reg);
2741 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2745 * From spc4r17 Section 5.7.11.2 Releasing and above:
2747 * b) TYPE field and SCOPE field set to match the persistent
2748 * reservation being released.
2750 if ((pr_res_holder->pr_res_type != type) ||
2751 (pr_res_holder->pr_res_scope != scope)) {
2752 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2753 pr_err("SPC-3 PR RELEASE: Attempted to release"
2754 " reservation from [%s]: %s with different TYPE "
2755 "and/or SCOPE while reservation already held by"
2756 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2757 cmd->se_tfo->get_fabric_name(),
2758 se_sess->se_node_acl->initiatorname,
2759 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2760 pr_res_holder->pr_reg_nacl->initiatorname);
2762 spin_unlock(&dev->dev_reservation_lock);
2763 core_scsi3_put_pr_reg(pr_reg);
2764 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2768 * In response to a persistent reservation release request from the
2769 * persistent reservation holder the device server shall perform a
2770 * release by doing the following as an uninterrupted series of actions:
2771 * a) Release the persistent reservation;
2772 * b) Not remove any registration(s);
2773 * c) If the released persistent reservation is a registrants only type
2774 * or all registrants type persistent reservation,
2775 * the device server shall establish a unit attention condition for
2776 * the initiator port associated with every regis-
2777 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2778 * RESERVE OUT command with RELEASE service action was received,
2779 * with the additional sense code set to RESERVATIONS RELEASED; and
2780 * d) If the persistent reservation is of any other type, the device
2781 * server shall not establish a unit attention condition.
2783 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2786 spin_unlock(&dev->dev_reservation_lock);
2788 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2789 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2790 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2791 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2793 * If no UNIT ATTENTION conditions will be established for
2794 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2795 * go ahead and check for APTPL=1 update+write below
2800 spin_lock(&pr_tmpl->registration_lock);
2801 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2804 * Do not establish a UNIT ATTENTION condition
2805 * for the calling I_T Nexus
2807 if (pr_reg_p == pr_reg)
2810 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2811 pr_reg_p->pr_res_mapped_lun,
2812 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2814 spin_unlock(&pr_tmpl->registration_lock);
2817 if (pr_tmpl->pr_aptpl_active) {
2818 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2819 &pr_reg->pr_aptpl_buf[0],
2820 pr_tmpl->pr_aptpl_buf_len);
2822 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2825 core_scsi3_put_pr_reg(pr_reg);
2829 static int core_scsi3_emulate_pro_clear(
2833 struct se_device *dev = cmd->se_dev;
2834 struct se_node_acl *pr_reg_nacl;
2835 struct se_session *se_sess = cmd->se_sess;
2836 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2837 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2838 u32 pr_res_mapped_lun = 0;
2839 int calling_it_nexus = 0;
2841 * Locate the existing *pr_reg via struct se_node_acl pointers
2843 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2844 se_sess->se_node_acl, se_sess);
2846 pr_err("SPC-3 PR: Unable to locate"
2847 " PR_REGISTERED *pr_reg for CLEAR\n");
2848 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2852 * From spc4r17 section 5.7.11.6, Clearing:
2854 * Any application client may release the persistent reservation and
2855 * remove all registrations from a device server by issuing a
2856 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2857 * registered I_T nexus with the following parameter:
2859 * a) RESERVATION KEY field set to the value of the reservation key
2860 * that is registered with the logical unit for the I_T nexus.
2862 if (res_key != pr_reg_n->pr_res_key) {
2863 pr_err("SPC-3 PR REGISTER: Received"
2864 " res_key: 0x%016Lx does not match"
2865 " existing SA REGISTER res_key:"
2866 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2867 core_scsi3_put_pr_reg(pr_reg_n);
2868 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2872 * a) Release the persistent reservation, if any;
2874 spin_lock(&dev->dev_reservation_lock);
2875 pr_res_holder = dev->dev_pr_res_holder;
2876 if (pr_res_holder) {
2877 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2878 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2881 spin_unlock(&dev->dev_reservation_lock);
2883 * b) Remove all registration(s) (see spc4r17 5.7.7);
2885 spin_lock(&pr_tmpl->registration_lock);
2886 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2887 &pr_tmpl->registration_list, pr_reg_list) {
2889 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2890 pr_reg_nacl = pr_reg->pr_reg_nacl;
2891 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2892 __core_scsi3_free_registration(dev, pr_reg, NULL,
2895 * e) Establish a unit attention condition for the initiator
2896 * port associated with every registered I_T nexus other
2897 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2898 * command with CLEAR service action was received, with the
2899 * additional sense code set to RESERVATIONS PREEMPTED.
2901 if (!calling_it_nexus)
2902 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2903 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2905 spin_unlock(&pr_tmpl->registration_lock);
2907 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2908 cmd->se_tfo->get_fabric_name());
2910 if (pr_tmpl->pr_aptpl_active) {
2911 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2912 pr_debug("SPC-3 PR: Updated APTPL metadata"
2916 core_scsi3_pr_generation(dev);
2921 * Called with struct se_device->dev_reservation_lock held.
2923 static void __core_scsi3_complete_pro_preempt(
2924 struct se_device *dev,
2925 struct t10_pr_registration *pr_reg,
2926 struct list_head *preempt_and_abort_list,
2931 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2932 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2933 char i_buf[PR_REG_ISID_ID_LEN];
2936 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2937 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2938 PR_REG_ISID_ID_LEN);
2940 * Do an implict RELEASE of the existing reservation.
2942 if (dev->dev_pr_res_holder)
2943 __core_scsi3_complete_pro_release(dev, nacl,
2944 dev->dev_pr_res_holder, 0);
2946 dev->dev_pr_res_holder = pr_reg;
2947 pr_reg->pr_res_holder = 1;
2948 pr_reg->pr_res_type = type;
2949 pr_reg->pr_res_scope = scope;
2951 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2952 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2953 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2954 core_scsi3_pr_dump_type(type),
2955 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2956 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2957 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2958 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2960 * For PREEMPT_AND_ABORT, add the preempting reservation's
2961 * struct t10_pr_registration to the list that will be compared
2962 * against received CDBs..
2964 if (preempt_and_abort_list)
2965 list_add_tail(&pr_reg->pr_reg_abort_list,
2966 preempt_and_abort_list);
2969 static void core_scsi3_release_preempt_and_abort(
2970 struct list_head *preempt_and_abort_list,
2971 struct t10_pr_registration *pr_reg_holder)
2973 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2975 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2976 pr_reg_abort_list) {
2978 list_del(&pr_reg->pr_reg_abort_list);
2979 if (pr_reg_holder == pr_reg)
2981 if (pr_reg->pr_res_holder) {
2982 pr_warn("pr_reg->pr_res_holder still set\n");
2986 pr_reg->pr_reg_deve = NULL;
2987 pr_reg->pr_reg_nacl = NULL;
2988 kfree(pr_reg->pr_aptpl_buf);
2989 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2993 static int core_scsi3_pro_preempt(
3001 struct se_device *dev = cmd->se_dev;
3002 struct se_node_acl *pr_reg_nacl;
3003 struct se_session *se_sess = cmd->se_sess;
3004 LIST_HEAD(preempt_and_abort_list);
3005 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
3006 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3007 u32 pr_res_mapped_lun = 0;
3008 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3009 int prh_type = 0, prh_scope = 0, ret;
3012 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3016 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3019 pr_err("SPC-3 PR: Unable to locate"
3020 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3021 (abort) ? "_AND_ABORT" : "");
3022 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3025 if (pr_reg_n->pr_res_key != res_key) {
3026 core_scsi3_put_pr_reg(pr_reg_n);
3027 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3030 if (scope != PR_SCOPE_LU_SCOPE) {
3031 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3032 core_scsi3_put_pr_reg(pr_reg_n);
3033 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3037 spin_lock(&dev->dev_reservation_lock);
3038 pr_res_holder = dev->dev_pr_res_holder;
3039 if (pr_res_holder &&
3040 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3041 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3044 if (!all_reg && !sa_res_key) {
3045 spin_unlock(&dev->dev_reservation_lock);
3046 core_scsi3_put_pr_reg(pr_reg_n);
3047 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3051 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3053 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3054 * persistent reservation holder or there is no persistent reservation
3055 * holder (i.e., there is no persistent reservation), then the device
3056 * server shall perform a preempt by doing the following in an
3057 * uninterrupted series of actions. (See below..)
3059 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3061 * No existing or SA Reservation Key matching reservations..
3063 * PROUT SA PREEMPT with All Registrant type reservations are
3064 * allowed to be processed without a matching SA Reservation Key
3066 spin_lock(&pr_tmpl->registration_lock);
3067 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3068 &pr_tmpl->registration_list, pr_reg_list) {
3070 * Removing of registrations in non all registrants
3071 * type reservations without a matching SA reservation
3074 * a) Remove the registrations for all I_T nexuses
3075 * specified by the SERVICE ACTION RESERVATION KEY
3077 * b) Ignore the contents of the SCOPE and TYPE fields;
3078 * c) Process tasks as defined in 5.7.1; and
3079 * d) Establish a unit attention condition for the
3080 * initiator port associated with every I_T nexus
3081 * that lost its registration other than the I_T
3082 * nexus on which the PERSISTENT RESERVE OUT command
3083 * was received, with the additional sense code set
3084 * to REGISTRATIONS PREEMPTED.
3087 if (pr_reg->pr_res_key != sa_res_key)
3090 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3091 pr_reg_nacl = pr_reg->pr_reg_nacl;
3092 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3093 __core_scsi3_free_registration(dev, pr_reg,
3094 (abort) ? &preempt_and_abort_list :
3095 NULL, calling_it_nexus);
3099 * Case for any existing all registrants type
3100 * reservation, follow logic in spc4r17 section
3101 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3103 * For a ZERO SA Reservation key, release
3104 * all other registrations and do an implict
3105 * release of active persistent reservation.
3107 * For a non-ZERO SA Reservation key, only
3108 * release the matching reservation key from
3112 (pr_reg->pr_res_key != sa_res_key))
3115 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3116 if (calling_it_nexus)
3119 pr_reg_nacl = pr_reg->pr_reg_nacl;
3120 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3121 __core_scsi3_free_registration(dev, pr_reg,
3122 (abort) ? &preempt_and_abort_list :
3126 if (!calling_it_nexus)
3127 core_scsi3_ua_allocate(pr_reg_nacl,
3128 pr_res_mapped_lun, 0x2A,
3129 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3131 spin_unlock(&pr_tmpl->registration_lock);
3133 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3134 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3135 * RESERVATION KEY field to a value that does not match any
3136 * registered reservation key, then the device server shall
3137 * complete the command with RESERVATION CONFLICT status.
3139 if (!released_regs) {
3140 spin_unlock(&dev->dev_reservation_lock);
3141 core_scsi3_put_pr_reg(pr_reg_n);
3142 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3146 * For an existing all registrants type reservation
3147 * with a zero SA rservation key, preempt the existing
3148 * reservation with the new PR type and scope.
3150 if (pr_res_holder && all_reg && !(sa_res_key)) {
3151 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3152 (abort) ? &preempt_and_abort_list : NULL,
3153 type, scope, abort);
3156 core_scsi3_release_preempt_and_abort(
3157 &preempt_and_abort_list, pr_reg_n);
3159 spin_unlock(&dev->dev_reservation_lock);
3161 if (pr_tmpl->pr_aptpl_active) {
3162 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3163 &pr_reg_n->pr_aptpl_buf[0],
3164 pr_tmpl->pr_aptpl_buf_len);
3166 pr_debug("SPC-3 PR: Updated APTPL"
3167 " metadata for PREEMPT%s\n", (abort) ?
3171 core_scsi3_put_pr_reg(pr_reg_n);
3172 core_scsi3_pr_generation(cmd->se_dev);
3176 * The PREEMPTing SA reservation key matches that of the
3177 * existing persistent reservation, first, we check if
3178 * we are preempting our own reservation.
3179 * From spc4r17, section 5.7.11.4.3 Preempting
3180 * persistent reservations and registration handling
3182 * If an all registrants persistent reservation is not
3183 * present, it is not an error for the persistent
3184 * reservation holder to preempt itself (i.e., a
3185 * PERSISTENT RESERVE OUT with a PREEMPT service action
3186 * or a PREEMPT AND ABORT service action with the
3187 * SERVICE ACTION RESERVATION KEY value equal to the
3188 * persistent reservation holder's reservation key that
3189 * is received from the persistent reservation holder).
3190 * In that case, the device server shall establish the
3191 * new persistent reservation and maintain the
3194 prh_type = pr_res_holder->pr_res_type;
3195 prh_scope = pr_res_holder->pr_res_scope;
3197 * If the SERVICE ACTION RESERVATION KEY field identifies a
3198 * persistent reservation holder (see 5.7.10), the device
3199 * server shall perform a preempt by doing the following as
3200 * an uninterrupted series of actions:
3202 * a) Release the persistent reservation for the holder
3203 * identified by the SERVICE ACTION RESERVATION KEY field;
3205 if (pr_reg_n != pr_res_holder)
3206 __core_scsi3_complete_pro_release(dev,
3207 pr_res_holder->pr_reg_nacl,
3208 dev->dev_pr_res_holder, 0);
3210 * b) Remove the registrations for all I_T nexuses identified
3211 * by the SERVICE ACTION RESERVATION KEY field, except the
3212 * I_T nexus that is being used for the PERSISTENT RESERVE
3213 * OUT command. If an all registrants persistent reservation
3214 * is present and the SERVICE ACTION RESERVATION KEY field
3215 * is set to zero, then all registrations shall be removed
3216 * except for that of the I_T nexus that is being used for
3217 * the PERSISTENT RESERVE OUT command;
3219 spin_lock(&pr_tmpl->registration_lock);
3220 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3221 &pr_tmpl->registration_list, pr_reg_list) {
3223 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3224 if (calling_it_nexus)
3227 if (pr_reg->pr_res_key != sa_res_key)
3230 pr_reg_nacl = pr_reg->pr_reg_nacl;
3231 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3232 __core_scsi3_free_registration(dev, pr_reg,
3233 (abort) ? &preempt_and_abort_list : NULL,
3236 * e) Establish a unit attention condition for the initiator
3237 * port associated with every I_T nexus that lost its
3238 * persistent reservation and/or registration, with the
3239 * additional sense code set to REGISTRATIONS PREEMPTED;
3241 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3242 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3244 spin_unlock(&pr_tmpl->registration_lock);
3246 * c) Establish a persistent reservation for the preempting
3247 * I_T nexus using the contents of the SCOPE and TYPE fields;
3249 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3250 (abort) ? &preempt_and_abort_list : NULL,
3251 type, scope, abort);
3253 * d) Process tasks as defined in 5.7.1;
3255 * f) If the type or scope has changed, then for every I_T nexus
3256 * whose reservation key was not removed, except for the I_T
3257 * nexus on which the PERSISTENT RESERVE OUT command was
3258 * received, the device server shall establish a unit
3259 * attention condition for the initiator port associated with
3260 * that I_T nexus, with the additional sense code set to
3261 * RESERVATIONS RELEASED. If the type or scope have not
3262 * changed, then no unit attention condition(s) shall be
3263 * established for this reason.
3265 if ((prh_type != type) || (prh_scope != scope)) {
3266 spin_lock(&pr_tmpl->registration_lock);
3267 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3268 &pr_tmpl->registration_list, pr_reg_list) {
3270 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3271 if (calling_it_nexus)
3274 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3275 pr_reg->pr_res_mapped_lun, 0x2A,
3276 ASCQ_2AH_RESERVATIONS_RELEASED);
3278 spin_unlock(&pr_tmpl->registration_lock);
3280 spin_unlock(&dev->dev_reservation_lock);
3282 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3283 * All received CDBs for the matching existing reservation and
3284 * registrations undergo ABORT_TASK logic.
3286 * From there, core_scsi3_release_preempt_and_abort() will
3287 * release every registration in the list (which have already
3288 * been removed from the primary pr_reg list), except the
3289 * new persistent reservation holder, the calling Initiator Port.
3292 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3293 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3297 if (pr_tmpl->pr_aptpl_active) {
3298 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3299 &pr_reg_n->pr_aptpl_buf[0],
3300 pr_tmpl->pr_aptpl_buf_len);
3302 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3303 "%s\n", (abort) ? "_AND_ABORT" : "");
3306 core_scsi3_put_pr_reg(pr_reg_n);
3307 core_scsi3_pr_generation(cmd->se_dev);
3311 static int core_scsi3_emulate_pro_preempt(
3322 case PR_TYPE_WRITE_EXCLUSIVE:
3323 case PR_TYPE_EXCLUSIVE_ACCESS:
3324 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3325 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3326 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3327 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3328 ret = core_scsi3_pro_preempt(cmd, type, scope,
3329 res_key, sa_res_key, abort);
3332 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3333 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3334 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3342 static int core_scsi3_emulate_pro_register_and_move(
3349 struct se_session *se_sess = cmd->se_sess;
3350 struct se_device *dev = cmd->se_dev;
3351 struct se_dev_entry *dest_se_deve = NULL;
3352 struct se_lun *se_lun = cmd->se_lun;
3353 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3354 struct se_port *se_port;
3355 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3356 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3357 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3358 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3360 unsigned char *initiator_str;
3361 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3362 u32 tid_len, tmp_tid_len;
3363 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3364 unsigned short rtpi;
3365 unsigned char proto_ident;
3367 if (!se_sess || !se_lun) {
3368 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3369 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3372 memset(dest_iport, 0, 64);
3373 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3374 se_tpg = se_sess->se_tpg;
3375 tf_ops = se_tpg->se_tpg_tfo;
3377 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3378 * Register behaviors for a REGISTER AND MOVE service action
3380 * Locate the existing *pr_reg via struct se_node_acl pointers
3382 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3385 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3386 " *pr_reg for REGISTER_AND_MOVE\n");
3387 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3391 * The provided reservation key much match the existing reservation key
3392 * provided during this initiator's I_T nexus registration.
3394 if (res_key != pr_reg->pr_res_key) {
3395 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3396 " res_key: 0x%016Lx does not match existing SA REGISTER"
3397 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3398 core_scsi3_put_pr_reg(pr_reg);
3399 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3403 * The service active reservation key needs to be non zero
3406 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3408 core_scsi3_put_pr_reg(pr_reg);
3409 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3414 * Determine the Relative Target Port Identifier where the reservation
3415 * will be moved to for the TransportID containing SCSI initiator WWN
3418 buf = transport_kmap_data_sg(cmd);
3419 rtpi = (buf[18] & 0xff) << 8;
3420 rtpi |= buf[19] & 0xff;
3421 tid_len = (buf[20] & 0xff) << 24;
3422 tid_len |= (buf[21] & 0xff) << 16;
3423 tid_len |= (buf[22] & 0xff) << 8;
3424 tid_len |= buf[23] & 0xff;
3425 transport_kunmap_data_sg(cmd);
3428 if ((tid_len + 24) != cmd->data_length) {
3429 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3430 " does not equal CDB data_length: %u\n", tid_len,
3432 core_scsi3_put_pr_reg(pr_reg);
3433 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3437 spin_lock(&dev->se_port_lock);
3438 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3439 if (se_port->sep_rtpi != rtpi)
3441 dest_se_tpg = se_port->sep_tpg;
3444 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3448 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3449 smp_mb__after_atomic_inc();
3450 spin_unlock(&dev->se_port_lock);
3452 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3454 pr_err("core_scsi3_tpg_depend_item() failed"
3455 " for dest_se_tpg\n");
3456 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3457 smp_mb__after_atomic_dec();
3458 core_scsi3_put_pr_reg(pr_reg);
3459 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3463 spin_lock(&dev->se_port_lock);
3466 spin_unlock(&dev->se_port_lock);
3468 if (!dest_se_tpg || !dest_tf_ops) {
3469 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3470 " fabric ops from Relative Target Port Identifier:"
3472 core_scsi3_put_pr_reg(pr_reg);
3473 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3477 buf = transport_kmap_data_sg(cmd);
3478 proto_ident = (buf[24] & 0x0f);
3480 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3481 " 0x%02x\n", proto_ident);
3483 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3484 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3485 " proto_ident: 0x%02x does not match ident: 0x%02x"
3486 " from fabric: %s\n", proto_ident,
3487 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3488 dest_tf_ops->get_fabric_name());
3489 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3493 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3494 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3495 " containg a valid tpg_parse_pr_out_transport_id"
3496 " function pointer\n");
3497 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3501 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3502 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3503 if (!initiator_str) {
3504 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3505 " initiator_str from Transport ID\n");
3506 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3511 transport_kunmap_data_sg(cmd);
3514 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3515 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3516 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3519 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3520 * action specifies a TransportID that is the same as the initiator port
3521 * of the I_T nexus for the command received, then the command shall
3522 * be terminated with CHECK CONDITION status, with the sense key set to
3523 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3524 * IN PARAMETER LIST.
3526 pr_reg_nacl = pr_reg->pr_reg_nacl;
3527 matching_iname = (!strcmp(initiator_str,
3528 pr_reg_nacl->initiatorname)) ? 1 : 0;
3529 if (!matching_iname)
3530 goto after_iport_check;
3532 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3533 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3534 " matches: %s on received I_T Nexus\n", initiator_str,
3535 pr_reg_nacl->initiatorname);
3536 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3540 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3541 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3542 " matches: %s %s on received I_T Nexus\n",
3543 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3544 pr_reg->pr_reg_isid);
3545 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3551 * Locate the destination struct se_node_acl from the received Transport ID
3553 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3554 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3556 if (dest_node_acl) {
3557 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3558 smp_mb__after_atomic_inc();
3560 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3562 if (!dest_node_acl) {
3563 pr_err("Unable to locate %s dest_node_acl for"
3564 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3566 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3570 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3572 pr_err("core_scsi3_nodeacl_depend_item() for"
3573 " dest_node_acl\n");
3574 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3575 smp_mb__after_atomic_dec();
3576 dest_node_acl = NULL;
3577 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3582 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3583 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3584 dest_node_acl->initiatorname);
3587 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3590 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3591 if (!dest_se_deve) {
3592 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3593 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3594 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3599 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3601 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3602 atomic_dec(&dest_se_deve->pr_ref_count);
3603 smp_mb__after_atomic_dec();
3604 dest_se_deve = NULL;
3605 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3610 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3611 " ACL for dest_se_deve->mapped_lun: %u\n",
3612 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3613 dest_se_deve->mapped_lun);
3616 * A persistent reservation needs to already existing in order to
3617 * successfully complete the REGISTER_AND_MOVE service action..
3619 spin_lock(&dev->dev_reservation_lock);
3620 pr_res_holder = dev->dev_pr_res_holder;
3621 if (!pr_res_holder) {
3622 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3623 " currently held\n");
3624 spin_unlock(&dev->dev_reservation_lock);
3625 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3630 * The received on I_T Nexus must be the reservation holder.
3632 * From spc4r17 section 5.7.8 Table 50 --
3633 * Register behaviors for a REGISTER AND MOVE service action
3635 if (pr_res_holder != pr_reg) {
3636 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3637 " Nexus is not reservation holder\n");
3638 spin_unlock(&dev->dev_reservation_lock);
3639 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3644 * From spc4r17 section 5.7.8: registering and moving reservation
3646 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3647 * action is received and the established persistent reservation is a
3648 * Write Exclusive - All Registrants type or Exclusive Access -
3649 * All Registrants type reservation, then the command shall be completed
3650 * with RESERVATION CONFLICT status.
3652 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3653 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3654 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3655 " reservation for type: %s\n",
3656 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3657 spin_unlock(&dev->dev_reservation_lock);
3658 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3662 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3664 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3666 type = pr_res_holder->pr_res_type;
3667 scope = pr_res_holder->pr_res_type;
3669 * c) Associate the reservation key specified in the SERVICE ACTION
3670 * RESERVATION KEY field with the I_T nexus specified as the
3671 * destination of the register and move, where:
3672 * A) The I_T nexus is specified by the TransportID and the
3673 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3674 * B) Regardless of the TransportID format used, the association for
3675 * the initiator port is based on either the initiator port name
3676 * (see 3.1.71) on SCSI transport protocols where port names are
3677 * required or the initiator port identifier (see 3.1.70) on SCSI
3678 * transport protocols where port names are not required;
3679 * d) Register the reservation key specified in the SERVICE ACTION
3680 * RESERVATION KEY field;
3681 * e) Retain the reservation key specified in the SERVICE ACTION
3682 * RESERVATION KEY field and associated information;
3684 * Also, It is not an error for a REGISTER AND MOVE service action to
3685 * register an I_T nexus that is already registered with the same
3686 * reservation key or a different reservation key.
3688 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3691 ret = core_scsi3_alloc_registration(cmd->se_dev,
3692 dest_node_acl, dest_se_deve, iport_ptr,
3693 sa_res_key, 0, aptpl, 2, 1);
3695 spin_unlock(&dev->dev_reservation_lock);
3696 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3700 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3705 * f) Release the persistent reservation for the persistent reservation
3706 * holder (i.e., the I_T nexus on which the
3708 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3709 dev->dev_pr_res_holder, 0);
3711 * g) Move the persistent reservation to the specified I_T nexus using
3712 * the same scope and type as the persistent reservation released in
3715 dev->dev_pr_res_holder = dest_pr_reg;
3716 dest_pr_reg->pr_res_holder = 1;
3717 dest_pr_reg->pr_res_type = type;
3718 pr_reg->pr_res_scope = scope;
3719 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3720 PR_REG_ISID_ID_LEN);
3722 * Increment PRGeneration for existing registrations..
3725 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3726 spin_unlock(&dev->dev_reservation_lock);
3728 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3729 " created new reservation holder TYPE: %s on object RTPI:"
3730 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3731 core_scsi3_pr_dump_type(type), rtpi,
3732 dest_pr_reg->pr_res_generation);
3733 pr_debug("SPC-3 PR Successfully moved reservation from"
3734 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3735 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3736 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3737 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3740 * It is now safe to release configfs group dependencies for destination
3741 * of Transport ID Initiator Device/Port Identifier
3743 core_scsi3_lunacl_undepend_item(dest_se_deve);
3744 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3745 core_scsi3_tpg_undepend_item(dest_se_tpg);
3747 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3748 * nexus on which PERSISTENT RESERVE OUT command was received.
3751 spin_lock(&pr_tmpl->registration_lock);
3752 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3753 spin_unlock(&pr_tmpl->registration_lock);
3755 core_scsi3_put_pr_reg(pr_reg);
3758 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3759 * write out the updated metadata to struct file for this SCSI device.
3762 pr_tmpl->pr_aptpl_active = 0;
3763 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3764 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3765 " REGISTER_AND_MOVE\n");
3767 pr_tmpl->pr_aptpl_active = 1;
3768 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3769 &dest_pr_reg->pr_aptpl_buf[0],
3770 pr_tmpl->pr_aptpl_buf_len);
3772 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3773 " REGISTER_AND_MOVE\n");
3776 transport_kunmap_data_sg(cmd);
3778 core_scsi3_put_pr_reg(dest_pr_reg);
3782 transport_kunmap_data_sg(cmd);
3784 core_scsi3_lunacl_undepend_item(dest_se_deve);
3786 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3787 core_scsi3_tpg_undepend_item(dest_se_tpg);
3788 core_scsi3_put_pr_reg(pr_reg);
3792 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3794 unsigned int __v1, __v2;
3796 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3797 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3799 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3803 * See spc4r17 section 6.14 Table 170
3805 int target_scsi3_emulate_pr_out(struct se_task *task)
3807 struct se_cmd *cmd = task->task_se_cmd;
3808 unsigned char *cdb = &cmd->t_task_cdb[0];
3810 u64 res_key, sa_res_key;
3811 int sa, scope, type, aptpl;
3812 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3816 * Following spc2r20 5.5.1 Reservations overview:
3818 * If a logical unit has been reserved by any RESERVE command and is
3819 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3820 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3821 * initiator or service action and shall terminate with a RESERVATION
3824 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3825 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3826 " SPC-2 reservation is held, returning"
3827 " RESERVATION_CONFLICT\n");
3828 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3834 * FIXME: A NULL struct se_session pointer means an this is not coming from
3835 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3837 if (!cmd->se_sess) {
3838 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3842 if (cmd->data_length < 24) {
3843 pr_warn("SPC-PR: Received PR OUT parameter list"
3844 " length too small: %u\n", cmd->data_length);
3845 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3850 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3852 sa = (cdb[1] & 0x1f);
3853 scope = (cdb[2] & 0xf0);
3854 type = (cdb[2] & 0x0f);
3856 buf = transport_kmap_data_sg(cmd);
3858 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3860 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3861 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3863 * REGISTER_AND_MOVE uses a different SA parameter list containing
3864 * SCSI TransportIDs.
3866 if (sa != PRO_REGISTER_AND_MOVE) {
3867 spec_i_pt = (buf[20] & 0x08);
3868 all_tg_pt = (buf[20] & 0x04);
3869 aptpl = (buf[20] & 0x01);
3871 aptpl = (buf[17] & 0x01);
3872 unreg = (buf[17] & 0x02);
3874 transport_kunmap_data_sg(cmd);
3878 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3880 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3881 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3887 * From spc4r17 section 6.14:
3889 * If the SPEC_I_PT bit is set to zero, the service action is not
3890 * REGISTER AND MOVE, and the parameter list length is not 24, then
3891 * the command shall be terminated with CHECK CONDITION status, with
3892 * the sense key set to ILLEGAL REQUEST, and the additional sense
3893 * code set to PARAMETER LIST LENGTH ERROR.
3895 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3896 (cmd->data_length != 24)) {
3897 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3898 " list length: %u\n", cmd->data_length);
3899 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3904 * (core_scsi3_emulate_pro_* function parameters
3905 * are defined by spc4r17 Table 174:
3906 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3910 ret = core_scsi3_emulate_pro_register(cmd,
3911 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3914 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3917 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3920 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3923 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3924 res_key, sa_res_key, 0);
3926 case PRO_PREEMPT_AND_ABORT:
3927 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3928 res_key, sa_res_key, 1);
3930 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3931 ret = core_scsi3_emulate_pro_register(cmd,
3932 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3934 case PRO_REGISTER_AND_MOVE:
3935 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3936 sa_res_key, aptpl, unreg);
3939 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3940 " action: 0x%02x\n", cdb[1] & 0x1f);
3941 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3948 task->task_scsi_status = GOOD;
3949 transport_complete_task(task, 1);
3955 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3957 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3959 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3961 struct se_device *se_dev = cmd->se_dev;
3962 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
3963 struct t10_pr_registration *pr_reg;
3965 u32 add_len = 0, off = 8;
3967 if (cmd->data_length < 8) {
3968 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3969 " too small\n", cmd->data_length);
3970 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3974 buf = transport_kmap_data_sg(cmd);
3975 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3976 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3977 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3978 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
3980 spin_lock(&su_dev->t10_pr.registration_lock);
3981 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
3984 * Check for overflow of 8byte PRI READ_KEYS payload and
3985 * next reservation key list descriptor.
3987 if ((add_len + 8) > (cmd->data_length - 8))
3990 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3991 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3992 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3993 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3994 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3995 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3996 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3997 buf[off++] = (pr_reg->pr_res_key & 0xff);
4001 spin_unlock(&su_dev->t10_pr.registration_lock);
4003 buf[4] = ((add_len >> 24) & 0xff);
4004 buf[5] = ((add_len >> 16) & 0xff);
4005 buf[6] = ((add_len >> 8) & 0xff);
4006 buf[7] = (add_len & 0xff);
4008 transport_kunmap_data_sg(cmd);
4014 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4016 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4018 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4020 struct se_device *se_dev = cmd->se_dev;
4021 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4022 struct t10_pr_registration *pr_reg;
4025 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4027 if (cmd->data_length < 8) {
4028 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4029 " too small\n", cmd->data_length);
4030 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4034 buf = transport_kmap_data_sg(cmd);
4035 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4036 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4037 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4038 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4040 spin_lock(&se_dev->dev_reservation_lock);
4041 pr_reg = se_dev->dev_pr_res_holder;
4044 * Set the hardcoded Additional Length
4046 buf[4] = ((add_len >> 24) & 0xff);
4047 buf[5] = ((add_len >> 16) & 0xff);
4048 buf[6] = ((add_len >> 8) & 0xff);
4049 buf[7] = (add_len & 0xff);
4051 if (cmd->data_length < 22)
4055 * Set the Reservation key.
4057 * From spc4r17, section 5.7.10:
4058 * A persistent reservation holder has its reservation key
4059 * returned in the parameter data from a PERSISTENT
4060 * RESERVE IN command with READ RESERVATION service action as
4062 * a) For a persistent reservation of the type Write Exclusive
4063 * - All Registrants or Exclusive Access  All Regitrants,
4064 * the reservation key shall be set to zero; or
4065 * b) For all other persistent reservation types, the
4066 * reservation key shall be set to the registered
4067 * reservation key for the I_T nexus that holds the
4068 * persistent reservation.
4070 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4071 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4074 pr_res_key = pr_reg->pr_res_key;
4076 buf[8] = ((pr_res_key >> 56) & 0xff);
4077 buf[9] = ((pr_res_key >> 48) & 0xff);
4078 buf[10] = ((pr_res_key >> 40) & 0xff);
4079 buf[11] = ((pr_res_key >> 32) & 0xff);
4080 buf[12] = ((pr_res_key >> 24) & 0xff);
4081 buf[13] = ((pr_res_key >> 16) & 0xff);
4082 buf[14] = ((pr_res_key >> 8) & 0xff);
4083 buf[15] = (pr_res_key & 0xff);
4085 * Set the SCOPE and TYPE
4087 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4088 (pr_reg->pr_res_type & 0x0f);
4092 spin_unlock(&se_dev->dev_reservation_lock);
4093 transport_kunmap_data_sg(cmd);
4099 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4101 * See spc4r17 section 6.13.4 Table 165
4103 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4105 struct se_device *dev = cmd->se_dev;
4106 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
4108 u16 add_len = 8; /* Hardcoded to 8. */
4110 if (cmd->data_length < 6) {
4111 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4112 " %u too small\n", cmd->data_length);
4113 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4117 buf = transport_kmap_data_sg(cmd);
4119 buf[0] = ((add_len << 8) & 0xff);
4120 buf[1] = (add_len & 0xff);
4121 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4122 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4123 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4124 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4126 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4127 * set the TMV: Task Mask Valid bit.
4131 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4133 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4135 * PTPL_A: Persistence across Target Power Loss Active bit
4137 if (pr_tmpl->pr_aptpl_active)
4140 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4142 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4143 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4144 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4145 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4146 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4147 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4149 transport_kunmap_data_sg(cmd);
4155 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4157 * See spc4r17 section 6.13.5 Table 168 and 169
4159 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4161 struct se_device *se_dev = cmd->se_dev;
4162 struct se_node_acl *se_nacl;
4163 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4164 struct se_portal_group *se_tpg;
4165 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4166 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
4168 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4169 u32 off = 8; /* off into first Full Status descriptor */
4170 int format_code = 0;
4172 if (cmd->data_length < 8) {
4173 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4174 " too small\n", cmd->data_length);
4175 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4179 buf = transport_kmap_data_sg(cmd);
4181 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4182 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4183 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4184 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4186 spin_lock(&pr_tmpl->registration_lock);
4187 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4188 &pr_tmpl->registration_list, pr_reg_list) {
4190 se_nacl = pr_reg->pr_reg_nacl;
4191 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4194 atomic_inc(&pr_reg->pr_res_holders);
4195 smp_mb__after_atomic_inc();
4196 spin_unlock(&pr_tmpl->registration_lock);
4198 * Determine expected length of $FABRIC_MOD specific
4199 * TransportID full status descriptor..
4201 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4202 se_tpg, se_nacl, pr_reg, &format_code);
4204 if ((exp_desc_len + add_len) > cmd->data_length) {
4205 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4206 " out of buffer: %d\n", cmd->data_length);
4207 spin_lock(&pr_tmpl->registration_lock);
4208 atomic_dec(&pr_reg->pr_res_holders);
4209 smp_mb__after_atomic_dec();
4213 * Set RESERVATION KEY
4215 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4216 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4217 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4218 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4219 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4220 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4221 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4222 buf[off++] = (pr_reg->pr_res_key & 0xff);
4223 off += 4; /* Skip Over Reserved area */
4226 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4228 if (pr_reg->pr_reg_all_tg_pt)
4231 * The struct se_lun pointer will be present for the
4232 * reservation holder for PR_HOLDER bit.
4234 * Also, if this registration is the reservation
4235 * holder, fill in SCOPE and TYPE in the next byte.
4237 if (pr_reg->pr_res_holder) {
4239 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4240 (pr_reg->pr_res_type & 0x0f);
4244 off += 4; /* Skip over reserved area */
4246 * From spc4r17 6.3.15:
4248 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4249 * IDENTIFIER field contains the relative port identifier (see
4250 * 3.1.120) of the target port that is part of the I_T nexus
4251 * described by this full status descriptor. If the ALL_TG_PT
4252 * bit is set to one, the contents of the RELATIVE TARGET PORT
4253 * IDENTIFIER field are not defined by this standard.
4255 if (!pr_reg->pr_reg_all_tg_pt) {
4256 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4258 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4259 buf[off++] = (port->sep_rtpi & 0xff);
4261 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4264 * Now, have the $FABRIC_MOD fill in the protocol identifier
4266 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4267 se_nacl, pr_reg, &format_code, &buf[off+4]);
4269 spin_lock(&pr_tmpl->registration_lock);
4270 atomic_dec(&pr_reg->pr_res_holders);
4271 smp_mb__after_atomic_dec();
4273 * Set the ADDITIONAL DESCRIPTOR LENGTH
4275 buf[off++] = ((desc_len >> 24) & 0xff);
4276 buf[off++] = ((desc_len >> 16) & 0xff);
4277 buf[off++] = ((desc_len >> 8) & 0xff);
4278 buf[off++] = (desc_len & 0xff);
4280 * Size of full desctipor header minus TransportID
4281 * containing $FABRIC_MOD specific) initiator device/port
4284 * See spc4r17 Section 6.13.5 Table 169
4286 add_desc_len = (24 + desc_len);
4289 add_len += add_desc_len;
4291 spin_unlock(&pr_tmpl->registration_lock);
4293 * Set ADDITIONAL_LENGTH
4295 buf[4] = ((add_len >> 24) & 0xff);
4296 buf[5] = ((add_len >> 16) & 0xff);
4297 buf[6] = ((add_len >> 8) & 0xff);
4298 buf[7] = (add_len & 0xff);
4300 transport_kunmap_data_sg(cmd);
4305 int target_scsi3_emulate_pr_in(struct se_task *task)
4307 struct se_cmd *cmd = task->task_se_cmd;
4311 * Following spc2r20 5.5.1 Reservations overview:
4313 * If a logical unit has been reserved by any RESERVE command and is
4314 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4315 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4316 * initiator or service action and shall terminate with a RESERVATION
4319 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4320 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4321 " SPC-2 reservation is held, returning"
4322 " RESERVATION_CONFLICT\n");
4323 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4327 switch (cmd->t_task_cdb[1] & 0x1f) {
4329 ret = core_scsi3_pri_read_keys(cmd);
4331 case PRI_READ_RESERVATION:
4332 ret = core_scsi3_pri_read_reservation(cmd);
4334 case PRI_REPORT_CAPABILITIES:
4335 ret = core_scsi3_pri_report_capabilities(cmd);
4337 case PRI_READ_FULL_STATUS:
4338 ret = core_scsi3_pri_read_full_status(cmd);
4341 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4342 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4343 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4349 task->task_scsi_status = GOOD;
4350 transport_complete_task(task, 1);
4355 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4360 static int core_pt_seq_non_holder(
4368 int core_setup_reservations(struct se_device *dev, int force_pt)
4370 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
4371 struct t10_reservation *rest = &su_dev->t10_pr;
4373 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4374 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4375 * cause a problem because libata and some SATA RAID HBAs appear
4376 * under Linux/SCSI, but to emulate reservations themselves.
4378 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4379 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
4380 rest->res_type = SPC_PASSTHROUGH;
4381 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4382 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4383 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4384 " emulation\n", dev->transport->name);
4388 * If SPC-3 or above is reported by real or emulated struct se_device,
4389 * use emulated Persistent Reservations.
4391 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4392 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4393 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4394 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4395 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4396 " emulation\n", dev->transport->name);
4398 rest->res_type = SPC2_RESERVATIONS;
4399 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4400 rest->pr_ops.t10_seq_non_holder =
4401 &core_scsi2_reservation_seq_non_holder;
4402 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4403 dev->transport->name);