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_device.h>
37 #include <target/target_core_tmr.h>
38 #include <target/target_core_tpg.h>
39 #include <target/target_core_transport.h>
40 #include <target/target_core_fabric_ops.h>
41 #include <target/target_core_configfs.h>
43 #include "target_core_hba.h"
44 #include "target_core_pr.h"
45 #include "target_core_ua.h"
48 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
50 struct pr_transport_id_holder {
52 struct t10_pr_registration *dest_pr_reg;
53 struct se_portal_group *dest_tpg;
54 struct se_node_acl *dest_node_acl;
55 struct se_dev_entry *dest_se_deve;
56 struct list_head dest_list;
59 int core_pr_dump_initiator_port(
60 struct t10_pr_registration *pr_reg,
64 if (!pr_reg->isid_present_at_reg)
67 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
71 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
72 struct t10_pr_registration *, int);
74 static int core_scsi2_reservation_seq_non_holder(
91 static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
93 struct se_device *dev = cmd->se_dev;
94 struct se_session *sess = cmd->se_sess;
100 spin_lock(&dev->dev_reservation_lock);
101 if (!dev->dev_reserved_node_acl || !sess) {
102 spin_unlock(&dev->dev_reservation_lock);
105 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
106 spin_unlock(&dev->dev_reservation_lock);
109 if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
110 spin_unlock(&dev->dev_reservation_lock);
113 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
114 spin_unlock(&dev->dev_reservation_lock);
119 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
120 struct se_node_acl *, struct se_session *);
121 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
123 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
125 struct se_session *se_sess = cmd->se_sess;
126 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
127 struct t10_pr_registration *pr_reg;
128 struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
129 int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
135 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
139 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
142 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
143 * status, but no reservation shall be established and the
144 * persistent reservation shall not be changed, if the command
145 * is received from a) and b) below.
147 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
148 * status, but the persistent reservation shall not be released,
149 * if the command is received from a) and b)
151 * a) An I_T nexus that is a persistent reservation holder; or
152 * b) An I_T nexus that is registered if a registrants only or
153 * all registrants type persistent reservation is present.
155 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
156 * RELEASE(6) command, or RELEASE(10) command shall be processed
157 * as defined in SPC-2.
159 if (pr_reg->pr_res_holder) {
160 core_scsi3_put_pr_reg(pr_reg);
163 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
164 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
165 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
166 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
167 core_scsi3_put_pr_reg(pr_reg);
170 core_scsi3_put_pr_reg(pr_reg);
174 * Following spc2r20 5.5.1 Reservations overview:
176 * If a logical unit has executed a PERSISTENT RESERVE OUT
177 * command with the REGISTER or the REGISTER AND IGNORE
178 * EXISTING KEY service action and is still registered by any
179 * initiator, all RESERVE commands and all RELEASE commands
180 * regardless of initiator shall conflict and shall terminate
181 * with a RESERVATION CONFLICT status.
183 spin_lock(&pr_tmpl->registration_lock);
184 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
185 spin_unlock(&pr_tmpl->registration_lock);
189 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
190 " while active SPC-3 registrations exist,"
191 " returning RESERVATION_CONFLICT\n");
192 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
199 int target_scsi2_reservation_release(struct se_task *task)
201 struct se_cmd *cmd = task->task_se_cmd;
202 struct se_device *dev = cmd->se_dev;
203 struct se_session *sess = cmd->se_sess;
204 struct se_portal_group *tpg = sess->se_tpg;
209 rc = target_check_scsi2_reservation_conflict(cmd);
213 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
219 spin_lock(&dev->dev_reservation_lock);
220 if (!dev->dev_reserved_node_acl || !sess)
223 if (dev->dev_reserved_node_acl != sess->se_node_acl)
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 struct 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);
1514 INIT_LIST_HEAD(&tid_dest_list);
1516 local_se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1518 * Allocate a struct pr_transport_id_holder and setup the
1519 * local_node_acl and local_se_deve pointers and add to
1520 * struct list_head tid_dest_list for add registration
1521 * processing in the loop of tid_dest_list below.
1523 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1525 pr_err("Unable to allocate tidh_new\n");
1526 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1529 INIT_LIST_HEAD(&tidh_new->dest_list);
1530 tidh_new->dest_tpg = tpg;
1531 tidh_new->dest_node_acl = se_sess->se_node_acl;
1532 tidh_new->dest_se_deve = local_se_deve;
1534 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1535 se_sess->se_node_acl, local_se_deve, l_isid,
1536 sa_res_key, all_tg_pt, aptpl);
1537 if (!local_pr_reg) {
1539 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1542 tidh_new->dest_pr_reg = local_pr_reg;
1544 * The local I_T nexus does not hold any configfs dependances,
1545 * so we set tid_h->dest_local_nexus=1 to prevent the
1546 * configfs_undepend_item() calls in the tid_dest_list loops below.
1548 tidh_new->dest_local_nexus = 1;
1549 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1551 buf = transport_kmap_data_sg(cmd);
1553 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1554 * first extract TransportID Parameter Data Length, and make sure
1555 * the value matches up to the SCSI expected data transfer length.
1557 tpdl = (buf[24] & 0xff) << 24;
1558 tpdl |= (buf[25] & 0xff) << 16;
1559 tpdl |= (buf[26] & 0xff) << 8;
1560 tpdl |= buf[27] & 0xff;
1562 if ((tpdl + 28) != cmd->data_length) {
1563 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1564 " does not equal CDB data_length: %u\n", tpdl,
1566 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1571 * Start processing the received transport IDs using the
1572 * receiving I_T Nexus portal's fabric dependent methods to
1573 * obtain the SCSI Initiator Port/Device Identifiers.
1578 proto_ident = (ptr[0] & 0x0f);
1581 spin_lock(&dev->se_port_lock);
1582 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1583 tmp_tpg = tmp_port->sep_tpg;
1586 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1589 if (!tmp_tf_ops->get_fabric_proto_ident ||
1590 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1593 * Look for the matching proto_ident provided by
1594 * the received TransportID
1596 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1597 if (tmp_proto_ident != proto_ident)
1599 dest_rtpi = tmp_port->sep_rtpi;
1601 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1602 tmp_tpg, (const char *)ptr, &tid_len,
1607 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1608 smp_mb__after_atomic_inc();
1609 spin_unlock(&dev->se_port_lock);
1611 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1613 pr_err(" core_scsi3_tpg_depend_item()"
1615 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1616 smp_mb__after_atomic_dec();
1617 cmd->scsi_sense_reason =
1618 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1623 * Locate the desination initiator ACL to be registered
1624 * from the decoded fabric module specific TransportID
1627 spin_lock_irq(&tmp_tpg->acl_node_lock);
1628 dest_node_acl = __core_tpg_get_initiator_node_acl(
1630 if (dest_node_acl) {
1631 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1632 smp_mb__after_atomic_inc();
1634 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1636 if (!dest_node_acl) {
1637 core_scsi3_tpg_undepend_item(tmp_tpg);
1638 spin_lock(&dev->se_port_lock);
1642 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1644 pr_err("configfs_depend_item() failed"
1645 " for dest_node_acl->acl_group\n");
1646 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1647 smp_mb__after_atomic_dec();
1648 core_scsi3_tpg_undepend_item(tmp_tpg);
1649 cmd->scsi_sense_reason =
1650 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1656 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1657 " %s Port RTPI: %hu\n",
1658 dest_tpg->se_tpg_tfo->get_fabric_name(),
1659 dest_node_acl->initiatorname, dest_rtpi);
1661 spin_lock(&dev->se_port_lock);
1664 spin_unlock(&dev->se_port_lock);
1667 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1669 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1674 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1675 " tid_len: %d for %s + %s\n",
1676 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1677 tpdl, tid_len, i_str, iport_ptr);
1679 if (tid_len > tpdl) {
1680 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1681 " %u for Transport ID: %s\n", tid_len, ptr);
1682 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1683 core_scsi3_tpg_undepend_item(dest_tpg);
1684 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1689 * Locate the desintation struct se_dev_entry pointer for matching
1690 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1693 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1695 if (!dest_se_deve) {
1696 pr_err("Unable to locate %s dest_se_deve"
1697 " from destination RTPI: %hu\n",
1698 dest_tpg->se_tpg_tfo->get_fabric_name(),
1701 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1702 core_scsi3_tpg_undepend_item(dest_tpg);
1703 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1708 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1710 pr_err("core_scsi3_lunacl_depend_item()"
1712 atomic_dec(&dest_se_deve->pr_ref_count);
1713 smp_mb__after_atomic_dec();
1714 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1715 core_scsi3_tpg_undepend_item(dest_tpg);
1716 cmd->scsi_sense_reason =
1717 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1722 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1723 " dest_se_deve mapped_lun: %u\n",
1724 dest_tpg->se_tpg_tfo->get_fabric_name(),
1725 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1728 * Skip any TransportIDs that already have a registration for
1731 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1734 core_scsi3_put_pr_reg(pr_reg_e);
1735 core_scsi3_lunacl_undepend_item(dest_se_deve);
1736 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1737 core_scsi3_tpg_undepend_item(dest_tpg);
1744 * Allocate a struct pr_transport_id_holder and setup
1745 * the dest_node_acl and dest_se_deve pointers for the
1748 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1751 pr_err("Unable to allocate tidh_new\n");
1752 core_scsi3_lunacl_undepend_item(dest_se_deve);
1753 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1754 core_scsi3_tpg_undepend_item(dest_tpg);
1755 cmd->scsi_sense_reason =
1756 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1760 INIT_LIST_HEAD(&tidh_new->dest_list);
1761 tidh_new->dest_tpg = dest_tpg;
1762 tidh_new->dest_node_acl = dest_node_acl;
1763 tidh_new->dest_se_deve = dest_se_deve;
1766 * Allocate, but do NOT add the registration for the
1767 * TransportID referenced SCSI Initiator port. This
1768 * done because of the following from spc4r17 in section
1769 * 6.14.3 wrt SPEC_I_PT:
1771 * "If a registration fails for any initiator port (e.g., if th
1772 * logical unit does not have enough resources available to
1773 * hold the registration information), no registrations shall be
1774 * made, and the command shall be terminated with
1775 * CHECK CONDITION status."
1777 * That means we call __core_scsi3_alloc_registration() here,
1778 * and then call __core_scsi3_add_registration() in the
1779 * 2nd loop which will never fail.
1781 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1782 dest_node_acl, dest_se_deve, iport_ptr,
1783 sa_res_key, all_tg_pt, aptpl);
1785 core_scsi3_lunacl_undepend_item(dest_se_deve);
1786 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1787 core_scsi3_tpg_undepend_item(dest_tpg);
1789 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1793 tidh_new->dest_pr_reg = dest_pr_reg;
1794 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1802 transport_kunmap_data_sg(cmd);
1805 * Go ahead and create a registrations from tid_dest_list for the
1806 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1809 * The SA Reservation Key from the PROUT is set for the
1810 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1811 * means that the TransportID Initiator port will be
1812 * registered on all of the target ports in the SCSI target device
1813 * ALL_TG_PT=0 means the registration will only be for the
1814 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1817 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1818 dest_tpg = tidh->dest_tpg;
1819 dest_node_acl = tidh->dest_node_acl;
1820 dest_se_deve = tidh->dest_se_deve;
1821 dest_pr_reg = tidh->dest_pr_reg;
1822 dest_local_nexus = tidh->dest_local_nexus;
1824 list_del(&tidh->dest_list);
1827 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1828 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1829 PR_REG_ISID_ID_LEN);
1831 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1834 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1835 " registered Transport ID for Node: %s%s Mapped LUN:"
1836 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1837 dest_node_acl->initiatorname, (prf_isid) ?
1838 &i_buf[0] : "", dest_se_deve->mapped_lun);
1840 if (dest_local_nexus)
1843 core_scsi3_lunacl_undepend_item(dest_se_deve);
1844 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1845 core_scsi3_tpg_undepend_item(dest_tpg);
1850 transport_kunmap_data_sg(cmd);
1852 * For the failure case, release everything from tid_dest_list
1853 * including *dest_pr_reg and the configfs dependances..
1855 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1856 dest_tpg = tidh->dest_tpg;
1857 dest_node_acl = tidh->dest_node_acl;
1858 dest_se_deve = tidh->dest_se_deve;
1859 dest_pr_reg = tidh->dest_pr_reg;
1860 dest_local_nexus = tidh->dest_local_nexus;
1862 list_del(&tidh->dest_list);
1865 * Release any extra ALL_TG_PT=1 registrations for
1866 * the SPEC_I_PT=1 case.
1868 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1869 &dest_pr_reg->pr_reg_atp_list,
1870 pr_reg_atp_mem_list) {
1871 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1872 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1873 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1876 kfree(dest_pr_reg->pr_aptpl_buf);
1877 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1879 if (dest_local_nexus)
1882 core_scsi3_lunacl_undepend_item(dest_se_deve);
1883 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1884 core_scsi3_tpg_undepend_item(dest_tpg);
1890 * Called with struct se_device->dev_reservation_lock held
1892 static int __core_scsi3_update_aptpl_buf(
1893 struct se_device *dev,
1895 u32 pr_aptpl_buf_len,
1896 int clear_aptpl_metadata)
1899 struct se_portal_group *tpg;
1900 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1901 struct t10_pr_registration *pr_reg;
1902 unsigned char tmp[512], isid_buf[32];
1906 memset(buf, 0, pr_aptpl_buf_len);
1908 * Called to clear metadata once APTPL has been deactivated.
1910 if (clear_aptpl_metadata) {
1911 snprintf(buf, pr_aptpl_buf_len,
1912 "No Registrations or Reservations\n");
1916 * Walk the registration list..
1918 spin_lock(&su_dev->t10_pr.registration_lock);
1919 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1924 tpg = pr_reg->pr_reg_nacl->se_tpg;
1925 lun = pr_reg->pr_reg_tg_pt_lun;
1927 * Write out any ISID value to APTPL metadata that was included
1928 * in the original registration.
1930 if (pr_reg->isid_present_at_reg)
1931 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1932 pr_reg->pr_reg_isid);
1934 * Include special metadata if the pr_reg matches the
1935 * reservation holder.
1937 if (dev->dev_pr_res_holder == pr_reg) {
1938 snprintf(tmp, 512, "PR_REG_START: %d"
1939 "\ninitiator_fabric=%s\n"
1940 "initiator_node=%s\n%s"
1942 "res_holder=1\nres_type=%02x\n"
1943 "res_scope=%02x\nres_all_tg_pt=%d\n"
1944 "mapped_lun=%u\n", reg_count,
1945 tpg->se_tpg_tfo->get_fabric_name(),
1946 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1947 pr_reg->pr_res_key, pr_reg->pr_res_type,
1948 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1949 pr_reg->pr_res_mapped_lun);
1951 snprintf(tmp, 512, "PR_REG_START: %d\n"
1952 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1953 "sa_res_key=%llu\nres_holder=0\n"
1954 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1955 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1956 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1957 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1958 pr_reg->pr_res_mapped_lun);
1961 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1962 pr_err("Unable to update renaming"
1963 " APTPL metadata\n");
1964 spin_unlock(&su_dev->t10_pr.registration_lock);
1967 len += sprintf(buf+len, "%s", tmp);
1970 * Include information about the associated SCSI target port.
1972 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1973 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1974 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1975 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1976 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1977 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1979 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1980 pr_err("Unable to update renaming"
1981 " APTPL metadata\n");
1982 spin_unlock(&su_dev->t10_pr.registration_lock);
1985 len += sprintf(buf+len, "%s", tmp);
1988 spin_unlock(&su_dev->t10_pr.registration_lock);
1991 len += sprintf(buf+len, "No Registrations or Reservations");
1996 static int core_scsi3_update_aptpl_buf(
1997 struct se_device *dev,
1999 u32 pr_aptpl_buf_len,
2000 int clear_aptpl_metadata)
2004 spin_lock(&dev->dev_reservation_lock);
2005 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2006 clear_aptpl_metadata);
2007 spin_unlock(&dev->dev_reservation_lock);
2013 * Called with struct se_device->aptpl_file_mutex held
2015 static int __core_scsi3_write_aptpl_to_file(
2016 struct se_device *dev,
2018 u32 pr_aptpl_buf_len)
2020 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
2022 struct iovec iov[1];
2023 mm_segment_t old_fs;
2024 int flags = O_RDWR | O_CREAT | O_TRUNC;
2028 memset(iov, 0, sizeof(struct iovec));
2029 memset(path, 0, 512);
2031 if (strlen(&wwn->unit_serial[0]) >= 512) {
2032 pr_err("WWN value for struct se_device does not fit"
2033 " into path buffer\n");
2037 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2038 file = filp_open(path, flags, 0600);
2039 if (IS_ERR(file) || !file || !file->f_dentry) {
2040 pr_err("filp_open(%s) for APTPL metadata"
2042 return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
2045 iov[0].iov_base = &buf[0];
2046 if (!pr_aptpl_buf_len)
2047 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2049 iov[0].iov_len = pr_aptpl_buf_len;
2053 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2057 pr_debug("Error writing APTPL metadata file: %s\n", path);
2058 filp_close(file, NULL);
2061 filp_close(file, NULL);
2066 static int core_scsi3_update_and_write_aptpl(
2067 struct se_device *dev,
2068 unsigned char *in_buf,
2069 u32 in_pr_aptpl_buf_len)
2071 unsigned char null_buf[64], *buf;
2072 u32 pr_aptpl_buf_len;
2073 int ret, clear_aptpl_metadata = 0;
2075 * Can be called with a NULL pointer from PROUT service action CLEAR
2078 memset(null_buf, 0, 64);
2081 * This will clear the APTPL metadata to:
2082 * "No Registrations or Reservations" status
2084 pr_aptpl_buf_len = 64;
2085 clear_aptpl_metadata = 1;
2088 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2091 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2092 clear_aptpl_metadata);
2096 * __core_scsi3_write_aptpl_to_file() will call strlen()
2097 * on the passed buf to determine pr_aptpl_buf_len.
2099 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2106 static int core_scsi3_emulate_pro_register(
2115 struct se_session *se_sess = cmd->se_sess;
2116 struct se_device *dev = cmd->se_dev;
2117 struct se_dev_entry *se_deve;
2118 struct se_lun *se_lun = cmd->se_lun;
2119 struct se_portal_group *se_tpg;
2120 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2121 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2122 /* Used for APTPL metadata w/ UNREGISTER */
2123 unsigned char *pr_aptpl_buf = NULL;
2124 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2125 int pr_holder = 0, ret = 0, type;
2127 if (!se_sess || !se_lun) {
2128 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2129 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2132 se_tpg = se_sess->se_tpg;
2133 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2135 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2136 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2137 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2139 isid_ptr = &isid_buf[0];
2142 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2144 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2147 pr_warn("SPC-3 PR: Reservation Key non-zero"
2148 " for SA REGISTER, returning CONFLICT\n");
2149 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2153 * Do nothing but return GOOD status.
2160 * Perform the Service Action REGISTER on the Initiator
2161 * Port Endpoint that the PRO was received from on the
2162 * Logical Unit of the SCSI device server.
2164 ret = core_scsi3_alloc_registration(cmd->se_dev,
2165 se_sess->se_node_acl, se_deve, isid_ptr,
2166 sa_res_key, all_tg_pt, aptpl,
2169 pr_err("Unable to allocate"
2170 " struct t10_pr_registration\n");
2171 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2176 * Register both the Initiator port that received
2177 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2178 * TransportID from Parameter list and loop through
2179 * fabric dependent parameter list while calling
2180 * logic from of core_scsi3_alloc_registration() for
2181 * each TransportID provided SCSI Initiator Port/Device
2183 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2184 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2189 * Nothing left to do for the APTPL=0 case.
2192 pr_tmpl->pr_aptpl_active = 0;
2193 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2194 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2199 * Locate the newly allocated local I_T Nexus *pr_reg, and
2200 * update the APTPL metadata information using its
2201 * preallocated *pr_reg->pr_aptpl_buf.
2203 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2204 se_sess->se_node_acl, se_sess);
2206 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2207 &pr_reg->pr_aptpl_buf[0],
2208 pr_tmpl->pr_aptpl_buf_len);
2210 pr_tmpl->pr_aptpl_active = 1;
2211 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2214 core_scsi3_put_pr_reg(pr_reg);
2218 * Locate the existing *pr_reg via struct se_node_acl pointers
2221 type = pr_reg->pr_res_type;
2224 if (res_key != pr_reg->pr_res_key) {
2225 pr_err("SPC-3 PR REGISTER: Received"
2226 " res_key: 0x%016Lx does not match"
2227 " existing SA REGISTER res_key:"
2228 " 0x%016Lx\n", res_key,
2229 pr_reg->pr_res_key);
2230 core_scsi3_put_pr_reg(pr_reg);
2231 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2236 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2237 " set while sa_res_key=0\n");
2238 core_scsi3_put_pr_reg(pr_reg);
2239 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2243 * An existing ALL_TG_PT=1 registration being released
2244 * must also set ALL_TG_PT=1 in the incoming PROUT.
2246 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2247 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2248 " registration exists, but ALL_TG_PT=1 bit not"
2249 " present in received PROUT\n");
2250 core_scsi3_put_pr_reg(pr_reg);
2251 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2255 * Allocate APTPL metadata buffer used for UNREGISTER ops
2258 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2260 if (!pr_aptpl_buf) {
2261 pr_err("Unable to allocate"
2263 core_scsi3_put_pr_reg(pr_reg);
2264 cmd->scsi_sense_reason =
2265 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2270 * sa_res_key=0 Unregister Reservation Key for registered I_T
2271 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2275 pr_holder = core_scsi3_check_implict_release(
2276 cmd->se_dev, pr_reg);
2277 if (pr_holder < 0) {
2278 kfree(pr_aptpl_buf);
2279 core_scsi3_put_pr_reg(pr_reg);
2280 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2284 spin_lock(&pr_tmpl->registration_lock);
2286 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2287 * and matching pr_res_key.
2289 if (pr_reg->pr_reg_all_tg_pt) {
2290 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2291 &pr_tmpl->registration_list,
2294 if (!pr_reg_p->pr_reg_all_tg_pt)
2297 if (pr_reg_p->pr_res_key != res_key)
2300 if (pr_reg == pr_reg_p)
2303 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2304 pr_reg_p->pr_reg_nacl->initiatorname))
2307 __core_scsi3_free_registration(dev,
2312 * Release the calling I_T Nexus registration now..
2314 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2317 * From spc4r17, section 5.7.11.3 Unregistering
2319 * If the persistent reservation is a registrants only
2320 * type, the device server shall establish a unit
2321 * attention condition for the initiator port associated
2322 * with every registered I_T nexus except for the I_T
2323 * nexus on which the PERSISTENT RESERVE OUT command was
2324 * received, with the additional sense code set to
2325 * RESERVATIONS RELEASED.
2328 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2329 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2330 list_for_each_entry(pr_reg_p,
2331 &pr_tmpl->registration_list,
2334 core_scsi3_ua_allocate(
2335 pr_reg_p->pr_reg_nacl,
2336 pr_reg_p->pr_res_mapped_lun,
2338 ASCQ_2AH_RESERVATIONS_RELEASED);
2341 spin_unlock(&pr_tmpl->registration_lock);
2344 pr_tmpl->pr_aptpl_active = 0;
2345 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2346 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2347 " for UNREGISTER\n");
2351 ret = core_scsi3_update_and_write_aptpl(dev,
2353 pr_tmpl->pr_aptpl_buf_len);
2355 pr_tmpl->pr_aptpl_active = 1;
2356 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2357 " for UNREGISTER\n");
2360 kfree(pr_aptpl_buf);
2364 * Increment PRgeneration counter for struct se_device"
2365 * upon a successful REGISTER, see spc4r17 section 6.3.2
2366 * READ_KEYS service action.
2368 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2370 pr_reg->pr_res_key = sa_res_key;
2371 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2372 " Key for %s to: 0x%016Lx PRgeneration:"
2373 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2374 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2375 pr_reg->pr_reg_nacl->initiatorname,
2376 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2379 pr_tmpl->pr_aptpl_active = 0;
2380 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2381 core_scsi3_put_pr_reg(pr_reg);
2382 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2387 ret = core_scsi3_update_and_write_aptpl(dev,
2389 pr_tmpl->pr_aptpl_buf_len);
2391 pr_tmpl->pr_aptpl_active = 1;
2392 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2396 kfree(pr_aptpl_buf);
2397 core_scsi3_put_pr_reg(pr_reg);
2403 unsigned char *core_scsi3_pr_dump_type(int type)
2406 case PR_TYPE_WRITE_EXCLUSIVE:
2407 return "Write Exclusive Access";
2408 case PR_TYPE_EXCLUSIVE_ACCESS:
2409 return "Exclusive Access";
2410 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2411 return "Write Exclusive Access, Registrants Only";
2412 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2413 return "Exclusive Access, Registrants Only";
2414 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2415 return "Write Exclusive Access, All Registrants";
2416 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2417 return "Exclusive Access, All Registrants";
2422 return "Unknown SPC-3 PR Type";
2425 static int core_scsi3_pro_reserve(
2427 struct se_device *dev,
2432 struct se_session *se_sess = cmd->se_sess;
2433 struct se_dev_entry *se_deve;
2434 struct se_lun *se_lun = cmd->se_lun;
2435 struct se_portal_group *se_tpg;
2436 struct t10_pr_registration *pr_reg, *pr_res_holder;
2437 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2438 char i_buf[PR_REG_ISID_ID_LEN];
2441 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2443 if (!se_sess || !se_lun) {
2444 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2445 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2448 se_tpg = se_sess->se_tpg;
2449 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2451 * Locate the existing *pr_reg via struct se_node_acl pointers
2453 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2456 pr_err("SPC-3 PR: Unable to locate"
2457 " PR_REGISTERED *pr_reg for RESERVE\n");
2458 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2462 * From spc4r17 Section 5.7.9: Reserving:
2464 * An application client creates a persistent reservation by issuing
2465 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2466 * a registered I_T nexus with the following parameters:
2467 * a) RESERVATION KEY set to the value of the reservation key that is
2468 * registered with the logical unit for the I_T nexus; and
2470 if (res_key != pr_reg->pr_res_key) {
2471 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2472 " does not match existing SA REGISTER res_key:"
2473 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2474 core_scsi3_put_pr_reg(pr_reg);
2475 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2479 * From spc4r17 Section 5.7.9: Reserving:
2482 * b) TYPE field and SCOPE field set to the persistent reservation
2485 * Only one persistent reservation is allowed at a time per logical unit
2486 * and that persistent reservation has a scope of LU_SCOPE.
2488 if (scope != PR_SCOPE_LU_SCOPE) {
2489 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2490 core_scsi3_put_pr_reg(pr_reg);
2491 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2495 * See if we have an existing PR reservation holder pointer at
2496 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2499 spin_lock(&dev->dev_reservation_lock);
2500 pr_res_holder = dev->dev_pr_res_holder;
2501 if ((pr_res_holder)) {
2503 * From spc4r17 Section 5.7.9: Reserving:
2505 * If the device server receives a PERSISTENT RESERVE OUT
2506 * command from an I_T nexus other than a persistent reservation
2507 * holder (see 5.7.10) that attempts to create a persistent
2508 * reservation when a persistent reservation already exists for
2509 * the logical unit, then the command shall be completed with
2510 * RESERVATION CONFLICT status.
2512 if (pr_res_holder != pr_reg) {
2513 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2514 pr_err("SPC-3 PR: Attempted RESERVE from"
2515 " [%s]: %s while reservation already held by"
2516 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2517 cmd->se_tfo->get_fabric_name(),
2518 se_sess->se_node_acl->initiatorname,
2519 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2520 pr_res_holder->pr_reg_nacl->initiatorname);
2522 spin_unlock(&dev->dev_reservation_lock);
2523 core_scsi3_put_pr_reg(pr_reg);
2524 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2528 * From spc4r17 Section 5.7.9: Reserving:
2530 * If a persistent reservation holder attempts to modify the
2531 * type or scope of an existing persistent reservation, the
2532 * command shall be completed with RESERVATION CONFLICT status.
2534 if ((pr_res_holder->pr_res_type != type) ||
2535 (pr_res_holder->pr_res_scope != scope)) {
2536 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2537 pr_err("SPC-3 PR: Attempted RESERVE from"
2538 " [%s]: %s trying to change TYPE and/or SCOPE,"
2539 " while reservation already held by [%s]: %s,"
2540 " returning RESERVATION_CONFLICT\n",
2541 cmd->se_tfo->get_fabric_name(),
2542 se_sess->se_node_acl->initiatorname,
2543 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2544 pr_res_holder->pr_reg_nacl->initiatorname);
2546 spin_unlock(&dev->dev_reservation_lock);
2547 core_scsi3_put_pr_reg(pr_reg);
2548 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2552 * From spc4r17 Section 5.7.9: Reserving:
2554 * If the device server receives a PERSISTENT RESERVE OUT
2555 * command with RESERVE service action where the TYPE field and
2556 * the SCOPE field contain the same values as the existing type
2557 * and scope from a persistent reservation holder, it shall not
2558 * make any change to the existing persistent reservation and
2559 * shall completethe command with GOOD status.
2561 spin_unlock(&dev->dev_reservation_lock);
2562 core_scsi3_put_pr_reg(pr_reg);
2566 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2567 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2569 pr_reg->pr_res_scope = scope;
2570 pr_reg->pr_res_type = type;
2571 pr_reg->pr_res_holder = 1;
2572 dev->dev_pr_res_holder = pr_reg;
2573 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2574 PR_REG_ISID_ID_LEN);
2576 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2577 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2578 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2579 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2580 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2581 cmd->se_tfo->get_fabric_name(),
2582 se_sess->se_node_acl->initiatorname,
2583 (prf_isid) ? &i_buf[0] : "");
2584 spin_unlock(&dev->dev_reservation_lock);
2586 if (pr_tmpl->pr_aptpl_active) {
2587 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2588 &pr_reg->pr_aptpl_buf[0],
2589 pr_tmpl->pr_aptpl_buf_len);
2591 pr_debug("SPC-3 PR: Updated APTPL metadata"
2595 core_scsi3_put_pr_reg(pr_reg);
2599 static int core_scsi3_emulate_pro_reserve(
2605 struct se_device *dev = cmd->se_dev;
2609 case PR_TYPE_WRITE_EXCLUSIVE:
2610 case PR_TYPE_EXCLUSIVE_ACCESS:
2611 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2612 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2613 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2614 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2615 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2618 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2620 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2628 * Called with struct se_device->dev_reservation_lock held.
2630 static void __core_scsi3_complete_pro_release(
2631 struct se_device *dev,
2632 struct se_node_acl *se_nacl,
2633 struct t10_pr_registration *pr_reg,
2636 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2637 char i_buf[PR_REG_ISID_ID_LEN];
2640 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2641 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2642 PR_REG_ISID_ID_LEN);
2644 * Go ahead and release the current PR reservation holder.
2646 dev->dev_pr_res_holder = NULL;
2648 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2649 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2650 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2651 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2652 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2653 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2654 tfo->get_fabric_name(), se_nacl->initiatorname,
2655 (prf_isid) ? &i_buf[0] : "");
2657 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2659 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2662 static int core_scsi3_emulate_pro_release(
2668 struct se_device *dev = cmd->se_dev;
2669 struct se_session *se_sess = cmd->se_sess;
2670 struct se_lun *se_lun = cmd->se_lun;
2671 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2672 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2673 int ret, all_reg = 0;
2675 if (!se_sess || !se_lun) {
2676 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2677 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2681 * Locate the existing *pr_reg via struct se_node_acl pointers
2683 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2685 pr_err("SPC-3 PR: Unable to locate"
2686 " PR_REGISTERED *pr_reg for RELEASE\n");
2687 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2691 * From spc4r17 Section 5.7.11.2 Releasing:
2693 * If there is no persistent reservation or in response to a persistent
2694 * reservation release request from a registered I_T nexus that is not a
2695 * persistent reservation holder (see 5.7.10), the device server shall
2698 * a) Not release the persistent reservation, if any;
2699 * b) Not remove any registrations; and
2700 * c) Complete the command with GOOD status.
2702 spin_lock(&dev->dev_reservation_lock);
2703 pr_res_holder = dev->dev_pr_res_holder;
2704 if (!pr_res_holder) {
2706 * No persistent reservation, return GOOD status.
2708 spin_unlock(&dev->dev_reservation_lock);
2709 core_scsi3_put_pr_reg(pr_reg);
2712 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2713 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2716 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2718 * Non 'All Registrants' PR Type cases..
2719 * Release request from a registered I_T nexus that is not a
2720 * persistent reservation holder. return GOOD status.
2722 spin_unlock(&dev->dev_reservation_lock);
2723 core_scsi3_put_pr_reg(pr_reg);
2727 * From spc4r17 Section 5.7.11.2 Releasing:
2729 * Only the persistent reservation holder (see 5.7.10) is allowed to
2730 * release a persistent reservation.
2732 * An application client releases the persistent reservation by issuing
2733 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2734 * an I_T nexus that is a persistent reservation holder with the
2735 * following parameters:
2737 * a) RESERVATION KEY field set to the value of the reservation key
2738 * that is registered with the logical unit for the I_T nexus;
2740 if (res_key != pr_reg->pr_res_key) {
2741 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2742 " does not match existing SA REGISTER res_key:"
2743 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2744 spin_unlock(&dev->dev_reservation_lock);
2745 core_scsi3_put_pr_reg(pr_reg);
2746 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2750 * From spc4r17 Section 5.7.11.2 Releasing and above:
2752 * b) TYPE field and SCOPE field set to match the persistent
2753 * reservation being released.
2755 if ((pr_res_holder->pr_res_type != type) ||
2756 (pr_res_holder->pr_res_scope != scope)) {
2757 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2758 pr_err("SPC-3 PR RELEASE: Attempted to release"
2759 " reservation from [%s]: %s with different TYPE "
2760 "and/or SCOPE while reservation already held by"
2761 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2762 cmd->se_tfo->get_fabric_name(),
2763 se_sess->se_node_acl->initiatorname,
2764 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2765 pr_res_holder->pr_reg_nacl->initiatorname);
2767 spin_unlock(&dev->dev_reservation_lock);
2768 core_scsi3_put_pr_reg(pr_reg);
2769 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2773 * In response to a persistent reservation release request from the
2774 * persistent reservation holder the device server shall perform a
2775 * release by doing the following as an uninterrupted series of actions:
2776 * a) Release the persistent reservation;
2777 * b) Not remove any registration(s);
2778 * c) If the released persistent reservation is a registrants only type
2779 * or all registrants type persistent reservation,
2780 * the device server shall establish a unit attention condition for
2781 * the initiator port associated with every regis-
2782 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2783 * RESERVE OUT command with RELEASE service action was received,
2784 * with the additional sense code set to RESERVATIONS RELEASED; and
2785 * d) If the persistent reservation is of any other type, the device
2786 * server shall not establish a unit attention condition.
2788 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2791 spin_unlock(&dev->dev_reservation_lock);
2793 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2794 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2795 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2796 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2798 * If no UNIT ATTENTION conditions will be established for
2799 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2800 * go ahead and check for APTPL=1 update+write below
2805 spin_lock(&pr_tmpl->registration_lock);
2806 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2809 * Do not establish a UNIT ATTENTION condition
2810 * for the calling I_T Nexus
2812 if (pr_reg_p == pr_reg)
2815 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2816 pr_reg_p->pr_res_mapped_lun,
2817 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2819 spin_unlock(&pr_tmpl->registration_lock);
2822 if (pr_tmpl->pr_aptpl_active) {
2823 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2824 &pr_reg->pr_aptpl_buf[0],
2825 pr_tmpl->pr_aptpl_buf_len);
2827 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2830 core_scsi3_put_pr_reg(pr_reg);
2834 static int core_scsi3_emulate_pro_clear(
2838 struct se_device *dev = cmd->se_dev;
2839 struct se_node_acl *pr_reg_nacl;
2840 struct se_session *se_sess = cmd->se_sess;
2841 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2842 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2843 u32 pr_res_mapped_lun = 0;
2844 int calling_it_nexus = 0;
2846 * Locate the existing *pr_reg via struct se_node_acl pointers
2848 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2849 se_sess->se_node_acl, se_sess);
2851 pr_err("SPC-3 PR: Unable to locate"
2852 " PR_REGISTERED *pr_reg for CLEAR\n");
2853 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2857 * From spc4r17 section 5.7.11.6, Clearing:
2859 * Any application client may release the persistent reservation and
2860 * remove all registrations from a device server by issuing a
2861 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2862 * registered I_T nexus with the following parameter:
2864 * a) RESERVATION KEY field set to the value of the reservation key
2865 * that is registered with the logical unit for the I_T nexus.
2867 if (res_key != pr_reg_n->pr_res_key) {
2868 pr_err("SPC-3 PR REGISTER: Received"
2869 " res_key: 0x%016Lx does not match"
2870 " existing SA REGISTER res_key:"
2871 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2872 core_scsi3_put_pr_reg(pr_reg_n);
2873 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2877 * a) Release the persistent reservation, if any;
2879 spin_lock(&dev->dev_reservation_lock);
2880 pr_res_holder = dev->dev_pr_res_holder;
2881 if (pr_res_holder) {
2882 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2883 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2886 spin_unlock(&dev->dev_reservation_lock);
2888 * b) Remove all registration(s) (see spc4r17 5.7.7);
2890 spin_lock(&pr_tmpl->registration_lock);
2891 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2892 &pr_tmpl->registration_list, pr_reg_list) {
2894 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2895 pr_reg_nacl = pr_reg->pr_reg_nacl;
2896 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2897 __core_scsi3_free_registration(dev, pr_reg, NULL,
2900 * e) Establish a unit attention condition for the initiator
2901 * port associated with every registered I_T nexus other
2902 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2903 * command with CLEAR service action was received, with the
2904 * additional sense code set to RESERVATIONS PREEMPTED.
2906 if (!calling_it_nexus)
2907 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2908 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2910 spin_unlock(&pr_tmpl->registration_lock);
2912 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2913 cmd->se_tfo->get_fabric_name());
2915 if (pr_tmpl->pr_aptpl_active) {
2916 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2917 pr_debug("SPC-3 PR: Updated APTPL metadata"
2921 core_scsi3_pr_generation(dev);
2926 * Called with struct se_device->dev_reservation_lock held.
2928 static void __core_scsi3_complete_pro_preempt(
2929 struct se_device *dev,
2930 struct t10_pr_registration *pr_reg,
2931 struct list_head *preempt_and_abort_list,
2936 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2937 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2938 char i_buf[PR_REG_ISID_ID_LEN];
2941 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2942 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2943 PR_REG_ISID_ID_LEN);
2945 * Do an implict RELEASE of the existing reservation.
2947 if (dev->dev_pr_res_holder)
2948 __core_scsi3_complete_pro_release(dev, nacl,
2949 dev->dev_pr_res_holder, 0);
2951 dev->dev_pr_res_holder = pr_reg;
2952 pr_reg->pr_res_holder = 1;
2953 pr_reg->pr_res_type = type;
2954 pr_reg->pr_res_scope = scope;
2956 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2957 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2958 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2959 core_scsi3_pr_dump_type(type),
2960 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2961 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2962 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2963 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2965 * For PREEMPT_AND_ABORT, add the preempting reservation's
2966 * struct t10_pr_registration to the list that will be compared
2967 * against received CDBs..
2969 if (preempt_and_abort_list)
2970 list_add_tail(&pr_reg->pr_reg_abort_list,
2971 preempt_and_abort_list);
2974 static void core_scsi3_release_preempt_and_abort(
2975 struct list_head *preempt_and_abort_list,
2976 struct t10_pr_registration *pr_reg_holder)
2978 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2980 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2981 pr_reg_abort_list) {
2983 list_del(&pr_reg->pr_reg_abort_list);
2984 if (pr_reg_holder == pr_reg)
2986 if (pr_reg->pr_res_holder) {
2987 pr_warn("pr_reg->pr_res_holder still set\n");
2991 pr_reg->pr_reg_deve = NULL;
2992 pr_reg->pr_reg_nacl = NULL;
2993 kfree(pr_reg->pr_aptpl_buf);
2994 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2998 int core_scsi3_check_cdb_abort_and_preempt(
2999 struct list_head *preempt_and_abort_list,
3002 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3004 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
3005 pr_reg_abort_list) {
3006 if (pr_reg->pr_res_key == cmd->pr_res_key)
3013 static int core_scsi3_pro_preempt(
3021 struct se_device *dev = cmd->se_dev;
3022 struct se_dev_entry *se_deve;
3023 struct se_node_acl *pr_reg_nacl;
3024 struct se_session *se_sess = cmd->se_sess;
3025 struct list_head preempt_and_abort_list;
3026 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
3027 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3028 u32 pr_res_mapped_lun = 0;
3029 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3030 int prh_type = 0, prh_scope = 0, ret;
3033 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3037 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3038 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3041 pr_err("SPC-3 PR: Unable to locate"
3042 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3043 (abort) ? "_AND_ABORT" : "");
3044 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3047 if (pr_reg_n->pr_res_key != res_key) {
3048 core_scsi3_put_pr_reg(pr_reg_n);
3049 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3052 if (scope != PR_SCOPE_LU_SCOPE) {
3053 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3054 core_scsi3_put_pr_reg(pr_reg_n);
3055 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3058 INIT_LIST_HEAD(&preempt_and_abort_list);
3060 spin_lock(&dev->dev_reservation_lock);
3061 pr_res_holder = dev->dev_pr_res_holder;
3062 if (pr_res_holder &&
3063 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3064 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3067 if (!all_reg && !sa_res_key) {
3068 spin_unlock(&dev->dev_reservation_lock);
3069 core_scsi3_put_pr_reg(pr_reg_n);
3070 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3074 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3076 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3077 * persistent reservation holder or there is no persistent reservation
3078 * holder (i.e., there is no persistent reservation), then the device
3079 * server shall perform a preempt by doing the following in an
3080 * uninterrupted series of actions. (See below..)
3082 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3084 * No existing or SA Reservation Key matching reservations..
3086 * PROUT SA PREEMPT with All Registrant type reservations are
3087 * allowed to be processed without a matching SA Reservation Key
3089 spin_lock(&pr_tmpl->registration_lock);
3090 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3091 &pr_tmpl->registration_list, pr_reg_list) {
3093 * Removing of registrations in non all registrants
3094 * type reservations without a matching SA reservation
3097 * a) Remove the registrations for all I_T nexuses
3098 * specified by the SERVICE ACTION RESERVATION KEY
3100 * b) Ignore the contents of the SCOPE and TYPE fields;
3101 * c) Process tasks as defined in 5.7.1; and
3102 * d) Establish a unit attention condition for the
3103 * initiator port associated with every I_T nexus
3104 * that lost its registration other than the I_T
3105 * nexus on which the PERSISTENT RESERVE OUT command
3106 * was received, with the additional sense code set
3107 * to REGISTRATIONS PREEMPTED.
3110 if (pr_reg->pr_res_key != sa_res_key)
3113 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3114 pr_reg_nacl = pr_reg->pr_reg_nacl;
3115 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3116 __core_scsi3_free_registration(dev, pr_reg,
3117 (abort) ? &preempt_and_abort_list :
3118 NULL, calling_it_nexus);
3122 * Case for any existing all registrants type
3123 * reservation, follow logic in spc4r17 section
3124 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3126 * For a ZERO SA Reservation key, release
3127 * all other registrations and do an implict
3128 * release of active persistent reservation.
3130 * For a non-ZERO SA Reservation key, only
3131 * release the matching reservation key from
3135 (pr_reg->pr_res_key != sa_res_key))
3138 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3139 if (calling_it_nexus)
3142 pr_reg_nacl = pr_reg->pr_reg_nacl;
3143 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3144 __core_scsi3_free_registration(dev, pr_reg,
3145 (abort) ? &preempt_and_abort_list :
3149 if (!calling_it_nexus)
3150 core_scsi3_ua_allocate(pr_reg_nacl,
3151 pr_res_mapped_lun, 0x2A,
3152 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3154 spin_unlock(&pr_tmpl->registration_lock);
3156 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3157 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3158 * RESERVATION KEY field to a value that does not match any
3159 * registered reservation key, then the device server shall
3160 * complete the command with RESERVATION CONFLICT status.
3162 if (!released_regs) {
3163 spin_unlock(&dev->dev_reservation_lock);
3164 core_scsi3_put_pr_reg(pr_reg_n);
3165 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3169 * For an existing all registrants type reservation
3170 * with a zero SA rservation key, preempt the existing
3171 * reservation with the new PR type and scope.
3173 if (pr_res_holder && all_reg && !(sa_res_key)) {
3174 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3175 (abort) ? &preempt_and_abort_list : NULL,
3176 type, scope, abort);
3179 core_scsi3_release_preempt_and_abort(
3180 &preempt_and_abort_list, pr_reg_n);
3182 spin_unlock(&dev->dev_reservation_lock);
3184 if (pr_tmpl->pr_aptpl_active) {
3185 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3186 &pr_reg_n->pr_aptpl_buf[0],
3187 pr_tmpl->pr_aptpl_buf_len);
3189 pr_debug("SPC-3 PR: Updated APTPL"
3190 " metadata for PREEMPT%s\n", (abort) ?
3194 core_scsi3_put_pr_reg(pr_reg_n);
3195 core_scsi3_pr_generation(cmd->se_dev);
3199 * The PREEMPTing SA reservation key matches that of the
3200 * existing persistent reservation, first, we check if
3201 * we are preempting our own reservation.
3202 * From spc4r17, section 5.7.11.4.3 Preempting
3203 * persistent reservations and registration handling
3205 * If an all registrants persistent reservation is not
3206 * present, it is not an error for the persistent
3207 * reservation holder to preempt itself (i.e., a
3208 * PERSISTENT RESERVE OUT with a PREEMPT service action
3209 * or a PREEMPT AND ABORT service action with the
3210 * SERVICE ACTION RESERVATION KEY value equal to the
3211 * persistent reservation holder's reservation key that
3212 * is received from the persistent reservation holder).
3213 * In that case, the device server shall establish the
3214 * new persistent reservation and maintain the
3217 prh_type = pr_res_holder->pr_res_type;
3218 prh_scope = pr_res_holder->pr_res_scope;
3220 * If the SERVICE ACTION RESERVATION KEY field identifies a
3221 * persistent reservation holder (see 5.7.10), the device
3222 * server shall perform a preempt by doing the following as
3223 * an uninterrupted series of actions:
3225 * a) Release the persistent reservation for the holder
3226 * identified by the SERVICE ACTION RESERVATION KEY field;
3228 if (pr_reg_n != pr_res_holder)
3229 __core_scsi3_complete_pro_release(dev,
3230 pr_res_holder->pr_reg_nacl,
3231 dev->dev_pr_res_holder, 0);
3233 * b) Remove the registrations for all I_T nexuses identified
3234 * by the SERVICE ACTION RESERVATION KEY field, except the
3235 * I_T nexus that is being used for the PERSISTENT RESERVE
3236 * OUT command. If an all registrants persistent reservation
3237 * is present and the SERVICE ACTION RESERVATION KEY field
3238 * is set to zero, then all registrations shall be removed
3239 * except for that of the I_T nexus that is being used for
3240 * the PERSISTENT RESERVE OUT command;
3242 spin_lock(&pr_tmpl->registration_lock);
3243 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3244 &pr_tmpl->registration_list, pr_reg_list) {
3246 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3247 if (calling_it_nexus)
3250 if (pr_reg->pr_res_key != sa_res_key)
3253 pr_reg_nacl = pr_reg->pr_reg_nacl;
3254 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3255 __core_scsi3_free_registration(dev, pr_reg,
3256 (abort) ? &preempt_and_abort_list : NULL,
3259 * e) Establish a unit attention condition for the initiator
3260 * port associated with every I_T nexus that lost its
3261 * persistent reservation and/or registration, with the
3262 * additional sense code set to REGISTRATIONS PREEMPTED;
3264 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3265 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3267 spin_unlock(&pr_tmpl->registration_lock);
3269 * c) Establish a persistent reservation for the preempting
3270 * I_T nexus using the contents of the SCOPE and TYPE fields;
3272 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3273 (abort) ? &preempt_and_abort_list : NULL,
3274 type, scope, abort);
3276 * d) Process tasks as defined in 5.7.1;
3278 * f) If the type or scope has changed, then for every I_T nexus
3279 * whose reservation key was not removed, except for the I_T
3280 * nexus on which the PERSISTENT RESERVE OUT command was
3281 * received, the device server shall establish a unit
3282 * attention condition for the initiator port associated with
3283 * that I_T nexus, with the additional sense code set to
3284 * RESERVATIONS RELEASED. If the type or scope have not
3285 * changed, then no unit attention condition(s) shall be
3286 * established for this reason.
3288 if ((prh_type != type) || (prh_scope != scope)) {
3289 spin_lock(&pr_tmpl->registration_lock);
3290 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3291 &pr_tmpl->registration_list, pr_reg_list) {
3293 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3294 if (calling_it_nexus)
3297 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3298 pr_reg->pr_res_mapped_lun, 0x2A,
3299 ASCQ_2AH_RESERVATIONS_RELEASED);
3301 spin_unlock(&pr_tmpl->registration_lock);
3303 spin_unlock(&dev->dev_reservation_lock);
3305 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3306 * All received CDBs for the matching existing reservation and
3307 * registrations undergo ABORT_TASK logic.
3309 * From there, core_scsi3_release_preempt_and_abort() will
3310 * release every registration in the list (which have already
3311 * been removed from the primary pr_reg list), except the
3312 * new persistent reservation holder, the calling Initiator Port.
3315 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3316 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3320 if (pr_tmpl->pr_aptpl_active) {
3321 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3322 &pr_reg_n->pr_aptpl_buf[0],
3323 pr_tmpl->pr_aptpl_buf_len);
3325 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3326 "%s\n", (abort) ? "_AND_ABORT" : "");
3329 core_scsi3_put_pr_reg(pr_reg_n);
3330 core_scsi3_pr_generation(cmd->se_dev);
3334 static int core_scsi3_emulate_pro_preempt(
3345 case PR_TYPE_WRITE_EXCLUSIVE:
3346 case PR_TYPE_EXCLUSIVE_ACCESS:
3347 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3348 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3349 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3350 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3351 ret = core_scsi3_pro_preempt(cmd, type, scope,
3352 res_key, sa_res_key, abort);
3355 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3356 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3357 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3365 static int core_scsi3_emulate_pro_register_and_move(
3372 struct se_session *se_sess = cmd->se_sess;
3373 struct se_device *dev = cmd->se_dev;
3374 struct se_dev_entry *se_deve, *dest_se_deve = NULL;
3375 struct se_lun *se_lun = cmd->se_lun;
3376 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3377 struct se_port *se_port;
3378 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3379 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3380 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3381 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3383 unsigned char *initiator_str;
3384 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3385 u32 tid_len, tmp_tid_len;
3386 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3387 unsigned short rtpi;
3388 unsigned char proto_ident;
3390 if (!se_sess || !se_lun) {
3391 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3392 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3395 memset(dest_iport, 0, 64);
3396 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3397 se_tpg = se_sess->se_tpg;
3398 tf_ops = se_tpg->se_tpg_tfo;
3399 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3401 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3402 * Register behaviors for a REGISTER AND MOVE service action
3404 * Locate the existing *pr_reg via struct se_node_acl pointers
3406 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3409 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3410 " *pr_reg for REGISTER_AND_MOVE\n");
3411 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3415 * The provided reservation key much match the existing reservation key
3416 * provided during this initiator's I_T nexus registration.
3418 if (res_key != pr_reg->pr_res_key) {
3419 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3420 " res_key: 0x%016Lx does not match existing SA REGISTER"
3421 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3422 core_scsi3_put_pr_reg(pr_reg);
3423 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3427 * The service active reservation key needs to be non zero
3430 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3432 core_scsi3_put_pr_reg(pr_reg);
3433 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3438 * Determine the Relative Target Port Identifier where the reservation
3439 * will be moved to for the TransportID containing SCSI initiator WWN
3442 buf = transport_kmap_data_sg(cmd);
3443 rtpi = (buf[18] & 0xff) << 8;
3444 rtpi |= buf[19] & 0xff;
3445 tid_len = (buf[20] & 0xff) << 24;
3446 tid_len |= (buf[21] & 0xff) << 16;
3447 tid_len |= (buf[22] & 0xff) << 8;
3448 tid_len |= buf[23] & 0xff;
3449 transport_kunmap_data_sg(cmd);
3452 if ((tid_len + 24) != cmd->data_length) {
3453 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3454 " does not equal CDB data_length: %u\n", tid_len,
3456 core_scsi3_put_pr_reg(pr_reg);
3457 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3461 spin_lock(&dev->se_port_lock);
3462 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3463 if (se_port->sep_rtpi != rtpi)
3465 dest_se_tpg = se_port->sep_tpg;
3468 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3472 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3473 smp_mb__after_atomic_inc();
3474 spin_unlock(&dev->se_port_lock);
3476 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3478 pr_err("core_scsi3_tpg_depend_item() failed"
3479 " for dest_se_tpg\n");
3480 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3481 smp_mb__after_atomic_dec();
3482 core_scsi3_put_pr_reg(pr_reg);
3483 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3487 spin_lock(&dev->se_port_lock);
3490 spin_unlock(&dev->se_port_lock);
3492 if (!dest_se_tpg || !dest_tf_ops) {
3493 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3494 " fabric ops from Relative Target Port Identifier:"
3496 core_scsi3_put_pr_reg(pr_reg);
3497 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3501 buf = transport_kmap_data_sg(cmd);
3502 proto_ident = (buf[24] & 0x0f);
3504 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3505 " 0x%02x\n", proto_ident);
3507 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3508 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3509 " proto_ident: 0x%02x does not match ident: 0x%02x"
3510 " from fabric: %s\n", proto_ident,
3511 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3512 dest_tf_ops->get_fabric_name());
3513 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3517 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3518 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3519 " containg a valid tpg_parse_pr_out_transport_id"
3520 " function pointer\n");
3521 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3525 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3526 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3527 if (!initiator_str) {
3528 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3529 " initiator_str from Transport ID\n");
3530 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3535 transport_kunmap_data_sg(cmd);
3538 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3539 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3540 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3543 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3544 * action specifies a TransportID that is the same as the initiator port
3545 * of the I_T nexus for the command received, then the command shall
3546 * be terminated with CHECK CONDITION status, with the sense key set to
3547 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3548 * IN PARAMETER LIST.
3550 pr_reg_nacl = pr_reg->pr_reg_nacl;
3551 matching_iname = (!strcmp(initiator_str,
3552 pr_reg_nacl->initiatorname)) ? 1 : 0;
3553 if (!matching_iname)
3554 goto after_iport_check;
3556 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3557 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3558 " matches: %s on received I_T Nexus\n", initiator_str,
3559 pr_reg_nacl->initiatorname);
3560 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3564 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3565 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3566 " matches: %s %s on received I_T Nexus\n",
3567 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3568 pr_reg->pr_reg_isid);
3569 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3575 * Locate the destination struct se_node_acl from the received Transport ID
3577 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3578 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3580 if (dest_node_acl) {
3581 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3582 smp_mb__after_atomic_inc();
3584 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3586 if (!dest_node_acl) {
3587 pr_err("Unable to locate %s dest_node_acl for"
3588 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3590 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3594 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3596 pr_err("core_scsi3_nodeacl_depend_item() for"
3597 " dest_node_acl\n");
3598 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3599 smp_mb__after_atomic_dec();
3600 dest_node_acl = NULL;
3601 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3606 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3607 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3608 dest_node_acl->initiatorname);
3611 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3614 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3615 if (!dest_se_deve) {
3616 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3617 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3618 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3623 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3625 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3626 atomic_dec(&dest_se_deve->pr_ref_count);
3627 smp_mb__after_atomic_dec();
3628 dest_se_deve = NULL;
3629 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3634 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3635 " ACL for dest_se_deve->mapped_lun: %u\n",
3636 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3637 dest_se_deve->mapped_lun);
3640 * A persistent reservation needs to already existing in order to
3641 * successfully complete the REGISTER_AND_MOVE service action..
3643 spin_lock(&dev->dev_reservation_lock);
3644 pr_res_holder = dev->dev_pr_res_holder;
3645 if (!pr_res_holder) {
3646 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3647 " currently held\n");
3648 spin_unlock(&dev->dev_reservation_lock);
3649 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3654 * The received on I_T Nexus must be the reservation holder.
3656 * From spc4r17 section 5.7.8 Table 50 --
3657 * Register behaviors for a REGISTER AND MOVE service action
3659 if (pr_res_holder != pr_reg) {
3660 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3661 " Nexus is not reservation holder\n");
3662 spin_unlock(&dev->dev_reservation_lock);
3663 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3668 * From spc4r17 section 5.7.8: registering and moving reservation
3670 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3671 * action is received and the established persistent reservation is a
3672 * Write Exclusive - All Registrants type or Exclusive Access -
3673 * All Registrants type reservation, then the command shall be completed
3674 * with RESERVATION CONFLICT status.
3676 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3677 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3678 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3679 " reservation for type: %s\n",
3680 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3681 spin_unlock(&dev->dev_reservation_lock);
3682 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3686 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3688 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3690 type = pr_res_holder->pr_res_type;
3691 scope = pr_res_holder->pr_res_type;
3693 * c) Associate the reservation key specified in the SERVICE ACTION
3694 * RESERVATION KEY field with the I_T nexus specified as the
3695 * destination of the register and move, where:
3696 * A) The I_T nexus is specified by the TransportID and the
3697 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3698 * B) Regardless of the TransportID format used, the association for
3699 * the initiator port is based on either the initiator port name
3700 * (see 3.1.71) on SCSI transport protocols where port names are
3701 * required or the initiator port identifier (see 3.1.70) on SCSI
3702 * transport protocols where port names are not required;
3703 * d) Register the reservation key specified in the SERVICE ACTION
3704 * RESERVATION KEY field;
3705 * e) Retain the reservation key specified in the SERVICE ACTION
3706 * RESERVATION KEY field and associated information;
3708 * Also, It is not an error for a REGISTER AND MOVE service action to
3709 * register an I_T nexus that is already registered with the same
3710 * reservation key or a different reservation key.
3712 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3715 ret = core_scsi3_alloc_registration(cmd->se_dev,
3716 dest_node_acl, dest_se_deve, iport_ptr,
3717 sa_res_key, 0, aptpl, 2, 1);
3719 spin_unlock(&dev->dev_reservation_lock);
3720 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3724 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3729 * f) Release the persistent reservation for the persistent reservation
3730 * holder (i.e., the I_T nexus on which the
3732 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3733 dev->dev_pr_res_holder, 0);
3735 * g) Move the persistent reservation to the specified I_T nexus using
3736 * the same scope and type as the persistent reservation released in
3739 dev->dev_pr_res_holder = dest_pr_reg;
3740 dest_pr_reg->pr_res_holder = 1;
3741 dest_pr_reg->pr_res_type = type;
3742 pr_reg->pr_res_scope = scope;
3743 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3744 PR_REG_ISID_ID_LEN);
3746 * Increment PRGeneration for existing registrations..
3749 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3750 spin_unlock(&dev->dev_reservation_lock);
3752 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3753 " created new reservation holder TYPE: %s on object RTPI:"
3754 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3755 core_scsi3_pr_dump_type(type), rtpi,
3756 dest_pr_reg->pr_res_generation);
3757 pr_debug("SPC-3 PR Successfully moved reservation from"
3758 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3759 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3760 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3761 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3764 * It is now safe to release configfs group dependencies for destination
3765 * of Transport ID Initiator Device/Port Identifier
3767 core_scsi3_lunacl_undepend_item(dest_se_deve);
3768 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3769 core_scsi3_tpg_undepend_item(dest_se_tpg);
3771 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3772 * nexus on which PERSISTENT RESERVE OUT command was received.
3775 spin_lock(&pr_tmpl->registration_lock);
3776 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3777 spin_unlock(&pr_tmpl->registration_lock);
3779 core_scsi3_put_pr_reg(pr_reg);
3782 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3783 * write out the updated metadata to struct file for this SCSI device.
3786 pr_tmpl->pr_aptpl_active = 0;
3787 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3788 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3789 " REGISTER_AND_MOVE\n");
3791 pr_tmpl->pr_aptpl_active = 1;
3792 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3793 &dest_pr_reg->pr_aptpl_buf[0],
3794 pr_tmpl->pr_aptpl_buf_len);
3796 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3797 " REGISTER_AND_MOVE\n");
3800 transport_kunmap_data_sg(cmd);
3802 core_scsi3_put_pr_reg(dest_pr_reg);
3806 transport_kunmap_data_sg(cmd);
3808 core_scsi3_lunacl_undepend_item(dest_se_deve);
3810 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3811 core_scsi3_tpg_undepend_item(dest_se_tpg);
3812 core_scsi3_put_pr_reg(pr_reg);
3816 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3818 unsigned int __v1, __v2;
3820 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3821 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3823 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3827 * See spc4r17 section 6.14 Table 170
3829 int target_scsi3_emulate_pr_out(struct se_task *task)
3831 struct se_cmd *cmd = task->task_se_cmd;
3832 unsigned char *cdb = &cmd->t_task_cdb[0];
3834 u64 res_key, sa_res_key;
3835 int sa, scope, type, aptpl;
3836 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3840 * Following spc2r20 5.5.1 Reservations overview:
3842 * If a logical unit has been reserved by any RESERVE command and is
3843 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3844 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3845 * initiator or service action and shall terminate with a RESERVATION
3848 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3849 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3850 " SPC-2 reservation is held, returning"
3851 " RESERVATION_CONFLICT\n");
3852 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3858 * FIXME: A NULL struct se_session pointer means an this is not coming from
3859 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3861 if (!cmd->se_sess) {
3862 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3866 if (cmd->data_length < 24) {
3867 pr_warn("SPC-PR: Received PR OUT parameter list"
3868 " length too small: %u\n", cmd->data_length);
3869 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3874 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3876 sa = (cdb[1] & 0x1f);
3877 scope = (cdb[2] & 0xf0);
3878 type = (cdb[2] & 0x0f);
3880 buf = transport_kmap_data_sg(cmd);
3882 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3884 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3885 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3887 * REGISTER_AND_MOVE uses a different SA parameter list containing
3888 * SCSI TransportIDs.
3890 if (sa != PRO_REGISTER_AND_MOVE) {
3891 spec_i_pt = (buf[20] & 0x08);
3892 all_tg_pt = (buf[20] & 0x04);
3893 aptpl = (buf[20] & 0x01);
3895 aptpl = (buf[17] & 0x01);
3896 unreg = (buf[17] & 0x02);
3898 transport_kunmap_data_sg(cmd);
3902 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3904 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3905 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3911 * From spc4r17 section 6.14:
3913 * If the SPEC_I_PT bit is set to zero, the service action is not
3914 * REGISTER AND MOVE, and the parameter list length is not 24, then
3915 * the command shall be terminated with CHECK CONDITION status, with
3916 * the sense key set to ILLEGAL REQUEST, and the additional sense
3917 * code set to PARAMETER LIST LENGTH ERROR.
3919 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3920 (cmd->data_length != 24)) {
3921 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3922 " list length: %u\n", cmd->data_length);
3923 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3928 * (core_scsi3_emulate_pro_* function parameters
3929 * are defined by spc4r17 Table 174:
3930 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3934 ret = core_scsi3_emulate_pro_register(cmd,
3935 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3938 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3941 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3944 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3947 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3948 res_key, sa_res_key, 0);
3950 case PRO_PREEMPT_AND_ABORT:
3951 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3952 res_key, sa_res_key, 1);
3954 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3955 ret = core_scsi3_emulate_pro_register(cmd,
3956 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3958 case PRO_REGISTER_AND_MOVE:
3959 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3960 sa_res_key, aptpl, unreg);
3963 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3964 " action: 0x%02x\n", cdb[1] & 0x1f);
3965 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3972 task->task_scsi_status = GOOD;
3973 transport_complete_task(task, 1);
3979 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3981 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3983 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3985 struct se_device *se_dev = cmd->se_dev;
3986 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
3987 struct t10_pr_registration *pr_reg;
3989 u32 add_len = 0, off = 8;
3991 if (cmd->data_length < 8) {
3992 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3993 " too small\n", cmd->data_length);
3994 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3998 buf = transport_kmap_data_sg(cmd);
3999 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4000 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4001 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4002 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4004 spin_lock(&su_dev->t10_pr.registration_lock);
4005 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
4008 * Check for overflow of 8byte PRI READ_KEYS payload and
4009 * next reservation key list descriptor.
4011 if ((add_len + 8) > (cmd->data_length - 8))
4014 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4015 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4016 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4017 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4018 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4019 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4020 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4021 buf[off++] = (pr_reg->pr_res_key & 0xff);
4025 spin_unlock(&su_dev->t10_pr.registration_lock);
4027 buf[4] = ((add_len >> 24) & 0xff);
4028 buf[5] = ((add_len >> 16) & 0xff);
4029 buf[6] = ((add_len >> 8) & 0xff);
4030 buf[7] = (add_len & 0xff);
4032 transport_kunmap_data_sg(cmd);
4038 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4040 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4042 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4044 struct se_device *se_dev = cmd->se_dev;
4045 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4046 struct t10_pr_registration *pr_reg;
4049 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4051 if (cmd->data_length < 8) {
4052 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4053 " too small\n", cmd->data_length);
4054 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4058 buf = transport_kmap_data_sg(cmd);
4059 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4060 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4061 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4062 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4064 spin_lock(&se_dev->dev_reservation_lock);
4065 pr_reg = se_dev->dev_pr_res_holder;
4068 * Set the hardcoded Additional Length
4070 buf[4] = ((add_len >> 24) & 0xff);
4071 buf[5] = ((add_len >> 16) & 0xff);
4072 buf[6] = ((add_len >> 8) & 0xff);
4073 buf[7] = (add_len & 0xff);
4075 if (cmd->data_length < 22)
4079 * Set the Reservation key.
4081 * From spc4r17, section 5.7.10:
4082 * A persistent reservation holder has its reservation key
4083 * returned in the parameter data from a PERSISTENT
4084 * RESERVE IN command with READ RESERVATION service action as
4086 * a) For a persistent reservation of the type Write Exclusive
4087 * - All Registrants or Exclusive Access  All Regitrants,
4088 * the reservation key shall be set to zero; or
4089 * b) For all other persistent reservation types, the
4090 * reservation key shall be set to the registered
4091 * reservation key for the I_T nexus that holds the
4092 * persistent reservation.
4094 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4095 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4098 pr_res_key = pr_reg->pr_res_key;
4100 buf[8] = ((pr_res_key >> 56) & 0xff);
4101 buf[9] = ((pr_res_key >> 48) & 0xff);
4102 buf[10] = ((pr_res_key >> 40) & 0xff);
4103 buf[11] = ((pr_res_key >> 32) & 0xff);
4104 buf[12] = ((pr_res_key >> 24) & 0xff);
4105 buf[13] = ((pr_res_key >> 16) & 0xff);
4106 buf[14] = ((pr_res_key >> 8) & 0xff);
4107 buf[15] = (pr_res_key & 0xff);
4109 * Set the SCOPE and TYPE
4111 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4112 (pr_reg->pr_res_type & 0x0f);
4116 spin_unlock(&se_dev->dev_reservation_lock);
4117 transport_kunmap_data_sg(cmd);
4123 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4125 * See spc4r17 section 6.13.4 Table 165
4127 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4129 struct se_device *dev = cmd->se_dev;
4130 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
4132 u16 add_len = 8; /* Hardcoded to 8. */
4134 if (cmd->data_length < 6) {
4135 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4136 " %u too small\n", cmd->data_length);
4137 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4141 buf = transport_kmap_data_sg(cmd);
4143 buf[0] = ((add_len << 8) & 0xff);
4144 buf[1] = (add_len & 0xff);
4145 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4146 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4147 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4148 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4150 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4151 * set the TMV: Task Mask Valid bit.
4155 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4157 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4159 * PTPL_A: Persistence across Target Power Loss Active bit
4161 if (pr_tmpl->pr_aptpl_active)
4164 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4166 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4167 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4168 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4169 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4170 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4171 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4173 transport_kunmap_data_sg(cmd);
4179 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4181 * See spc4r17 section 6.13.5 Table 168 and 169
4183 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4185 struct se_device *se_dev = cmd->se_dev;
4186 struct se_node_acl *se_nacl;
4187 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4188 struct se_portal_group *se_tpg;
4189 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4190 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
4192 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4193 u32 off = 8; /* off into first Full Status descriptor */
4194 int format_code = 0;
4196 if (cmd->data_length < 8) {
4197 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4198 " too small\n", cmd->data_length);
4199 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4203 buf = transport_kmap_data_sg(cmd);
4205 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4206 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4207 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4208 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4210 spin_lock(&pr_tmpl->registration_lock);
4211 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4212 &pr_tmpl->registration_list, pr_reg_list) {
4214 se_nacl = pr_reg->pr_reg_nacl;
4215 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4218 atomic_inc(&pr_reg->pr_res_holders);
4219 smp_mb__after_atomic_inc();
4220 spin_unlock(&pr_tmpl->registration_lock);
4222 * Determine expected length of $FABRIC_MOD specific
4223 * TransportID full status descriptor..
4225 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4226 se_tpg, se_nacl, pr_reg, &format_code);
4228 if ((exp_desc_len + add_len) > cmd->data_length) {
4229 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4230 " out of buffer: %d\n", cmd->data_length);
4231 spin_lock(&pr_tmpl->registration_lock);
4232 atomic_dec(&pr_reg->pr_res_holders);
4233 smp_mb__after_atomic_dec();
4237 * Set RESERVATION KEY
4239 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4240 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4241 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4242 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4243 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4244 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4245 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4246 buf[off++] = (pr_reg->pr_res_key & 0xff);
4247 off += 4; /* Skip Over Reserved area */
4250 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4252 if (pr_reg->pr_reg_all_tg_pt)
4255 * The struct se_lun pointer will be present for the
4256 * reservation holder for PR_HOLDER bit.
4258 * Also, if this registration is the reservation
4259 * holder, fill in SCOPE and TYPE in the next byte.
4261 if (pr_reg->pr_res_holder) {
4263 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4264 (pr_reg->pr_res_type & 0x0f);
4268 off += 4; /* Skip over reserved area */
4270 * From spc4r17 6.3.15:
4272 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4273 * IDENTIFIER field contains the relative port identifier (see
4274 * 3.1.120) of the target port that is part of the I_T nexus
4275 * described by this full status descriptor. If the ALL_TG_PT
4276 * bit is set to one, the contents of the RELATIVE TARGET PORT
4277 * IDENTIFIER field are not defined by this standard.
4279 if (!pr_reg->pr_reg_all_tg_pt) {
4280 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4282 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4283 buf[off++] = (port->sep_rtpi & 0xff);
4285 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4288 * Now, have the $FABRIC_MOD fill in the protocol identifier
4290 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4291 se_nacl, pr_reg, &format_code, &buf[off+4]);
4293 spin_lock(&pr_tmpl->registration_lock);
4294 atomic_dec(&pr_reg->pr_res_holders);
4295 smp_mb__after_atomic_dec();
4297 * Set the ADDITIONAL DESCRIPTOR LENGTH
4299 buf[off++] = ((desc_len >> 24) & 0xff);
4300 buf[off++] = ((desc_len >> 16) & 0xff);
4301 buf[off++] = ((desc_len >> 8) & 0xff);
4302 buf[off++] = (desc_len & 0xff);
4304 * Size of full desctipor header minus TransportID
4305 * containing $FABRIC_MOD specific) initiator device/port
4308 * See spc4r17 Section 6.13.5 Table 169
4310 add_desc_len = (24 + desc_len);
4313 add_len += add_desc_len;
4315 spin_unlock(&pr_tmpl->registration_lock);
4317 * Set ADDITIONAL_LENGTH
4319 buf[4] = ((add_len >> 24) & 0xff);
4320 buf[5] = ((add_len >> 16) & 0xff);
4321 buf[6] = ((add_len >> 8) & 0xff);
4322 buf[7] = (add_len & 0xff);
4324 transport_kunmap_data_sg(cmd);
4329 int target_scsi3_emulate_pr_in(struct se_task *task)
4331 struct se_cmd *cmd = task->task_se_cmd;
4335 * Following spc2r20 5.5.1 Reservations overview:
4337 * If a logical unit has been reserved by any RESERVE command and is
4338 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4339 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4340 * initiator or service action and shall terminate with a RESERVATION
4343 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4344 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4345 " SPC-2 reservation is held, returning"
4346 " RESERVATION_CONFLICT\n");
4347 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4351 switch (cmd->t_task_cdb[1] & 0x1f) {
4353 ret = core_scsi3_pri_read_keys(cmd);
4355 case PRI_READ_RESERVATION:
4356 ret = core_scsi3_pri_read_reservation(cmd);
4358 case PRI_REPORT_CAPABILITIES:
4359 ret = core_scsi3_pri_report_capabilities(cmd);
4361 case PRI_READ_FULL_STATUS:
4362 ret = core_scsi3_pri_read_full_status(cmd);
4365 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4366 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4367 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4373 task->task_scsi_status = GOOD;
4374 transport_complete_task(task, 1);
4379 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4384 static int core_pt_seq_non_holder(
4392 int core_setup_reservations(struct se_device *dev, int force_pt)
4394 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
4395 struct t10_reservation *rest = &su_dev->t10_pr;
4397 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4398 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4399 * cause a problem because libata and some SATA RAID HBAs appear
4400 * under Linux/SCSI, but to emulate reservations themselves.
4402 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4403 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
4404 rest->res_type = SPC_PASSTHROUGH;
4405 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4406 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4407 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4408 " emulation\n", dev->transport->name);
4412 * If SPC-3 or above is reported by real or emulated struct se_device,
4413 * use emulated Persistent Reservations.
4415 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4416 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4417 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4418 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4419 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4420 " emulation\n", dev->transport->name);
4422 rest->res_type = SPC2_RESERVATIONS;
4423 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4424 rest->pr_ops.t10_seq_non_holder =
4425 &core_scsi2_reservation_seq_non_holder;
4426 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4427 dev->transport->name);