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 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
229 dev->dev_reserved_node_acl = NULL;
230 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
231 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
232 dev->dev_res_bin_isid = 0;
233 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
235 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
236 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
237 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
238 sess->se_node_acl->initiatorname);
241 spin_unlock(&dev->dev_reservation_lock);
244 task->task_scsi_status = GOOD;
245 transport_complete_task(task, 1);
250 int target_scsi2_reservation_reserve(struct se_task *task)
252 struct se_cmd *cmd = task->task_se_cmd;
253 struct se_device *dev = cmd->se_dev;
254 struct se_session *sess = cmd->se_sess;
255 struct se_portal_group *tpg = sess->se_tpg;
258 if ((cmd->t_task_cdb[1] & 0x01) &&
259 (cmd->t_task_cdb[1] & 0x02)) {
260 pr_err("LongIO and Obselete Bits set, returning"
261 " ILLEGAL_REQUEST\n");
262 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
267 * This is currently the case for target_core_mod passthrough struct se_cmd
272 rc = target_check_scsi2_reservation_conflict(cmd);
276 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
282 spin_lock(&dev->dev_reservation_lock);
283 if (dev->dev_reserved_node_acl &&
284 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
285 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
286 tpg->se_tpg_tfo->get_fabric_name());
287 pr_err("Original reserver LUN: %u %s\n",
288 cmd->se_lun->unpacked_lun,
289 dev->dev_reserved_node_acl->initiatorname);
290 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
291 " from %s \n", cmd->se_lun->unpacked_lun,
292 cmd->se_deve->mapped_lun,
293 sess->se_node_acl->initiatorname);
294 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
299 dev->dev_reserved_node_acl = sess->se_node_acl;
300 dev->dev_flags |= DF_SPC2_RESERVATIONS;
301 if (sess->sess_bin_isid != 0) {
302 dev->dev_res_bin_isid = sess->sess_bin_isid;
303 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
305 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
306 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
307 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
308 sess->se_node_acl->initiatorname);
311 spin_unlock(&dev->dev_reservation_lock);
314 task->task_scsi_status = GOOD;
315 transport_complete_task(task, 1);
322 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
324 * This function is called by those initiator ports who are *NOT*
325 * the active PR reservation holder when a reservation is present.
327 static int core_scsi3_pr_seq_non_holder(
332 struct se_dev_entry *se_deve;
333 struct se_session *se_sess = cmd->se_sess;
334 int other_cdb = 0, ignore_reg;
335 int registered_nexus = 0, ret = 1; /* Conflict by default */
336 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
337 int we = 0; /* Write Exclusive */
338 int legacy = 0; /* Act like a legacy device and return
339 * RESERVATION CONFLICT on some CDBs */
341 * A legacy SPC-2 reservation is being held.
343 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
344 return core_scsi2_reservation_seq_non_holder(cmd,
347 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
349 * Determine if the registration should be ignored due to
350 * non-matching ISIDs in core_scsi3_pr_reservation_check().
352 ignore_reg = (pr_reg_type & 0x80000000);
354 pr_reg_type &= ~0x80000000;
356 switch (pr_reg_type) {
357 case PR_TYPE_WRITE_EXCLUSIVE:
359 case PR_TYPE_EXCLUSIVE_ACCESS:
361 * Some commands are only allowed for the persistent reservation
364 if ((se_deve->def_pr_registered) && !(ignore_reg))
365 registered_nexus = 1;
367 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
369 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
371 * Some commands are only allowed for registered I_T Nexuses.
374 if ((se_deve->def_pr_registered) && !(ignore_reg))
375 registered_nexus = 1;
377 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
379 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
381 * Each registered I_T Nexus is a reservation holder.
384 if ((se_deve->def_pr_registered) && !(ignore_reg))
385 registered_nexus = 1;
391 * Referenced from spc4r17 table 45 for *NON* PR holder access
394 case SECURITY_PROTOCOL_IN:
395 if (registered_nexus)
403 case RECEIVE_DIAGNOSTIC:
408 if (registered_nexus) {
412 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
414 case PERSISTENT_RESERVE_OUT:
416 * This follows PERSISTENT_RESERVE_OUT service actions that
417 * are allowed in the presence of various reservations.
418 * See spc4r17, table 46
420 switch (cdb[1] & 0x1f) {
423 case PRO_PREEMPT_AND_ABORT:
424 ret = (registered_nexus) ? 0 : 1;
427 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
430 case PRO_REGISTER_AND_MOVE:
435 ret = (registered_nexus) ? 0 : 1;
438 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
439 " action: 0x%02x\n", cdb[1] & 0x1f);
445 /* Handled by CRH=1 in target_scsi2_reservation_release() */
450 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
453 case TEST_UNIT_READY:
454 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
457 switch (cdb[1] & 0x1f) {
458 case MI_MANAGEMENT_PROTOCOL_IN:
459 if (registered_nexus) {
463 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
465 case MI_REPORT_SUPPORTED_OPERATION_CODES:
466 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
471 if (registered_nexus) {
475 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
477 case MI_REPORT_ALIASES:
478 case MI_REPORT_IDENTIFYING_INFORMATION:
479 case MI_REPORT_PRIORITY:
480 case MI_REPORT_TARGET_PGS:
481 case MI_REPORT_TIMESTAMP:
482 ret = 0; /* Allowed */
485 pr_err("Unknown MI Service Action: 0x%02x\n",
490 case ACCESS_CONTROL_IN:
491 case ACCESS_CONTROL_OUT:
494 case READ_MEDIA_SERIAL_NUMBER:
497 case PERSISTENT_RESERVE_IN:
498 ret = 0; /*/ Allowed CDBs */
505 * Case where the CDB is explicitly allowed in the above switch
508 if (!ret && !other_cdb) {
510 pr_debug("Allowing explict CDB: 0x%02x for %s"
511 " reservation holder\n", cdb[0],
512 core_scsi3_pr_dump_type(pr_reg_type));
517 * Check if write exclusive initiator ports *NOT* holding the
518 * WRITE_EXCLUSIVE_* reservation.
520 if ((we) && !(registered_nexus)) {
521 if (cmd->data_direction == DMA_TO_DEVICE) {
523 * Conflict for write exclusive
525 pr_debug("%s Conflict for unregistered nexus"
526 " %s CDB: 0x%02x to %s reservation\n",
527 transport_dump_cmd_direction(cmd),
528 se_sess->se_node_acl->initiatorname, cdb[0],
529 core_scsi3_pr_dump_type(pr_reg_type));
533 * Allow non WRITE CDBs for all Write Exclusive
534 * PR TYPEs to pass for registered and
535 * non-registered_nexuxes NOT holding the reservation.
537 * We only make noise for the unregisterd nexuses,
538 * as we expect registered non-reservation holding
539 * nexuses to issue CDBs.
542 if (!registered_nexus) {
543 pr_debug("Allowing implict CDB: 0x%02x"
544 " for %s reservation on unregistered"
546 core_scsi3_pr_dump_type(pr_reg_type));
551 } else if ((reg_only) || (all_reg)) {
552 if (registered_nexus) {
554 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
555 * allow commands from registered nexuses.
558 pr_debug("Allowing implict CDB: 0x%02x for %s"
559 " reservation\n", cdb[0],
560 core_scsi3_pr_dump_type(pr_reg_type));
565 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
566 " for %s reservation\n", transport_dump_cmd_direction(cmd),
567 (registered_nexus) ? "" : "un",
568 se_sess->se_node_acl->initiatorname, cdb[0],
569 core_scsi3_pr_dump_type(pr_reg_type));
571 return 1; /* Conflict by default */
574 static u32 core_scsi3_pr_generation(struct se_device *dev)
576 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
579 * PRGeneration field shall contain the value of a 32-bit wrapping
580 * counter mainted by the device server.
582 * Note that this is done regardless of Active Persist across
583 * Target PowerLoss (APTPL)
585 * See spc4r17 section 6.3.12 READ_KEYS service action
587 spin_lock(&dev->dev_reservation_lock);
588 prg = su_dev->t10_pr.pr_generation++;
589 spin_unlock(&dev->dev_reservation_lock);
594 static int core_scsi3_pr_reservation_check(
598 struct se_device *dev = cmd->se_dev;
599 struct se_session *sess = cmd->se_sess;
605 * A legacy SPC-2 reservation is being held.
607 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
608 return core_scsi2_reservation_check(cmd, pr_reg_type);
610 spin_lock(&dev->dev_reservation_lock);
611 if (!dev->dev_pr_res_holder) {
612 spin_unlock(&dev->dev_reservation_lock);
615 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
616 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
617 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
618 spin_unlock(&dev->dev_reservation_lock);
621 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
622 spin_unlock(&dev->dev_reservation_lock);
625 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
626 sess->sess_bin_isid) ? 0 : -EINVAL;
628 * Use bit in *pr_reg_type to notify ISID mismatch in
629 * core_scsi3_pr_seq_non_holder().
632 *pr_reg_type |= 0x80000000;
633 spin_unlock(&dev->dev_reservation_lock);
638 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
639 struct se_device *dev,
640 struct se_node_acl *nacl,
641 struct se_dev_entry *deve,
647 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
648 struct t10_pr_registration *pr_reg;
650 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
652 pr_err("Unable to allocate struct t10_pr_registration\n");
656 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
658 if (!pr_reg->pr_aptpl_buf) {
659 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
660 kmem_cache_free(t10_pr_reg_cache, pr_reg);
664 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
665 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
666 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
667 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
668 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
669 atomic_set(&pr_reg->pr_res_holders, 0);
670 pr_reg->pr_reg_nacl = nacl;
671 pr_reg->pr_reg_deve = deve;
672 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
673 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
674 pr_reg->pr_res_key = sa_res_key;
675 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
676 pr_reg->pr_reg_aptpl = aptpl;
677 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
679 * If an ISID value for this SCSI Initiator Port exists,
680 * save it to the registration now.
683 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
684 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
685 pr_reg->isid_present_at_reg = 1;
691 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
692 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
695 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
698 static struct t10_pr_registration *__core_scsi3_alloc_registration(
699 struct se_device *dev,
700 struct se_node_acl *nacl,
701 struct se_dev_entry *deve,
707 struct se_dev_entry *deve_tmp;
708 struct se_node_acl *nacl_tmp;
709 struct se_port *port, *port_tmp;
710 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
711 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
714 * Create a registration for the I_T Nexus upon which the
715 * PROUT REGISTER was received.
717 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
718 sa_res_key, all_tg_pt, aptpl);
722 * Return pointer to pr_reg for ALL_TG_PT=0
727 * Create list of matching SCSI Initiator Port registrations
730 spin_lock(&dev->se_port_lock);
731 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
732 atomic_inc(&port->sep_tg_pt_ref_cnt);
733 smp_mb__after_atomic_inc();
734 spin_unlock(&dev->se_port_lock);
736 spin_lock_bh(&port->sep_alua_lock);
737 list_for_each_entry(deve_tmp, &port->sep_alua_list,
740 * This pointer will be NULL for demo mode MappedLUNs
741 * that have not been make explict via a ConfigFS
742 * MappedLUN group for the SCSI Initiator Node ACL.
744 if (!deve_tmp->se_lun_acl)
747 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
749 * Skip the matching struct se_node_acl that is allocated
752 if (nacl == nacl_tmp)
755 * Only perform PR registrations for target ports on
756 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
759 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
762 * Look for a matching Initiator Node ACL in ASCII format
764 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
767 atomic_inc(&deve_tmp->pr_ref_count);
768 smp_mb__after_atomic_inc();
769 spin_unlock_bh(&port->sep_alua_lock);
771 * Grab a configfs group dependency that is released
772 * for the exception path at label out: below, or upon
773 * completion of adding ALL_TG_PT=1 registrations in
774 * __core_scsi3_add_registration()
776 ret = core_scsi3_lunacl_depend_item(deve_tmp);
778 pr_err("core_scsi3_lunacl_depend"
780 atomic_dec(&port->sep_tg_pt_ref_cnt);
781 smp_mb__after_atomic_dec();
782 atomic_dec(&deve_tmp->pr_ref_count);
783 smp_mb__after_atomic_dec();
787 * Located a matching SCSI Initiator Port on a different
788 * port, allocate the pr_reg_atp and attach it to the
789 * pr_reg->pr_reg_atp_list that will be processed once
790 * the original *pr_reg is processed in
791 * __core_scsi3_add_registration()
793 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
794 nacl_tmp, deve_tmp, NULL,
795 sa_res_key, all_tg_pt, aptpl);
797 atomic_dec(&port->sep_tg_pt_ref_cnt);
798 smp_mb__after_atomic_dec();
799 atomic_dec(&deve_tmp->pr_ref_count);
800 smp_mb__after_atomic_dec();
801 core_scsi3_lunacl_undepend_item(deve_tmp);
805 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
806 &pr_reg->pr_reg_atp_list);
807 spin_lock_bh(&port->sep_alua_lock);
809 spin_unlock_bh(&port->sep_alua_lock);
811 spin_lock(&dev->se_port_lock);
812 atomic_dec(&port->sep_tg_pt_ref_cnt);
813 smp_mb__after_atomic_dec();
815 spin_unlock(&dev->se_port_lock);
819 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
820 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
821 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
822 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
823 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
825 kmem_cache_free(t10_pr_reg_cache, pr_reg);
829 int core_scsi3_alloc_aptpl_registration(
830 struct t10_reservation *pr_tmpl,
832 unsigned char *i_port,
835 unsigned char *t_port,
842 struct t10_pr_registration *pr_reg;
844 if (!i_port || !t_port || !sa_res_key) {
845 pr_err("Illegal parameters for APTPL registration\n");
849 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
851 pr_err("Unable to allocate struct t10_pr_registration\n");
854 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
856 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
857 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
858 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
859 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
860 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
861 atomic_set(&pr_reg->pr_res_holders, 0);
862 pr_reg->pr_reg_nacl = NULL;
863 pr_reg->pr_reg_deve = NULL;
864 pr_reg->pr_res_mapped_lun = mapped_lun;
865 pr_reg->pr_aptpl_target_lun = target_lun;
866 pr_reg->pr_res_key = sa_res_key;
867 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
868 pr_reg->pr_reg_aptpl = 1;
869 pr_reg->pr_reg_tg_pt_lun = NULL;
870 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
871 pr_reg->pr_res_type = type;
873 * If an ISID value had been saved in APTPL metadata for this
874 * SCSI Initiator Port, restore it now.
877 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
878 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
879 pr_reg->isid_present_at_reg = 1;
882 * Copy the i_port and t_port information from caller.
884 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
885 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
886 pr_reg->pr_reg_tpgt = tpgt;
888 * Set pr_res_holder from caller, the pr_reg who is the reservation
889 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
890 * the Initiator Node LUN ACL from the fabric module is created for
893 pr_reg->pr_res_holder = res_holder;
895 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
896 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
897 " metadata\n", (res_holder) ? "+reservation" : "");
901 static void core_scsi3_aptpl_reserve(
902 struct se_device *dev,
903 struct se_portal_group *tpg,
904 struct se_node_acl *node_acl,
905 struct t10_pr_registration *pr_reg)
907 char i_buf[PR_REG_ISID_ID_LEN];
910 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
911 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
914 spin_lock(&dev->dev_reservation_lock);
915 dev->dev_pr_res_holder = pr_reg;
916 spin_unlock(&dev->dev_reservation_lock);
918 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
919 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
920 tpg->se_tpg_tfo->get_fabric_name(),
921 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
922 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
923 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
924 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
925 (prf_isid) ? &i_buf[0] : "");
928 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
929 struct t10_pr_registration *, int, int);
931 static int __core_scsi3_check_aptpl_registration(
932 struct se_device *dev,
933 struct se_portal_group *tpg,
936 struct se_node_acl *nacl,
937 struct se_dev_entry *deve)
939 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
940 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
941 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
942 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
945 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
946 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
948 * Copy Initiator Port information from struct se_node_acl
950 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
951 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
952 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
953 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
955 * Look for the matching registrations+reservation from those
956 * created from APTPL metadata. Note that multiple registrations
957 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
960 spin_lock(&pr_tmpl->aptpl_reg_lock);
961 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
963 if (!strcmp(pr_reg->pr_iport, i_port) &&
964 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
965 !(strcmp(pr_reg->pr_tport, t_port)) &&
966 (pr_reg->pr_reg_tpgt == tpgt) &&
967 (pr_reg->pr_aptpl_target_lun == target_lun)) {
969 pr_reg->pr_reg_nacl = nacl;
970 pr_reg->pr_reg_deve = deve;
971 pr_reg->pr_reg_tg_pt_lun = lun;
973 list_del(&pr_reg->pr_reg_aptpl_list);
974 spin_unlock(&pr_tmpl->aptpl_reg_lock);
976 * At this point all of the pointers in *pr_reg will
977 * be setup, so go ahead and add the registration.
980 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
982 * If this registration is the reservation holder,
983 * make that happen now..
985 if (pr_reg->pr_res_holder)
986 core_scsi3_aptpl_reserve(dev, tpg,
989 * Reenable pr_aptpl_active to accept new metadata
990 * updates once the SCSI device is active again..
992 spin_lock(&pr_tmpl->aptpl_reg_lock);
993 pr_tmpl->pr_aptpl_active = 1;
996 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1001 int core_scsi3_check_aptpl_registration(
1002 struct se_device *dev,
1003 struct se_portal_group *tpg,
1005 struct se_lun_acl *lun_acl)
1007 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1008 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
1009 struct se_dev_entry *deve = &nacl->device_list[lun_acl->mapped_lun];
1011 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1014 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1015 lun->unpacked_lun, nacl, deve);
1018 static void __core_scsi3_dump_registration(
1019 struct target_core_fabric_ops *tfo,
1020 struct se_device *dev,
1021 struct se_node_acl *nacl,
1022 struct t10_pr_registration *pr_reg,
1025 struct se_portal_group *se_tpg = nacl->se_tpg;
1026 char i_buf[PR_REG_ISID_ID_LEN];
1029 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1030 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1031 PR_REG_ISID_ID_LEN);
1033 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1034 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1035 "_AND_MOVE" : (register_type == 1) ?
1036 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1037 (prf_isid) ? i_buf : "");
1038 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1039 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1040 tfo->tpg_get_tag(se_tpg));
1041 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1042 " Port(s)\n", tfo->get_fabric_name(),
1043 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1044 dev->transport->name);
1045 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1046 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1047 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1048 pr_reg->pr_reg_aptpl);
1052 * this function can be called with struct se_device->dev_reservation_lock
1053 * when register_move = 1
1055 static void __core_scsi3_add_registration(
1056 struct se_device *dev,
1057 struct se_node_acl *nacl,
1058 struct t10_pr_registration *pr_reg,
1062 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1063 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1064 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1065 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1068 * Increment PRgeneration counter for struct se_device upon a successful
1069 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1071 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1072 * action, the struct se_device->dev_reservation_lock will already be held,
1073 * so we do not call core_scsi3_pr_generation() which grabs the lock
1076 pr_reg->pr_res_generation = (register_move) ?
1077 su_dev->t10_pr.pr_generation++ :
1078 core_scsi3_pr_generation(dev);
1080 spin_lock(&pr_tmpl->registration_lock);
1081 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1082 pr_reg->pr_reg_deve->def_pr_registered = 1;
1084 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1085 spin_unlock(&pr_tmpl->registration_lock);
1087 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1089 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1092 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1093 * allocated in __core_scsi3_alloc_registration()
1095 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1096 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1097 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1099 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1101 spin_lock(&pr_tmpl->registration_lock);
1102 list_add_tail(&pr_reg_tmp->pr_reg_list,
1103 &pr_tmpl->registration_list);
1104 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1106 __core_scsi3_dump_registration(tfo, dev,
1107 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1109 spin_unlock(&pr_tmpl->registration_lock);
1111 * Drop configfs group dependency reference from
1112 * __core_scsi3_alloc_registration()
1114 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1118 static int core_scsi3_alloc_registration(
1119 struct se_device *dev,
1120 struct se_node_acl *nacl,
1121 struct se_dev_entry *deve,
1122 unsigned char *isid,
1129 struct t10_pr_registration *pr_reg;
1131 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1132 sa_res_key, all_tg_pt, aptpl);
1136 __core_scsi3_add_registration(dev, nacl, pr_reg,
1137 register_type, register_move);
1141 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1142 struct se_device *dev,
1143 struct se_node_acl *nacl,
1144 unsigned char *isid)
1146 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1147 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1148 struct se_portal_group *tpg;
1150 spin_lock(&pr_tmpl->registration_lock);
1151 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1152 &pr_tmpl->registration_list, pr_reg_list) {
1154 * First look for a matching struct se_node_acl
1156 if (pr_reg->pr_reg_nacl != nacl)
1159 tpg = pr_reg->pr_reg_nacl->se_tpg;
1161 * If this registration does NOT contain a fabric provided
1162 * ISID, then we have found a match.
1164 if (!pr_reg->isid_present_at_reg) {
1166 * Determine if this SCSI device server requires that
1167 * SCSI Intiatior TransportID w/ ISIDs is enforced
1168 * for fabric modules (iSCSI) requiring them.
1170 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1171 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
1174 atomic_inc(&pr_reg->pr_res_holders);
1175 smp_mb__after_atomic_inc();
1176 spin_unlock(&pr_tmpl->registration_lock);
1180 * If the *pr_reg contains a fabric defined ISID for multi-value
1181 * SCSI Initiator Port TransportIDs, then we expect a valid
1182 * matching ISID to be provided by the local SCSI Initiator Port.
1186 if (strcmp(isid, pr_reg->pr_reg_isid))
1189 atomic_inc(&pr_reg->pr_res_holders);
1190 smp_mb__after_atomic_inc();
1191 spin_unlock(&pr_tmpl->registration_lock);
1194 spin_unlock(&pr_tmpl->registration_lock);
1199 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1200 struct se_device *dev,
1201 struct se_node_acl *nacl,
1202 struct se_session *sess)
1204 struct se_portal_group *tpg = nacl->se_tpg;
1205 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1207 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1208 memset(&buf[0], 0, PR_REG_ISID_LEN);
1209 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1214 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1217 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1219 atomic_dec(&pr_reg->pr_res_holders);
1220 smp_mb__after_atomic_dec();
1223 static int core_scsi3_check_implict_release(
1224 struct se_device *dev,
1225 struct t10_pr_registration *pr_reg)
1227 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1228 struct t10_pr_registration *pr_res_holder;
1231 spin_lock(&dev->dev_reservation_lock);
1232 pr_res_holder = dev->dev_pr_res_holder;
1233 if (!pr_res_holder) {
1234 spin_unlock(&dev->dev_reservation_lock);
1237 if (pr_res_holder == pr_reg) {
1239 * Perform an implict RELEASE if the registration that
1240 * is being released is holding the reservation.
1242 * From spc4r17, section 5.7.11.1:
1244 * e) If the I_T nexus is the persistent reservation holder
1245 * and the persistent reservation is not an all registrants
1246 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1247 * service action or REGISTER AND IGNORE EXISTING KEY
1248 * service action with the SERVICE ACTION RESERVATION KEY
1249 * field set to zero (see 5.7.11.3).
1251 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1254 * For 'All Registrants' reservation types, all existing
1255 * registrations are still processed as reservation holders
1256 * in core_scsi3_pr_seq_non_holder() after the initial
1257 * reservation holder is implictly released here.
1259 } else if (pr_reg->pr_reg_all_tg_pt &&
1260 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1261 pr_reg->pr_reg_nacl->initiatorname)) &&
1262 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1263 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1264 " UNREGISTER while existing reservation with matching"
1265 " key 0x%016Lx is present from another SCSI Initiator"
1266 " Port\n", pr_reg->pr_res_key);
1269 spin_unlock(&dev->dev_reservation_lock);
1275 * Called with struct t10_reservation->registration_lock held.
1277 static void __core_scsi3_free_registration(
1278 struct se_device *dev,
1279 struct t10_pr_registration *pr_reg,
1280 struct list_head *preempt_and_abort_list,
1283 struct target_core_fabric_ops *tfo =
1284 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1285 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1286 char i_buf[PR_REG_ISID_ID_LEN];
1289 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1290 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1291 PR_REG_ISID_ID_LEN);
1293 pr_reg->pr_reg_deve->def_pr_registered = 0;
1294 pr_reg->pr_reg_deve->pr_res_key = 0;
1295 list_del(&pr_reg->pr_reg_list);
1297 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1298 * so call core_scsi3_put_pr_reg() to decrement our reference.
1301 core_scsi3_put_pr_reg(pr_reg);
1303 * Wait until all reference from any other I_T nexuses for this
1304 * *pr_reg have been released. Because list_del() is called above,
1305 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1306 * count back to zero, and we release *pr_reg.
1308 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1309 spin_unlock(&pr_tmpl->registration_lock);
1310 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1311 tfo->get_fabric_name());
1313 spin_lock(&pr_tmpl->registration_lock);
1316 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1317 " Node: %s%s\n", tfo->get_fabric_name(),
1318 pr_reg->pr_reg_nacl->initiatorname,
1319 (prf_isid) ? &i_buf[0] : "");
1320 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1321 " Port(s)\n", tfo->get_fabric_name(),
1322 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1323 dev->transport->name);
1324 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1325 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1326 pr_reg->pr_res_generation);
1328 if (!preempt_and_abort_list) {
1329 pr_reg->pr_reg_deve = NULL;
1330 pr_reg->pr_reg_nacl = NULL;
1331 kfree(pr_reg->pr_aptpl_buf);
1332 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1336 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1337 * are released once the ABORT_TASK_SET has completed..
1339 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1342 void core_scsi3_free_pr_reg_from_nacl(
1343 struct se_device *dev,
1344 struct se_node_acl *nacl)
1346 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1347 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1349 * If the passed se_node_acl matches the reservation holder,
1350 * release the reservation.
1352 spin_lock(&dev->dev_reservation_lock);
1353 pr_res_holder = dev->dev_pr_res_holder;
1354 if ((pr_res_holder != NULL) &&
1355 (pr_res_holder->pr_reg_nacl == nacl))
1356 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1357 spin_unlock(&dev->dev_reservation_lock);
1359 * Release any registration associated with the struct se_node_acl.
1361 spin_lock(&pr_tmpl->registration_lock);
1362 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1363 &pr_tmpl->registration_list, pr_reg_list) {
1365 if (pr_reg->pr_reg_nacl != nacl)
1368 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1370 spin_unlock(&pr_tmpl->registration_lock);
1373 void core_scsi3_free_all_registrations(
1374 struct se_device *dev)
1376 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1377 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1379 spin_lock(&dev->dev_reservation_lock);
1380 pr_res_holder = dev->dev_pr_res_holder;
1381 if (pr_res_holder != NULL) {
1382 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1383 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1386 spin_unlock(&dev->dev_reservation_lock);
1388 spin_lock(&pr_tmpl->registration_lock);
1389 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1390 &pr_tmpl->registration_list, pr_reg_list) {
1392 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1394 spin_unlock(&pr_tmpl->registration_lock);
1396 spin_lock(&pr_tmpl->aptpl_reg_lock);
1397 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1398 pr_reg_aptpl_list) {
1399 list_del(&pr_reg->pr_reg_aptpl_list);
1400 kfree(pr_reg->pr_aptpl_buf);
1401 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1403 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1406 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1408 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1409 &tpg->tpg_group.cg_item);
1412 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1414 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1415 &tpg->tpg_group.cg_item);
1417 atomic_dec(&tpg->tpg_pr_ref_count);
1418 smp_mb__after_atomic_dec();
1421 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1423 struct se_portal_group *tpg = nacl->se_tpg;
1425 if (nacl->dynamic_node_acl)
1428 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1429 &nacl->acl_group.cg_item);
1432 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1434 struct se_portal_group *tpg = nacl->se_tpg;
1436 if (nacl->dynamic_node_acl) {
1437 atomic_dec(&nacl->acl_pr_ref_count);
1438 smp_mb__after_atomic_dec();
1442 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1443 &nacl->acl_group.cg_item);
1445 atomic_dec(&nacl->acl_pr_ref_count);
1446 smp_mb__after_atomic_dec();
1449 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1451 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1452 struct se_node_acl *nacl;
1453 struct se_portal_group *tpg;
1455 * For nacl->dynamic_node_acl=1
1460 nacl = lun_acl->se_lun_nacl;
1463 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1464 &lun_acl->se_lun_group.cg_item);
1467 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1469 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1470 struct se_node_acl *nacl;
1471 struct se_portal_group *tpg;
1473 * For nacl->dynamic_node_acl=1
1476 atomic_dec(&se_deve->pr_ref_count);
1477 smp_mb__after_atomic_dec();
1480 nacl = lun_acl->se_lun_nacl;
1483 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1484 &lun_acl->se_lun_group.cg_item);
1486 atomic_dec(&se_deve->pr_ref_count);
1487 smp_mb__after_atomic_dec();
1490 static int core_scsi3_decode_spec_i_port(
1492 struct se_portal_group *tpg,
1493 unsigned char *l_isid,
1498 struct se_device *dev = cmd->se_dev;
1499 struct se_port *tmp_port;
1500 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1501 struct se_session *se_sess = cmd->se_sess;
1502 struct se_node_acl *dest_node_acl = NULL;
1503 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1504 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1505 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1506 struct list_head tid_dest_list;
1507 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1508 struct target_core_fabric_ops *tmp_tf_ops;
1510 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1511 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1512 u32 tpdl, tid_len = 0;
1513 int ret, dest_local_nexus, prf_isid;
1516 memset(dest_iport, 0, 64);
1517 INIT_LIST_HEAD(&tid_dest_list);
1519 local_se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1521 * Allocate a struct pr_transport_id_holder and setup the
1522 * local_node_acl and local_se_deve pointers and add to
1523 * struct list_head tid_dest_list for add registration
1524 * processing in the loop of tid_dest_list below.
1526 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1528 pr_err("Unable to allocate tidh_new\n");
1529 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1532 INIT_LIST_HEAD(&tidh_new->dest_list);
1533 tidh_new->dest_tpg = tpg;
1534 tidh_new->dest_node_acl = se_sess->se_node_acl;
1535 tidh_new->dest_se_deve = local_se_deve;
1537 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1538 se_sess->se_node_acl, local_se_deve, l_isid,
1539 sa_res_key, all_tg_pt, aptpl);
1540 if (!local_pr_reg) {
1542 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1545 tidh_new->dest_pr_reg = local_pr_reg;
1547 * The local I_T nexus does not hold any configfs dependances,
1548 * so we set tid_h->dest_local_nexus=1 to prevent the
1549 * configfs_undepend_item() calls in the tid_dest_list loops below.
1551 tidh_new->dest_local_nexus = 1;
1552 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1554 buf = transport_kmap_data_sg(cmd);
1556 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1557 * first extract TransportID Parameter Data Length, and make sure
1558 * the value matches up to the SCSI expected data transfer length.
1560 tpdl = (buf[24] & 0xff) << 24;
1561 tpdl |= (buf[25] & 0xff) << 16;
1562 tpdl |= (buf[26] & 0xff) << 8;
1563 tpdl |= buf[27] & 0xff;
1565 if ((tpdl + 28) != cmd->data_length) {
1566 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1567 " does not equal CDB data_length: %u\n", tpdl,
1569 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1574 * Start processing the received transport IDs using the
1575 * receiving I_T Nexus portal's fabric dependent methods to
1576 * obtain the SCSI Initiator Port/Device Identifiers.
1581 proto_ident = (ptr[0] & 0x0f);
1584 spin_lock(&dev->se_port_lock);
1585 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1586 tmp_tpg = tmp_port->sep_tpg;
1589 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1592 if (!tmp_tf_ops->get_fabric_proto_ident ||
1593 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1596 * Look for the matching proto_ident provided by
1597 * the received TransportID
1599 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1600 if (tmp_proto_ident != proto_ident)
1602 dest_rtpi = tmp_port->sep_rtpi;
1604 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1605 tmp_tpg, (const char *)ptr, &tid_len,
1610 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1611 smp_mb__after_atomic_inc();
1612 spin_unlock(&dev->se_port_lock);
1614 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1616 pr_err(" core_scsi3_tpg_depend_item()"
1618 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1619 smp_mb__after_atomic_dec();
1620 cmd->scsi_sense_reason =
1621 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1626 * Locate the desination initiator ACL to be registered
1627 * from the decoded fabric module specific TransportID
1630 spin_lock_irq(&tmp_tpg->acl_node_lock);
1631 dest_node_acl = __core_tpg_get_initiator_node_acl(
1633 if (dest_node_acl) {
1634 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1635 smp_mb__after_atomic_inc();
1637 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1639 if (!dest_node_acl) {
1640 core_scsi3_tpg_undepend_item(tmp_tpg);
1641 spin_lock(&dev->se_port_lock);
1645 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1647 pr_err("configfs_depend_item() failed"
1648 " for dest_node_acl->acl_group\n");
1649 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1650 smp_mb__after_atomic_dec();
1651 core_scsi3_tpg_undepend_item(tmp_tpg);
1652 cmd->scsi_sense_reason =
1653 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1659 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1660 " %s Port RTPI: %hu\n",
1661 dest_tpg->se_tpg_tfo->get_fabric_name(),
1662 dest_node_acl->initiatorname, dest_rtpi);
1664 spin_lock(&dev->se_port_lock);
1667 spin_unlock(&dev->se_port_lock);
1670 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1672 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1677 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1678 " tid_len: %d for %s + %s\n",
1679 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1680 tpdl, tid_len, i_str, iport_ptr);
1682 if (tid_len > tpdl) {
1683 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1684 " %u for Transport ID: %s\n", tid_len, ptr);
1685 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1686 core_scsi3_tpg_undepend_item(dest_tpg);
1687 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1692 * Locate the desintation struct se_dev_entry pointer for matching
1693 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1696 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1698 if (!dest_se_deve) {
1699 pr_err("Unable to locate %s dest_se_deve"
1700 " from destination RTPI: %hu\n",
1701 dest_tpg->se_tpg_tfo->get_fabric_name(),
1704 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1705 core_scsi3_tpg_undepend_item(dest_tpg);
1706 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1711 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1713 pr_err("core_scsi3_lunacl_depend_item()"
1715 atomic_dec(&dest_se_deve->pr_ref_count);
1716 smp_mb__after_atomic_dec();
1717 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1718 core_scsi3_tpg_undepend_item(dest_tpg);
1719 cmd->scsi_sense_reason =
1720 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1725 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1726 " dest_se_deve mapped_lun: %u\n",
1727 dest_tpg->se_tpg_tfo->get_fabric_name(),
1728 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1731 * Skip any TransportIDs that already have a registration for
1734 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1737 core_scsi3_put_pr_reg(pr_reg_e);
1738 core_scsi3_lunacl_undepend_item(dest_se_deve);
1739 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1740 core_scsi3_tpg_undepend_item(dest_tpg);
1747 * Allocate a struct pr_transport_id_holder and setup
1748 * the dest_node_acl and dest_se_deve pointers for the
1751 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1754 pr_err("Unable to allocate tidh_new\n");
1755 core_scsi3_lunacl_undepend_item(dest_se_deve);
1756 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1757 core_scsi3_tpg_undepend_item(dest_tpg);
1758 cmd->scsi_sense_reason =
1759 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1763 INIT_LIST_HEAD(&tidh_new->dest_list);
1764 tidh_new->dest_tpg = dest_tpg;
1765 tidh_new->dest_node_acl = dest_node_acl;
1766 tidh_new->dest_se_deve = dest_se_deve;
1769 * Allocate, but do NOT add the registration for the
1770 * TransportID referenced SCSI Initiator port. This
1771 * done because of the following from spc4r17 in section
1772 * 6.14.3 wrt SPEC_I_PT:
1774 * "If a registration fails for any initiator port (e.g., if th
1775 * logical unit does not have enough resources available to
1776 * hold the registration information), no registrations shall be
1777 * made, and the command shall be terminated with
1778 * CHECK CONDITION status."
1780 * That means we call __core_scsi3_alloc_registration() here,
1781 * and then call __core_scsi3_add_registration() in the
1782 * 2nd loop which will never fail.
1784 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1785 dest_node_acl, dest_se_deve, iport_ptr,
1786 sa_res_key, all_tg_pt, aptpl);
1788 core_scsi3_lunacl_undepend_item(dest_se_deve);
1789 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1790 core_scsi3_tpg_undepend_item(dest_tpg);
1792 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1796 tidh_new->dest_pr_reg = dest_pr_reg;
1797 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1805 transport_kunmap_data_sg(cmd);
1808 * Go ahead and create a registrations from tid_dest_list for the
1809 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1812 * The SA Reservation Key from the PROUT is set for the
1813 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1814 * means that the TransportID Initiator port will be
1815 * registered on all of the target ports in the SCSI target device
1816 * ALL_TG_PT=0 means the registration will only be for the
1817 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1820 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1821 dest_tpg = tidh->dest_tpg;
1822 dest_node_acl = tidh->dest_node_acl;
1823 dest_se_deve = tidh->dest_se_deve;
1824 dest_pr_reg = tidh->dest_pr_reg;
1825 dest_local_nexus = tidh->dest_local_nexus;
1827 list_del(&tidh->dest_list);
1830 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1831 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1832 PR_REG_ISID_ID_LEN);
1834 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1837 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1838 " registered Transport ID for Node: %s%s Mapped LUN:"
1839 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1840 dest_node_acl->initiatorname, (prf_isid) ?
1841 &i_buf[0] : "", dest_se_deve->mapped_lun);
1843 if (dest_local_nexus)
1846 core_scsi3_lunacl_undepend_item(dest_se_deve);
1847 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1848 core_scsi3_tpg_undepend_item(dest_tpg);
1853 transport_kunmap_data_sg(cmd);
1855 * For the failure case, release everything from tid_dest_list
1856 * including *dest_pr_reg and the configfs dependances..
1858 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1859 dest_tpg = tidh->dest_tpg;
1860 dest_node_acl = tidh->dest_node_acl;
1861 dest_se_deve = tidh->dest_se_deve;
1862 dest_pr_reg = tidh->dest_pr_reg;
1863 dest_local_nexus = tidh->dest_local_nexus;
1865 list_del(&tidh->dest_list);
1868 * Release any extra ALL_TG_PT=1 registrations for
1869 * the SPEC_I_PT=1 case.
1871 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1872 &dest_pr_reg->pr_reg_atp_list,
1873 pr_reg_atp_mem_list) {
1874 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1875 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1876 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1879 kfree(dest_pr_reg->pr_aptpl_buf);
1880 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1882 if (dest_local_nexus)
1885 core_scsi3_lunacl_undepend_item(dest_se_deve);
1886 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1887 core_scsi3_tpg_undepend_item(dest_tpg);
1893 * Called with struct se_device->dev_reservation_lock held
1895 static int __core_scsi3_update_aptpl_buf(
1896 struct se_device *dev,
1898 u32 pr_aptpl_buf_len,
1899 int clear_aptpl_metadata)
1902 struct se_portal_group *tpg;
1903 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1904 struct t10_pr_registration *pr_reg;
1905 unsigned char tmp[512], isid_buf[32];
1909 memset(buf, 0, pr_aptpl_buf_len);
1911 * Called to clear metadata once APTPL has been deactivated.
1913 if (clear_aptpl_metadata) {
1914 snprintf(buf, pr_aptpl_buf_len,
1915 "No Registrations or Reservations\n");
1919 * Walk the registration list..
1921 spin_lock(&su_dev->t10_pr.registration_lock);
1922 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1927 tpg = pr_reg->pr_reg_nacl->se_tpg;
1928 lun = pr_reg->pr_reg_tg_pt_lun;
1930 * Write out any ISID value to APTPL metadata that was included
1931 * in the original registration.
1933 if (pr_reg->isid_present_at_reg)
1934 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1935 pr_reg->pr_reg_isid);
1937 * Include special metadata if the pr_reg matches the
1938 * reservation holder.
1940 if (dev->dev_pr_res_holder == pr_reg) {
1941 snprintf(tmp, 512, "PR_REG_START: %d"
1942 "\ninitiator_fabric=%s\n"
1943 "initiator_node=%s\n%s"
1945 "res_holder=1\nres_type=%02x\n"
1946 "res_scope=%02x\nres_all_tg_pt=%d\n"
1947 "mapped_lun=%u\n", reg_count,
1948 tpg->se_tpg_tfo->get_fabric_name(),
1949 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1950 pr_reg->pr_res_key, pr_reg->pr_res_type,
1951 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1952 pr_reg->pr_res_mapped_lun);
1954 snprintf(tmp, 512, "PR_REG_START: %d\n"
1955 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1956 "sa_res_key=%llu\nres_holder=0\n"
1957 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1958 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1959 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1960 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1961 pr_reg->pr_res_mapped_lun);
1964 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1965 pr_err("Unable to update renaming"
1966 " APTPL metadata\n");
1967 spin_unlock(&su_dev->t10_pr.registration_lock);
1970 len += sprintf(buf+len, "%s", tmp);
1973 * Include information about the associated SCSI target port.
1975 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1976 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1977 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1978 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1979 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1980 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1982 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1983 pr_err("Unable to update renaming"
1984 " APTPL metadata\n");
1985 spin_unlock(&su_dev->t10_pr.registration_lock);
1988 len += sprintf(buf+len, "%s", tmp);
1991 spin_unlock(&su_dev->t10_pr.registration_lock);
1994 len += sprintf(buf+len, "No Registrations or Reservations");
1999 static int core_scsi3_update_aptpl_buf(
2000 struct se_device *dev,
2002 u32 pr_aptpl_buf_len,
2003 int clear_aptpl_metadata)
2007 spin_lock(&dev->dev_reservation_lock);
2008 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2009 clear_aptpl_metadata);
2010 spin_unlock(&dev->dev_reservation_lock);
2016 * Called with struct se_device->aptpl_file_mutex held
2018 static int __core_scsi3_write_aptpl_to_file(
2019 struct se_device *dev,
2021 u32 pr_aptpl_buf_len)
2023 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
2025 struct iovec iov[1];
2026 mm_segment_t old_fs;
2027 int flags = O_RDWR | O_CREAT | O_TRUNC;
2031 memset(iov, 0, sizeof(struct iovec));
2032 memset(path, 0, 512);
2034 if (strlen(&wwn->unit_serial[0]) >= 512) {
2035 pr_err("WWN value for struct se_device does not fit"
2036 " into path buffer\n");
2040 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2041 file = filp_open(path, flags, 0600);
2042 if (IS_ERR(file) || !file || !file->f_dentry) {
2043 pr_err("filp_open(%s) for APTPL metadata"
2045 return IS_ERR(file) ? PTR_ERR(file) : -ENOENT;
2048 iov[0].iov_base = &buf[0];
2049 if (!pr_aptpl_buf_len)
2050 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2052 iov[0].iov_len = pr_aptpl_buf_len;
2056 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2060 pr_debug("Error writing APTPL metadata file: %s\n", path);
2061 filp_close(file, NULL);
2064 filp_close(file, NULL);
2069 static int core_scsi3_update_and_write_aptpl(
2070 struct se_device *dev,
2071 unsigned char *in_buf,
2072 u32 in_pr_aptpl_buf_len)
2074 unsigned char null_buf[64], *buf;
2075 u32 pr_aptpl_buf_len;
2076 int ret, clear_aptpl_metadata = 0;
2078 * Can be called with a NULL pointer from PROUT service action CLEAR
2081 memset(null_buf, 0, 64);
2084 * This will clear the APTPL metadata to:
2085 * "No Registrations or Reservations" status
2087 pr_aptpl_buf_len = 64;
2088 clear_aptpl_metadata = 1;
2091 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2094 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2095 clear_aptpl_metadata);
2099 * __core_scsi3_write_aptpl_to_file() will call strlen()
2100 * on the passed buf to determine pr_aptpl_buf_len.
2102 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2109 static int core_scsi3_emulate_pro_register(
2118 struct se_session *se_sess = cmd->se_sess;
2119 struct se_device *dev = cmd->se_dev;
2120 struct se_dev_entry *se_deve;
2121 struct se_lun *se_lun = cmd->se_lun;
2122 struct se_portal_group *se_tpg;
2123 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2124 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2125 /* Used for APTPL metadata w/ UNREGISTER */
2126 unsigned char *pr_aptpl_buf = NULL;
2127 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2128 int pr_holder = 0, ret = 0, type;
2130 if (!se_sess || !se_lun) {
2131 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2132 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2135 se_tpg = se_sess->se_tpg;
2136 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2138 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2139 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2140 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2142 isid_ptr = &isid_buf[0];
2145 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2147 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2150 pr_warn("SPC-3 PR: Reservation Key non-zero"
2151 " for SA REGISTER, returning CONFLICT\n");
2152 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2156 * Do nothing but return GOOD status.
2163 * Perform the Service Action REGISTER on the Initiator
2164 * Port Endpoint that the PRO was received from on the
2165 * Logical Unit of the SCSI device server.
2167 ret = core_scsi3_alloc_registration(cmd->se_dev,
2168 se_sess->se_node_acl, se_deve, isid_ptr,
2169 sa_res_key, all_tg_pt, aptpl,
2172 pr_err("Unable to allocate"
2173 " struct t10_pr_registration\n");
2174 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2179 * Register both the Initiator port that received
2180 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2181 * TransportID from Parameter list and loop through
2182 * fabric dependent parameter list while calling
2183 * logic from of core_scsi3_alloc_registration() for
2184 * each TransportID provided SCSI Initiator Port/Device
2186 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2187 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2192 * Nothing left to do for the APTPL=0 case.
2195 pr_tmpl->pr_aptpl_active = 0;
2196 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2197 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2202 * Locate the newly allocated local I_T Nexus *pr_reg, and
2203 * update the APTPL metadata information using its
2204 * preallocated *pr_reg->pr_aptpl_buf.
2206 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2207 se_sess->se_node_acl, se_sess);
2209 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2210 &pr_reg->pr_aptpl_buf[0],
2211 pr_tmpl->pr_aptpl_buf_len);
2213 pr_tmpl->pr_aptpl_active = 1;
2214 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2217 core_scsi3_put_pr_reg(pr_reg);
2221 * Locate the existing *pr_reg via struct se_node_acl pointers
2224 type = pr_reg->pr_res_type;
2227 if (res_key != pr_reg->pr_res_key) {
2228 pr_err("SPC-3 PR REGISTER: Received"
2229 " res_key: 0x%016Lx does not match"
2230 " existing SA REGISTER res_key:"
2231 " 0x%016Lx\n", res_key,
2232 pr_reg->pr_res_key);
2233 core_scsi3_put_pr_reg(pr_reg);
2234 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2239 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2240 " set while sa_res_key=0\n");
2241 core_scsi3_put_pr_reg(pr_reg);
2242 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2246 * An existing ALL_TG_PT=1 registration being released
2247 * must also set ALL_TG_PT=1 in the incoming PROUT.
2249 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2250 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2251 " registration exists, but ALL_TG_PT=1 bit not"
2252 " present in received PROUT\n");
2253 core_scsi3_put_pr_reg(pr_reg);
2254 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2258 * Allocate APTPL metadata buffer used for UNREGISTER ops
2261 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2263 if (!pr_aptpl_buf) {
2264 pr_err("Unable to allocate"
2266 core_scsi3_put_pr_reg(pr_reg);
2267 cmd->scsi_sense_reason =
2268 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2273 * sa_res_key=0 Unregister Reservation Key for registered I_T
2274 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2278 pr_holder = core_scsi3_check_implict_release(
2279 cmd->se_dev, pr_reg);
2280 if (pr_holder < 0) {
2281 kfree(pr_aptpl_buf);
2282 core_scsi3_put_pr_reg(pr_reg);
2283 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2287 spin_lock(&pr_tmpl->registration_lock);
2289 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2290 * and matching pr_res_key.
2292 if (pr_reg->pr_reg_all_tg_pt) {
2293 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2294 &pr_tmpl->registration_list,
2297 if (!pr_reg_p->pr_reg_all_tg_pt)
2300 if (pr_reg_p->pr_res_key != res_key)
2303 if (pr_reg == pr_reg_p)
2306 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2307 pr_reg_p->pr_reg_nacl->initiatorname))
2310 __core_scsi3_free_registration(dev,
2315 * Release the calling I_T Nexus registration now..
2317 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2320 * From spc4r17, section 5.7.11.3 Unregistering
2322 * If the persistent reservation is a registrants only
2323 * type, the device server shall establish a unit
2324 * attention condition for the initiator port associated
2325 * with every registered I_T nexus except for the I_T
2326 * nexus on which the PERSISTENT RESERVE OUT command was
2327 * received, with the additional sense code set to
2328 * RESERVATIONS RELEASED.
2331 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2332 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2333 list_for_each_entry(pr_reg_p,
2334 &pr_tmpl->registration_list,
2337 core_scsi3_ua_allocate(
2338 pr_reg_p->pr_reg_nacl,
2339 pr_reg_p->pr_res_mapped_lun,
2341 ASCQ_2AH_RESERVATIONS_RELEASED);
2344 spin_unlock(&pr_tmpl->registration_lock);
2347 pr_tmpl->pr_aptpl_active = 0;
2348 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2349 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2350 " for UNREGISTER\n");
2354 ret = core_scsi3_update_and_write_aptpl(dev,
2356 pr_tmpl->pr_aptpl_buf_len);
2358 pr_tmpl->pr_aptpl_active = 1;
2359 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2360 " for UNREGISTER\n");
2363 kfree(pr_aptpl_buf);
2367 * Increment PRgeneration counter for struct se_device"
2368 * upon a successful REGISTER, see spc4r17 section 6.3.2
2369 * READ_KEYS service action.
2371 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2373 pr_reg->pr_res_key = sa_res_key;
2374 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2375 " Key for %s to: 0x%016Lx PRgeneration:"
2376 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2377 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2378 pr_reg->pr_reg_nacl->initiatorname,
2379 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2382 pr_tmpl->pr_aptpl_active = 0;
2383 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2384 core_scsi3_put_pr_reg(pr_reg);
2385 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2390 ret = core_scsi3_update_and_write_aptpl(dev,
2392 pr_tmpl->pr_aptpl_buf_len);
2394 pr_tmpl->pr_aptpl_active = 1;
2395 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2399 kfree(pr_aptpl_buf);
2400 core_scsi3_put_pr_reg(pr_reg);
2406 unsigned char *core_scsi3_pr_dump_type(int type)
2409 case PR_TYPE_WRITE_EXCLUSIVE:
2410 return "Write Exclusive Access";
2411 case PR_TYPE_EXCLUSIVE_ACCESS:
2412 return "Exclusive Access";
2413 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2414 return "Write Exclusive Access, Registrants Only";
2415 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2416 return "Exclusive Access, Registrants Only";
2417 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2418 return "Write Exclusive Access, All Registrants";
2419 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2420 return "Exclusive Access, All Registrants";
2425 return "Unknown SPC-3 PR Type";
2428 static int core_scsi3_pro_reserve(
2430 struct se_device *dev,
2435 struct se_session *se_sess = cmd->se_sess;
2436 struct se_dev_entry *se_deve;
2437 struct se_lun *se_lun = cmd->se_lun;
2438 struct se_portal_group *se_tpg;
2439 struct t10_pr_registration *pr_reg, *pr_res_holder;
2440 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2441 char i_buf[PR_REG_ISID_ID_LEN];
2444 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2446 if (!se_sess || !se_lun) {
2447 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2448 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2451 se_tpg = se_sess->se_tpg;
2452 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2454 * Locate the existing *pr_reg via struct se_node_acl pointers
2456 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2459 pr_err("SPC-3 PR: Unable to locate"
2460 " PR_REGISTERED *pr_reg for RESERVE\n");
2461 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2465 * From spc4r17 Section 5.7.9: Reserving:
2467 * An application client creates a persistent reservation by issuing
2468 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2469 * a registered I_T nexus with the following parameters:
2470 * a) RESERVATION KEY set to the value of the reservation key that is
2471 * registered with the logical unit for the I_T nexus; and
2473 if (res_key != pr_reg->pr_res_key) {
2474 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2475 " does not match existing SA REGISTER res_key:"
2476 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2477 core_scsi3_put_pr_reg(pr_reg);
2478 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2482 * From spc4r17 Section 5.7.9: Reserving:
2485 * b) TYPE field and SCOPE field set to the persistent reservation
2488 * Only one persistent reservation is allowed at a time per logical unit
2489 * and that persistent reservation has a scope of LU_SCOPE.
2491 if (scope != PR_SCOPE_LU_SCOPE) {
2492 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2493 core_scsi3_put_pr_reg(pr_reg);
2494 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2498 * See if we have an existing PR reservation holder pointer at
2499 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2502 spin_lock(&dev->dev_reservation_lock);
2503 pr_res_holder = dev->dev_pr_res_holder;
2504 if ((pr_res_holder)) {
2506 * From spc4r17 Section 5.7.9: Reserving:
2508 * If the device server receives a PERSISTENT RESERVE OUT
2509 * command from an I_T nexus other than a persistent reservation
2510 * holder (see 5.7.10) that attempts to create a persistent
2511 * reservation when a persistent reservation already exists for
2512 * the logical unit, then the command shall be completed with
2513 * RESERVATION CONFLICT status.
2515 if (pr_res_holder != pr_reg) {
2516 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2517 pr_err("SPC-3 PR: Attempted RESERVE from"
2518 " [%s]: %s while reservation already held by"
2519 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2520 cmd->se_tfo->get_fabric_name(),
2521 se_sess->se_node_acl->initiatorname,
2522 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2523 pr_res_holder->pr_reg_nacl->initiatorname);
2525 spin_unlock(&dev->dev_reservation_lock);
2526 core_scsi3_put_pr_reg(pr_reg);
2527 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2531 * From spc4r17 Section 5.7.9: Reserving:
2533 * If a persistent reservation holder attempts to modify the
2534 * type or scope of an existing persistent reservation, the
2535 * command shall be completed with RESERVATION CONFLICT status.
2537 if ((pr_res_holder->pr_res_type != type) ||
2538 (pr_res_holder->pr_res_scope != scope)) {
2539 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2540 pr_err("SPC-3 PR: Attempted RESERVE from"
2541 " [%s]: %s trying to change TYPE and/or SCOPE,"
2542 " while reservation already held by [%s]: %s,"
2543 " returning RESERVATION_CONFLICT\n",
2544 cmd->se_tfo->get_fabric_name(),
2545 se_sess->se_node_acl->initiatorname,
2546 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2547 pr_res_holder->pr_reg_nacl->initiatorname);
2549 spin_unlock(&dev->dev_reservation_lock);
2550 core_scsi3_put_pr_reg(pr_reg);
2551 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2555 * From spc4r17 Section 5.7.9: Reserving:
2557 * If the device server receives a PERSISTENT RESERVE OUT
2558 * command with RESERVE service action where the TYPE field and
2559 * the SCOPE field contain the same values as the existing type
2560 * and scope from a persistent reservation holder, it shall not
2561 * make any change to the existing persistent reservation and
2562 * shall completethe command with GOOD status.
2564 spin_unlock(&dev->dev_reservation_lock);
2565 core_scsi3_put_pr_reg(pr_reg);
2569 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2570 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2572 pr_reg->pr_res_scope = scope;
2573 pr_reg->pr_res_type = type;
2574 pr_reg->pr_res_holder = 1;
2575 dev->dev_pr_res_holder = pr_reg;
2576 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2577 PR_REG_ISID_ID_LEN);
2579 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2580 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2581 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2582 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2583 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2584 cmd->se_tfo->get_fabric_name(),
2585 se_sess->se_node_acl->initiatorname,
2586 (prf_isid) ? &i_buf[0] : "");
2587 spin_unlock(&dev->dev_reservation_lock);
2589 if (pr_tmpl->pr_aptpl_active) {
2590 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2591 &pr_reg->pr_aptpl_buf[0],
2592 pr_tmpl->pr_aptpl_buf_len);
2594 pr_debug("SPC-3 PR: Updated APTPL metadata"
2598 core_scsi3_put_pr_reg(pr_reg);
2602 static int core_scsi3_emulate_pro_reserve(
2608 struct se_device *dev = cmd->se_dev;
2612 case PR_TYPE_WRITE_EXCLUSIVE:
2613 case PR_TYPE_EXCLUSIVE_ACCESS:
2614 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2615 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2616 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2617 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2618 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2621 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2623 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2631 * Called with struct se_device->dev_reservation_lock held.
2633 static void __core_scsi3_complete_pro_release(
2634 struct se_device *dev,
2635 struct se_node_acl *se_nacl,
2636 struct t10_pr_registration *pr_reg,
2639 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2640 char i_buf[PR_REG_ISID_ID_LEN];
2643 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2644 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2645 PR_REG_ISID_ID_LEN);
2647 * Go ahead and release the current PR reservation holder.
2649 dev->dev_pr_res_holder = NULL;
2651 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2652 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2653 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2654 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2655 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2656 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2657 tfo->get_fabric_name(), se_nacl->initiatorname,
2658 (prf_isid) ? &i_buf[0] : "");
2660 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2662 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2665 static int core_scsi3_emulate_pro_release(
2671 struct se_device *dev = cmd->se_dev;
2672 struct se_session *se_sess = cmd->se_sess;
2673 struct se_lun *se_lun = cmd->se_lun;
2674 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2675 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2676 int ret, all_reg = 0;
2678 if (!se_sess || !se_lun) {
2679 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2680 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2684 * Locate the existing *pr_reg via struct se_node_acl pointers
2686 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2688 pr_err("SPC-3 PR: Unable to locate"
2689 " PR_REGISTERED *pr_reg for RELEASE\n");
2690 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2694 * From spc4r17 Section 5.7.11.2 Releasing:
2696 * If there is no persistent reservation or in response to a persistent
2697 * reservation release request from a registered I_T nexus that is not a
2698 * persistent reservation holder (see 5.7.10), the device server shall
2701 * a) Not release the persistent reservation, if any;
2702 * b) Not remove any registrations; and
2703 * c) Complete the command with GOOD status.
2705 spin_lock(&dev->dev_reservation_lock);
2706 pr_res_holder = dev->dev_pr_res_holder;
2707 if (!pr_res_holder) {
2709 * No persistent reservation, return GOOD status.
2711 spin_unlock(&dev->dev_reservation_lock);
2712 core_scsi3_put_pr_reg(pr_reg);
2715 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2716 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2719 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2721 * Non 'All Registrants' PR Type cases..
2722 * Release request from a registered I_T nexus that is not a
2723 * persistent reservation holder. return GOOD status.
2725 spin_unlock(&dev->dev_reservation_lock);
2726 core_scsi3_put_pr_reg(pr_reg);
2730 * From spc4r17 Section 5.7.11.2 Releasing:
2732 * Only the persistent reservation holder (see 5.7.10) is allowed to
2733 * release a persistent reservation.
2735 * An application client releases the persistent reservation by issuing
2736 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2737 * an I_T nexus that is a persistent reservation holder with the
2738 * following parameters:
2740 * a) RESERVATION KEY field set to the value of the reservation key
2741 * that is registered with the logical unit for the I_T nexus;
2743 if (res_key != pr_reg->pr_res_key) {
2744 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2745 " does not match existing SA REGISTER res_key:"
2746 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2747 spin_unlock(&dev->dev_reservation_lock);
2748 core_scsi3_put_pr_reg(pr_reg);
2749 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2753 * From spc4r17 Section 5.7.11.2 Releasing and above:
2755 * b) TYPE field and SCOPE field set to match the persistent
2756 * reservation being released.
2758 if ((pr_res_holder->pr_res_type != type) ||
2759 (pr_res_holder->pr_res_scope != scope)) {
2760 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2761 pr_err("SPC-3 PR RELEASE: Attempted to release"
2762 " reservation from [%s]: %s with different TYPE "
2763 "and/or SCOPE while reservation already held by"
2764 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2765 cmd->se_tfo->get_fabric_name(),
2766 se_sess->se_node_acl->initiatorname,
2767 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2768 pr_res_holder->pr_reg_nacl->initiatorname);
2770 spin_unlock(&dev->dev_reservation_lock);
2771 core_scsi3_put_pr_reg(pr_reg);
2772 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2776 * In response to a persistent reservation release request from the
2777 * persistent reservation holder the device server shall perform a
2778 * release by doing the following as an uninterrupted series of actions:
2779 * a) Release the persistent reservation;
2780 * b) Not remove any registration(s);
2781 * c) If the released persistent reservation is a registrants only type
2782 * or all registrants type persistent reservation,
2783 * the device server shall establish a unit attention condition for
2784 * the initiator port associated with every regis-
2785 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2786 * RESERVE OUT command with RELEASE service action was received,
2787 * with the additional sense code set to RESERVATIONS RELEASED; and
2788 * d) If the persistent reservation is of any other type, the device
2789 * server shall not establish a unit attention condition.
2791 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2794 spin_unlock(&dev->dev_reservation_lock);
2796 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2797 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2798 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2799 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2801 * If no UNIT ATTENTION conditions will be established for
2802 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2803 * go ahead and check for APTPL=1 update+write below
2808 spin_lock(&pr_tmpl->registration_lock);
2809 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2812 * Do not establish a UNIT ATTENTION condition
2813 * for the calling I_T Nexus
2815 if (pr_reg_p == pr_reg)
2818 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2819 pr_reg_p->pr_res_mapped_lun,
2820 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2822 spin_unlock(&pr_tmpl->registration_lock);
2825 if (pr_tmpl->pr_aptpl_active) {
2826 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2827 &pr_reg->pr_aptpl_buf[0],
2828 pr_tmpl->pr_aptpl_buf_len);
2830 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2833 core_scsi3_put_pr_reg(pr_reg);
2837 static int core_scsi3_emulate_pro_clear(
2841 struct se_device *dev = cmd->se_dev;
2842 struct se_node_acl *pr_reg_nacl;
2843 struct se_session *se_sess = cmd->se_sess;
2844 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2845 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2846 u32 pr_res_mapped_lun = 0;
2847 int calling_it_nexus = 0;
2849 * Locate the existing *pr_reg via struct se_node_acl pointers
2851 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2852 se_sess->se_node_acl, se_sess);
2854 pr_err("SPC-3 PR: Unable to locate"
2855 " PR_REGISTERED *pr_reg for CLEAR\n");
2856 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2860 * From spc4r17 section 5.7.11.6, Clearing:
2862 * Any application client may release the persistent reservation and
2863 * remove all registrations from a device server by issuing a
2864 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2865 * registered I_T nexus with the following parameter:
2867 * a) RESERVATION KEY field set to the value of the reservation key
2868 * that is registered with the logical unit for the I_T nexus.
2870 if (res_key != pr_reg_n->pr_res_key) {
2871 pr_err("SPC-3 PR REGISTER: Received"
2872 " res_key: 0x%016Lx does not match"
2873 " existing SA REGISTER res_key:"
2874 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2875 core_scsi3_put_pr_reg(pr_reg_n);
2876 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2880 * a) Release the persistent reservation, if any;
2882 spin_lock(&dev->dev_reservation_lock);
2883 pr_res_holder = dev->dev_pr_res_holder;
2884 if (pr_res_holder) {
2885 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2886 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2889 spin_unlock(&dev->dev_reservation_lock);
2891 * b) Remove all registration(s) (see spc4r17 5.7.7);
2893 spin_lock(&pr_tmpl->registration_lock);
2894 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2895 &pr_tmpl->registration_list, pr_reg_list) {
2897 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2898 pr_reg_nacl = pr_reg->pr_reg_nacl;
2899 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2900 __core_scsi3_free_registration(dev, pr_reg, NULL,
2903 * e) Establish a unit attention condition for the initiator
2904 * port associated with every registered I_T nexus other
2905 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2906 * command with CLEAR service action was received, with the
2907 * additional sense code set to RESERVATIONS PREEMPTED.
2909 if (!calling_it_nexus)
2910 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2911 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2913 spin_unlock(&pr_tmpl->registration_lock);
2915 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2916 cmd->se_tfo->get_fabric_name());
2918 if (pr_tmpl->pr_aptpl_active) {
2919 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2920 pr_debug("SPC-3 PR: Updated APTPL metadata"
2924 core_scsi3_pr_generation(dev);
2929 * Called with struct se_device->dev_reservation_lock held.
2931 static void __core_scsi3_complete_pro_preempt(
2932 struct se_device *dev,
2933 struct t10_pr_registration *pr_reg,
2934 struct list_head *preempt_and_abort_list,
2939 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2940 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2941 char i_buf[PR_REG_ISID_ID_LEN];
2944 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2945 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2946 PR_REG_ISID_ID_LEN);
2948 * Do an implict RELEASE of the existing reservation.
2950 if (dev->dev_pr_res_holder)
2951 __core_scsi3_complete_pro_release(dev, nacl,
2952 dev->dev_pr_res_holder, 0);
2954 dev->dev_pr_res_holder = pr_reg;
2955 pr_reg->pr_res_holder = 1;
2956 pr_reg->pr_res_type = type;
2957 pr_reg->pr_res_scope = scope;
2959 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2960 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2961 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2962 core_scsi3_pr_dump_type(type),
2963 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2964 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2965 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2966 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2968 * For PREEMPT_AND_ABORT, add the preempting reservation's
2969 * struct t10_pr_registration to the list that will be compared
2970 * against received CDBs..
2972 if (preempt_and_abort_list)
2973 list_add_tail(&pr_reg->pr_reg_abort_list,
2974 preempt_and_abort_list);
2977 static void core_scsi3_release_preempt_and_abort(
2978 struct list_head *preempt_and_abort_list,
2979 struct t10_pr_registration *pr_reg_holder)
2981 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2983 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2984 pr_reg_abort_list) {
2986 list_del(&pr_reg->pr_reg_abort_list);
2987 if (pr_reg_holder == pr_reg)
2989 if (pr_reg->pr_res_holder) {
2990 pr_warn("pr_reg->pr_res_holder still set\n");
2994 pr_reg->pr_reg_deve = NULL;
2995 pr_reg->pr_reg_nacl = NULL;
2996 kfree(pr_reg->pr_aptpl_buf);
2997 kmem_cache_free(t10_pr_reg_cache, pr_reg);
3001 int core_scsi3_check_cdb_abort_and_preempt(
3002 struct list_head *preempt_and_abort_list,
3005 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3007 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
3008 pr_reg_abort_list) {
3009 if (pr_reg->pr_res_key == cmd->pr_res_key)
3016 static int core_scsi3_pro_preempt(
3024 struct se_device *dev = cmd->se_dev;
3025 struct se_dev_entry *se_deve;
3026 struct se_node_acl *pr_reg_nacl;
3027 struct se_session *se_sess = cmd->se_sess;
3028 struct list_head preempt_and_abort_list;
3029 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
3030 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3031 u32 pr_res_mapped_lun = 0;
3032 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3033 int prh_type = 0, prh_scope = 0, ret;
3036 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3040 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3041 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3044 pr_err("SPC-3 PR: Unable to locate"
3045 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3046 (abort) ? "_AND_ABORT" : "");
3047 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3050 if (pr_reg_n->pr_res_key != res_key) {
3051 core_scsi3_put_pr_reg(pr_reg_n);
3052 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3055 if (scope != PR_SCOPE_LU_SCOPE) {
3056 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3057 core_scsi3_put_pr_reg(pr_reg_n);
3058 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3061 INIT_LIST_HEAD(&preempt_and_abort_list);
3063 spin_lock(&dev->dev_reservation_lock);
3064 pr_res_holder = dev->dev_pr_res_holder;
3065 if (pr_res_holder &&
3066 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3067 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3070 if (!all_reg && !sa_res_key) {
3071 spin_unlock(&dev->dev_reservation_lock);
3072 core_scsi3_put_pr_reg(pr_reg_n);
3073 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3077 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3079 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3080 * persistent reservation holder or there is no persistent reservation
3081 * holder (i.e., there is no persistent reservation), then the device
3082 * server shall perform a preempt by doing the following in an
3083 * uninterrupted series of actions. (See below..)
3085 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3087 * No existing or SA Reservation Key matching reservations..
3089 * PROUT SA PREEMPT with All Registrant type reservations are
3090 * allowed to be processed without a matching SA Reservation Key
3092 spin_lock(&pr_tmpl->registration_lock);
3093 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3094 &pr_tmpl->registration_list, pr_reg_list) {
3096 * Removing of registrations in non all registrants
3097 * type reservations without a matching SA reservation
3100 * a) Remove the registrations for all I_T nexuses
3101 * specified by the SERVICE ACTION RESERVATION KEY
3103 * b) Ignore the contents of the SCOPE and TYPE fields;
3104 * c) Process tasks as defined in 5.7.1; and
3105 * d) Establish a unit attention condition for the
3106 * initiator port associated with every I_T nexus
3107 * that lost its registration other than the I_T
3108 * nexus on which the PERSISTENT RESERVE OUT command
3109 * was received, with the additional sense code set
3110 * to REGISTRATIONS PREEMPTED.
3113 if (pr_reg->pr_res_key != sa_res_key)
3116 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3117 pr_reg_nacl = pr_reg->pr_reg_nacl;
3118 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3119 __core_scsi3_free_registration(dev, pr_reg,
3120 (abort) ? &preempt_and_abort_list :
3121 NULL, calling_it_nexus);
3125 * Case for any existing all registrants type
3126 * reservation, follow logic in spc4r17 section
3127 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3129 * For a ZERO SA Reservation key, release
3130 * all other registrations and do an implict
3131 * release of active persistent reservation.
3133 * For a non-ZERO SA Reservation key, only
3134 * release the matching reservation key from
3138 (pr_reg->pr_res_key != sa_res_key))
3141 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3142 if (calling_it_nexus)
3145 pr_reg_nacl = pr_reg->pr_reg_nacl;
3146 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3147 __core_scsi3_free_registration(dev, pr_reg,
3148 (abort) ? &preempt_and_abort_list :
3152 if (!calling_it_nexus)
3153 core_scsi3_ua_allocate(pr_reg_nacl,
3154 pr_res_mapped_lun, 0x2A,
3155 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3157 spin_unlock(&pr_tmpl->registration_lock);
3159 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3160 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3161 * RESERVATION KEY field to a value that does not match any
3162 * registered reservation key, then the device server shall
3163 * complete the command with RESERVATION CONFLICT status.
3165 if (!released_regs) {
3166 spin_unlock(&dev->dev_reservation_lock);
3167 core_scsi3_put_pr_reg(pr_reg_n);
3168 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3172 * For an existing all registrants type reservation
3173 * with a zero SA rservation key, preempt the existing
3174 * reservation with the new PR type and scope.
3176 if (pr_res_holder && all_reg && !(sa_res_key)) {
3177 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3178 (abort) ? &preempt_and_abort_list : NULL,
3179 type, scope, abort);
3182 core_scsi3_release_preempt_and_abort(
3183 &preempt_and_abort_list, pr_reg_n);
3185 spin_unlock(&dev->dev_reservation_lock);
3187 if (pr_tmpl->pr_aptpl_active) {
3188 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3189 &pr_reg_n->pr_aptpl_buf[0],
3190 pr_tmpl->pr_aptpl_buf_len);
3192 pr_debug("SPC-3 PR: Updated APTPL"
3193 " metadata for PREEMPT%s\n", (abort) ?
3197 core_scsi3_put_pr_reg(pr_reg_n);
3198 core_scsi3_pr_generation(cmd->se_dev);
3202 * The PREEMPTing SA reservation key matches that of the
3203 * existing persistent reservation, first, we check if
3204 * we are preempting our own reservation.
3205 * From spc4r17, section 5.7.11.4.3 Preempting
3206 * persistent reservations and registration handling
3208 * If an all registrants persistent reservation is not
3209 * present, it is not an error for the persistent
3210 * reservation holder to preempt itself (i.e., a
3211 * PERSISTENT RESERVE OUT with a PREEMPT service action
3212 * or a PREEMPT AND ABORT service action with the
3213 * SERVICE ACTION RESERVATION KEY value equal to the
3214 * persistent reservation holder's reservation key that
3215 * is received from the persistent reservation holder).
3216 * In that case, the device server shall establish the
3217 * new persistent reservation and maintain the
3220 prh_type = pr_res_holder->pr_res_type;
3221 prh_scope = pr_res_holder->pr_res_scope;
3223 * If the SERVICE ACTION RESERVATION KEY field identifies a
3224 * persistent reservation holder (see 5.7.10), the device
3225 * server shall perform a preempt by doing the following as
3226 * an uninterrupted series of actions:
3228 * a) Release the persistent reservation for the holder
3229 * identified by the SERVICE ACTION RESERVATION KEY field;
3231 if (pr_reg_n != pr_res_holder)
3232 __core_scsi3_complete_pro_release(dev,
3233 pr_res_holder->pr_reg_nacl,
3234 dev->dev_pr_res_holder, 0);
3236 * b) Remove the registrations for all I_T nexuses identified
3237 * by the SERVICE ACTION RESERVATION KEY field, except the
3238 * I_T nexus that is being used for the PERSISTENT RESERVE
3239 * OUT command. If an all registrants persistent reservation
3240 * is present and the SERVICE ACTION RESERVATION KEY field
3241 * is set to zero, then all registrations shall be removed
3242 * except for that of the I_T nexus that is being used for
3243 * the PERSISTENT RESERVE OUT command;
3245 spin_lock(&pr_tmpl->registration_lock);
3246 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3247 &pr_tmpl->registration_list, pr_reg_list) {
3249 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3250 if (calling_it_nexus)
3253 if (pr_reg->pr_res_key != sa_res_key)
3256 pr_reg_nacl = pr_reg->pr_reg_nacl;
3257 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3258 __core_scsi3_free_registration(dev, pr_reg,
3259 (abort) ? &preempt_and_abort_list : NULL,
3262 * e) Establish a unit attention condition for the initiator
3263 * port associated with every I_T nexus that lost its
3264 * persistent reservation and/or registration, with the
3265 * additional sense code set to REGISTRATIONS PREEMPTED;
3267 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3268 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3270 spin_unlock(&pr_tmpl->registration_lock);
3272 * c) Establish a persistent reservation for the preempting
3273 * I_T nexus using the contents of the SCOPE and TYPE fields;
3275 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3276 (abort) ? &preempt_and_abort_list : NULL,
3277 type, scope, abort);
3279 * d) Process tasks as defined in 5.7.1;
3281 * f) If the type or scope has changed, then for every I_T nexus
3282 * whose reservation key was not removed, except for the I_T
3283 * nexus on which the PERSISTENT RESERVE OUT command was
3284 * received, the device server shall establish a unit
3285 * attention condition for the initiator port associated with
3286 * that I_T nexus, with the additional sense code set to
3287 * RESERVATIONS RELEASED. If the type or scope have not
3288 * changed, then no unit attention condition(s) shall be
3289 * established for this reason.
3291 if ((prh_type != type) || (prh_scope != scope)) {
3292 spin_lock(&pr_tmpl->registration_lock);
3293 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3294 &pr_tmpl->registration_list, pr_reg_list) {
3296 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3297 if (calling_it_nexus)
3300 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3301 pr_reg->pr_res_mapped_lun, 0x2A,
3302 ASCQ_2AH_RESERVATIONS_RELEASED);
3304 spin_unlock(&pr_tmpl->registration_lock);
3306 spin_unlock(&dev->dev_reservation_lock);
3308 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3309 * All received CDBs for the matching existing reservation and
3310 * registrations undergo ABORT_TASK logic.
3312 * From there, core_scsi3_release_preempt_and_abort() will
3313 * release every registration in the list (which have already
3314 * been removed from the primary pr_reg list), except the
3315 * new persistent reservation holder, the calling Initiator Port.
3318 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3319 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3323 if (pr_tmpl->pr_aptpl_active) {
3324 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3325 &pr_reg_n->pr_aptpl_buf[0],
3326 pr_tmpl->pr_aptpl_buf_len);
3328 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3329 "%s\n", (abort) ? "_AND_ABORT" : "");
3332 core_scsi3_put_pr_reg(pr_reg_n);
3333 core_scsi3_pr_generation(cmd->se_dev);
3337 static int core_scsi3_emulate_pro_preempt(
3348 case PR_TYPE_WRITE_EXCLUSIVE:
3349 case PR_TYPE_EXCLUSIVE_ACCESS:
3350 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3351 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3352 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3353 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3354 ret = core_scsi3_pro_preempt(cmd, type, scope,
3355 res_key, sa_res_key, abort);
3358 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3359 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3360 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3368 static int core_scsi3_emulate_pro_register_and_move(
3375 struct se_session *se_sess = cmd->se_sess;
3376 struct se_device *dev = cmd->se_dev;
3377 struct se_dev_entry *se_deve, *dest_se_deve = NULL;
3378 struct se_lun *se_lun = cmd->se_lun;
3379 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3380 struct se_port *se_port;
3381 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3382 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3383 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3384 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3386 unsigned char *initiator_str;
3387 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3388 u32 tid_len, tmp_tid_len;
3389 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3390 unsigned short rtpi;
3391 unsigned char proto_ident;
3393 if (!se_sess || !se_lun) {
3394 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3395 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3398 memset(dest_iport, 0, 64);
3399 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3400 se_tpg = se_sess->se_tpg;
3401 tf_ops = se_tpg->se_tpg_tfo;
3402 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3404 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3405 * Register behaviors for a REGISTER AND MOVE service action
3407 * Locate the existing *pr_reg via struct se_node_acl pointers
3409 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3412 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3413 " *pr_reg for REGISTER_AND_MOVE\n");
3414 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3418 * The provided reservation key much match the existing reservation key
3419 * provided during this initiator's I_T nexus registration.
3421 if (res_key != pr_reg->pr_res_key) {
3422 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3423 " res_key: 0x%016Lx does not match existing SA REGISTER"
3424 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3425 core_scsi3_put_pr_reg(pr_reg);
3426 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3430 * The service active reservation key needs to be non zero
3433 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3435 core_scsi3_put_pr_reg(pr_reg);
3436 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3441 * Determine the Relative Target Port Identifier where the reservation
3442 * will be moved to for the TransportID containing SCSI initiator WWN
3445 buf = transport_kmap_data_sg(cmd);
3446 rtpi = (buf[18] & 0xff) << 8;
3447 rtpi |= buf[19] & 0xff;
3448 tid_len = (buf[20] & 0xff) << 24;
3449 tid_len |= (buf[21] & 0xff) << 16;
3450 tid_len |= (buf[22] & 0xff) << 8;
3451 tid_len |= buf[23] & 0xff;
3452 transport_kunmap_data_sg(cmd);
3455 if ((tid_len + 24) != cmd->data_length) {
3456 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3457 " does not equal CDB data_length: %u\n", tid_len,
3459 core_scsi3_put_pr_reg(pr_reg);
3460 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3464 spin_lock(&dev->se_port_lock);
3465 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3466 if (se_port->sep_rtpi != rtpi)
3468 dest_se_tpg = se_port->sep_tpg;
3471 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3475 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3476 smp_mb__after_atomic_inc();
3477 spin_unlock(&dev->se_port_lock);
3479 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3481 pr_err("core_scsi3_tpg_depend_item() failed"
3482 " for dest_se_tpg\n");
3483 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3484 smp_mb__after_atomic_dec();
3485 core_scsi3_put_pr_reg(pr_reg);
3486 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3490 spin_lock(&dev->se_port_lock);
3493 spin_unlock(&dev->se_port_lock);
3495 if (!dest_se_tpg || !dest_tf_ops) {
3496 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3497 " fabric ops from Relative Target Port Identifier:"
3499 core_scsi3_put_pr_reg(pr_reg);
3500 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3504 buf = transport_kmap_data_sg(cmd);
3505 proto_ident = (buf[24] & 0x0f);
3507 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3508 " 0x%02x\n", proto_ident);
3510 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3511 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3512 " proto_ident: 0x%02x does not match ident: 0x%02x"
3513 " from fabric: %s\n", proto_ident,
3514 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3515 dest_tf_ops->get_fabric_name());
3516 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3520 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3521 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3522 " containg a valid tpg_parse_pr_out_transport_id"
3523 " function pointer\n");
3524 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3528 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3529 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3530 if (!initiator_str) {
3531 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3532 " initiator_str from Transport ID\n");
3533 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3538 transport_kunmap_data_sg(cmd);
3541 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3542 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3543 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3546 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3547 * action specifies a TransportID that is the same as the initiator port
3548 * of the I_T nexus for the command received, then the command shall
3549 * be terminated with CHECK CONDITION status, with the sense key set to
3550 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3551 * IN PARAMETER LIST.
3553 pr_reg_nacl = pr_reg->pr_reg_nacl;
3554 matching_iname = (!strcmp(initiator_str,
3555 pr_reg_nacl->initiatorname)) ? 1 : 0;
3556 if (!matching_iname)
3557 goto after_iport_check;
3559 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3560 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3561 " matches: %s on received I_T Nexus\n", initiator_str,
3562 pr_reg_nacl->initiatorname);
3563 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3567 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3568 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3569 " matches: %s %s on received I_T Nexus\n",
3570 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3571 pr_reg->pr_reg_isid);
3572 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3578 * Locate the destination struct se_node_acl from the received Transport ID
3580 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3581 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3583 if (dest_node_acl) {
3584 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3585 smp_mb__after_atomic_inc();
3587 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3589 if (!dest_node_acl) {
3590 pr_err("Unable to locate %s dest_node_acl for"
3591 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3593 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3597 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3599 pr_err("core_scsi3_nodeacl_depend_item() for"
3600 " dest_node_acl\n");
3601 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3602 smp_mb__after_atomic_dec();
3603 dest_node_acl = NULL;
3604 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3609 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3610 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3611 dest_node_acl->initiatorname);
3614 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3617 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3618 if (!dest_se_deve) {
3619 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3620 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3621 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3626 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3628 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3629 atomic_dec(&dest_se_deve->pr_ref_count);
3630 smp_mb__after_atomic_dec();
3631 dest_se_deve = NULL;
3632 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3637 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3638 " ACL for dest_se_deve->mapped_lun: %u\n",
3639 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3640 dest_se_deve->mapped_lun);
3643 * A persistent reservation needs to already existing in order to
3644 * successfully complete the REGISTER_AND_MOVE service action..
3646 spin_lock(&dev->dev_reservation_lock);
3647 pr_res_holder = dev->dev_pr_res_holder;
3648 if (!pr_res_holder) {
3649 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3650 " currently held\n");
3651 spin_unlock(&dev->dev_reservation_lock);
3652 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3657 * The received on I_T Nexus must be the reservation holder.
3659 * From spc4r17 section 5.7.8 Table 50 --
3660 * Register behaviors for a REGISTER AND MOVE service action
3662 if (pr_res_holder != pr_reg) {
3663 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3664 " Nexus is not reservation holder\n");
3665 spin_unlock(&dev->dev_reservation_lock);
3666 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3671 * From spc4r17 section 5.7.8: registering and moving reservation
3673 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3674 * action is received and the established persistent reservation is a
3675 * Write Exclusive - All Registrants type or Exclusive Access -
3676 * All Registrants type reservation, then the command shall be completed
3677 * with RESERVATION CONFLICT status.
3679 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3680 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3681 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3682 " reservation for type: %s\n",
3683 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3684 spin_unlock(&dev->dev_reservation_lock);
3685 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3689 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3691 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3693 type = pr_res_holder->pr_res_type;
3694 scope = pr_res_holder->pr_res_type;
3696 * c) Associate the reservation key specified in the SERVICE ACTION
3697 * RESERVATION KEY field with the I_T nexus specified as the
3698 * destination of the register and move, where:
3699 * A) The I_T nexus is specified by the TransportID and the
3700 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3701 * B) Regardless of the TransportID format used, the association for
3702 * the initiator port is based on either the initiator port name
3703 * (see 3.1.71) on SCSI transport protocols where port names are
3704 * required or the initiator port identifier (see 3.1.70) on SCSI
3705 * transport protocols where port names are not required;
3706 * d) Register the reservation key specified in the SERVICE ACTION
3707 * RESERVATION KEY field;
3708 * e) Retain the reservation key specified in the SERVICE ACTION
3709 * RESERVATION KEY field and associated information;
3711 * Also, It is not an error for a REGISTER AND MOVE service action to
3712 * register an I_T nexus that is already registered with the same
3713 * reservation key or a different reservation key.
3715 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3718 ret = core_scsi3_alloc_registration(cmd->se_dev,
3719 dest_node_acl, dest_se_deve, iport_ptr,
3720 sa_res_key, 0, aptpl, 2, 1);
3722 spin_unlock(&dev->dev_reservation_lock);
3723 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3727 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3732 * f) Release the persistent reservation for the persistent reservation
3733 * holder (i.e., the I_T nexus on which the
3735 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3736 dev->dev_pr_res_holder, 0);
3738 * g) Move the persistent reservation to the specified I_T nexus using
3739 * the same scope and type as the persistent reservation released in
3742 dev->dev_pr_res_holder = dest_pr_reg;
3743 dest_pr_reg->pr_res_holder = 1;
3744 dest_pr_reg->pr_res_type = type;
3745 pr_reg->pr_res_scope = scope;
3746 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3747 PR_REG_ISID_ID_LEN);
3749 * Increment PRGeneration for existing registrations..
3752 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3753 spin_unlock(&dev->dev_reservation_lock);
3755 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3756 " created new reservation holder TYPE: %s on object RTPI:"
3757 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3758 core_scsi3_pr_dump_type(type), rtpi,
3759 dest_pr_reg->pr_res_generation);
3760 pr_debug("SPC-3 PR Successfully moved reservation from"
3761 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3762 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3763 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3764 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3767 * It is now safe to release configfs group dependencies for destination
3768 * of Transport ID Initiator Device/Port Identifier
3770 core_scsi3_lunacl_undepend_item(dest_se_deve);
3771 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3772 core_scsi3_tpg_undepend_item(dest_se_tpg);
3774 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3775 * nexus on which PERSISTENT RESERVE OUT command was received.
3778 spin_lock(&pr_tmpl->registration_lock);
3779 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3780 spin_unlock(&pr_tmpl->registration_lock);
3782 core_scsi3_put_pr_reg(pr_reg);
3785 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3786 * write out the updated metadata to struct file for this SCSI device.
3789 pr_tmpl->pr_aptpl_active = 0;
3790 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3791 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3792 " REGISTER_AND_MOVE\n");
3794 pr_tmpl->pr_aptpl_active = 1;
3795 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3796 &dest_pr_reg->pr_aptpl_buf[0],
3797 pr_tmpl->pr_aptpl_buf_len);
3799 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3800 " REGISTER_AND_MOVE\n");
3803 transport_kunmap_data_sg(cmd);
3805 core_scsi3_put_pr_reg(dest_pr_reg);
3809 transport_kunmap_data_sg(cmd);
3811 core_scsi3_lunacl_undepend_item(dest_se_deve);
3813 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3814 core_scsi3_tpg_undepend_item(dest_se_tpg);
3815 core_scsi3_put_pr_reg(pr_reg);
3819 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3821 unsigned int __v1, __v2;
3823 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3824 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3826 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3830 * See spc4r17 section 6.14 Table 170
3832 int target_scsi3_emulate_pr_out(struct se_task *task)
3834 struct se_cmd *cmd = task->task_se_cmd;
3835 unsigned char *cdb = &cmd->t_task_cdb[0];
3837 u64 res_key, sa_res_key;
3838 int sa, scope, type, aptpl;
3839 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3843 * Following spc2r20 5.5.1 Reservations overview:
3845 * If a logical unit has been reserved by any RESERVE command and is
3846 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3847 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3848 * initiator or service action and shall terminate with a RESERVATION
3851 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3852 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3853 " SPC-2 reservation is held, returning"
3854 " RESERVATION_CONFLICT\n");
3855 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3861 * FIXME: A NULL struct se_session pointer means an this is not coming from
3862 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3864 if (!cmd->se_sess) {
3865 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3870 if (cmd->data_length < 24) {
3871 pr_warn("SPC-PR: Received PR OUT parameter list"
3872 " length too small: %u\n", cmd->data_length);
3873 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3878 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3880 sa = (cdb[1] & 0x1f);
3881 scope = (cdb[2] & 0xf0);
3882 type = (cdb[2] & 0x0f);
3884 buf = transport_kmap_data_sg(cmd);
3886 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3888 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3889 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3891 * REGISTER_AND_MOVE uses a different SA parameter list containing
3892 * SCSI TransportIDs.
3894 if (sa != PRO_REGISTER_AND_MOVE) {
3895 spec_i_pt = (buf[20] & 0x08);
3896 all_tg_pt = (buf[20] & 0x04);
3897 aptpl = (buf[20] & 0x01);
3899 aptpl = (buf[17] & 0x01);
3900 unreg = (buf[17] & 0x02);
3902 transport_kunmap_data_sg(cmd);
3906 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3908 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3909 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3915 * From spc4r17 section 6.14:
3917 * If the SPEC_I_PT bit is set to zero, the service action is not
3918 * REGISTER AND MOVE, and the parameter list length is not 24, then
3919 * the command shall be terminated with CHECK CONDITION status, with
3920 * the sense key set to ILLEGAL REQUEST, and the additional sense
3921 * code set to PARAMETER LIST LENGTH ERROR.
3923 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3924 (cmd->data_length != 24)) {
3925 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3926 " list length: %u\n", cmd->data_length);
3927 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3932 * (core_scsi3_emulate_pro_* function parameters
3933 * are defined by spc4r17 Table 174:
3934 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3938 ret = core_scsi3_emulate_pro_register(cmd,
3939 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3942 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3945 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3948 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3951 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3952 res_key, sa_res_key, 0);
3954 case PRO_PREEMPT_AND_ABORT:
3955 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3956 res_key, sa_res_key, 1);
3958 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3959 ret = core_scsi3_emulate_pro_register(cmd,
3960 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3962 case PRO_REGISTER_AND_MOVE:
3963 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3964 sa_res_key, aptpl, unreg);
3967 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3968 " action: 0x%02x\n", cdb[1] & 0x1f);
3969 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3976 task->task_scsi_status = GOOD;
3977 transport_complete_task(task, 1);
3983 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3985 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3987 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3989 struct se_device *se_dev = cmd->se_dev;
3990 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
3991 struct t10_pr_registration *pr_reg;
3993 u32 add_len = 0, off = 8;
3995 if (cmd->data_length < 8) {
3996 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3997 " too small\n", cmd->data_length);
3998 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4002 buf = transport_kmap_data_sg(cmd);
4003 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4004 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4005 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4006 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4008 spin_lock(&su_dev->t10_pr.registration_lock);
4009 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
4012 * Check for overflow of 8byte PRI READ_KEYS payload and
4013 * next reservation key list descriptor.
4015 if ((add_len + 8) > (cmd->data_length - 8))
4018 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4019 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4020 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4021 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4022 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4023 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4024 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4025 buf[off++] = (pr_reg->pr_res_key & 0xff);
4029 spin_unlock(&su_dev->t10_pr.registration_lock);
4031 buf[4] = ((add_len >> 24) & 0xff);
4032 buf[5] = ((add_len >> 16) & 0xff);
4033 buf[6] = ((add_len >> 8) & 0xff);
4034 buf[7] = (add_len & 0xff);
4036 transport_kunmap_data_sg(cmd);
4042 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4044 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4046 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4048 struct se_device *se_dev = cmd->se_dev;
4049 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4050 struct t10_pr_registration *pr_reg;
4053 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4055 if (cmd->data_length < 8) {
4056 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4057 " too small\n", cmd->data_length);
4058 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4062 buf = transport_kmap_data_sg(cmd);
4063 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4064 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4065 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4066 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4068 spin_lock(&se_dev->dev_reservation_lock);
4069 pr_reg = se_dev->dev_pr_res_holder;
4072 * Set the hardcoded Additional Length
4074 buf[4] = ((add_len >> 24) & 0xff);
4075 buf[5] = ((add_len >> 16) & 0xff);
4076 buf[6] = ((add_len >> 8) & 0xff);
4077 buf[7] = (add_len & 0xff);
4079 if (cmd->data_length < 22)
4083 * Set the Reservation key.
4085 * From spc4r17, section 5.7.10:
4086 * A persistent reservation holder has its reservation key
4087 * returned in the parameter data from a PERSISTENT
4088 * RESERVE IN command with READ RESERVATION service action as
4090 * a) For a persistent reservation of the type Write Exclusive
4091 * - All Registrants or Exclusive Access  All Regitrants,
4092 * the reservation key shall be set to zero; or
4093 * b) For all other persistent reservation types, the
4094 * reservation key shall be set to the registered
4095 * reservation key for the I_T nexus that holds the
4096 * persistent reservation.
4098 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4099 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4102 pr_res_key = pr_reg->pr_res_key;
4104 buf[8] = ((pr_res_key >> 56) & 0xff);
4105 buf[9] = ((pr_res_key >> 48) & 0xff);
4106 buf[10] = ((pr_res_key >> 40) & 0xff);
4107 buf[11] = ((pr_res_key >> 32) & 0xff);
4108 buf[12] = ((pr_res_key >> 24) & 0xff);
4109 buf[13] = ((pr_res_key >> 16) & 0xff);
4110 buf[14] = ((pr_res_key >> 8) & 0xff);
4111 buf[15] = (pr_res_key & 0xff);
4113 * Set the SCOPE and TYPE
4115 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4116 (pr_reg->pr_res_type & 0x0f);
4120 spin_unlock(&se_dev->dev_reservation_lock);
4121 transport_kunmap_data_sg(cmd);
4127 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4129 * See spc4r17 section 6.13.4 Table 165
4131 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4133 struct se_device *dev = cmd->se_dev;
4134 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
4136 u16 add_len = 8; /* Hardcoded to 8. */
4138 if (cmd->data_length < 6) {
4139 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4140 " %u too small\n", cmd->data_length);
4141 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4145 buf = transport_kmap_data_sg(cmd);
4147 buf[0] = ((add_len << 8) & 0xff);
4148 buf[1] = (add_len & 0xff);
4149 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4150 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4151 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4152 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4154 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4155 * set the TMV: Task Mask Valid bit.
4159 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4161 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4163 * PTPL_A: Persistence across Target Power Loss Active bit
4165 if (pr_tmpl->pr_aptpl_active)
4168 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4170 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4171 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4172 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4173 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4174 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4175 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4177 transport_kunmap_data_sg(cmd);
4183 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4185 * See spc4r17 section 6.13.5 Table 168 and 169
4187 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4189 struct se_device *se_dev = cmd->se_dev;
4190 struct se_node_acl *se_nacl;
4191 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4192 struct se_portal_group *se_tpg;
4193 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4194 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
4196 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4197 u32 off = 8; /* off into first Full Status descriptor */
4198 int format_code = 0;
4200 if (cmd->data_length < 8) {
4201 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4202 " too small\n", cmd->data_length);
4203 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4207 buf = transport_kmap_data_sg(cmd);
4209 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4210 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4211 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4212 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4214 spin_lock(&pr_tmpl->registration_lock);
4215 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4216 &pr_tmpl->registration_list, pr_reg_list) {
4218 se_nacl = pr_reg->pr_reg_nacl;
4219 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4222 atomic_inc(&pr_reg->pr_res_holders);
4223 smp_mb__after_atomic_inc();
4224 spin_unlock(&pr_tmpl->registration_lock);
4226 * Determine expected length of $FABRIC_MOD specific
4227 * TransportID full status descriptor..
4229 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4230 se_tpg, se_nacl, pr_reg, &format_code);
4232 if ((exp_desc_len + add_len) > cmd->data_length) {
4233 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4234 " out of buffer: %d\n", cmd->data_length);
4235 spin_lock(&pr_tmpl->registration_lock);
4236 atomic_dec(&pr_reg->pr_res_holders);
4237 smp_mb__after_atomic_dec();
4241 * Set RESERVATION KEY
4243 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4244 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4245 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4246 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4247 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4248 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4249 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4250 buf[off++] = (pr_reg->pr_res_key & 0xff);
4251 off += 4; /* Skip Over Reserved area */
4254 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4256 if (pr_reg->pr_reg_all_tg_pt)
4259 * The struct se_lun pointer will be present for the
4260 * reservation holder for PR_HOLDER bit.
4262 * Also, if this registration is the reservation
4263 * holder, fill in SCOPE and TYPE in the next byte.
4265 if (pr_reg->pr_res_holder) {
4267 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4268 (pr_reg->pr_res_type & 0x0f);
4272 off += 4; /* Skip over reserved area */
4274 * From spc4r17 6.3.15:
4276 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4277 * IDENTIFIER field contains the relative port identifier (see
4278 * 3.1.120) of the target port that is part of the I_T nexus
4279 * described by this full status descriptor. If the ALL_TG_PT
4280 * bit is set to one, the contents of the RELATIVE TARGET PORT
4281 * IDENTIFIER field are not defined by this standard.
4283 if (!pr_reg->pr_reg_all_tg_pt) {
4284 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4286 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4287 buf[off++] = (port->sep_rtpi & 0xff);
4289 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4292 * Now, have the $FABRIC_MOD fill in the protocol identifier
4294 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4295 se_nacl, pr_reg, &format_code, &buf[off+4]);
4297 spin_lock(&pr_tmpl->registration_lock);
4298 atomic_dec(&pr_reg->pr_res_holders);
4299 smp_mb__after_atomic_dec();
4301 * Set the ADDITIONAL DESCRIPTOR LENGTH
4303 buf[off++] = ((desc_len >> 24) & 0xff);
4304 buf[off++] = ((desc_len >> 16) & 0xff);
4305 buf[off++] = ((desc_len >> 8) & 0xff);
4306 buf[off++] = (desc_len & 0xff);
4308 * Size of full desctipor header minus TransportID
4309 * containing $FABRIC_MOD specific) initiator device/port
4312 * See spc4r17 Section 6.13.5 Table 169
4314 add_desc_len = (24 + desc_len);
4317 add_len += add_desc_len;
4319 spin_unlock(&pr_tmpl->registration_lock);
4321 * Set ADDITIONAL_LENGTH
4323 buf[4] = ((add_len >> 24) & 0xff);
4324 buf[5] = ((add_len >> 16) & 0xff);
4325 buf[6] = ((add_len >> 8) & 0xff);
4326 buf[7] = (add_len & 0xff);
4328 transport_kunmap_data_sg(cmd);
4333 int target_scsi3_emulate_pr_in(struct se_task *task)
4335 struct se_cmd *cmd = task->task_se_cmd;
4339 * Following spc2r20 5.5.1 Reservations overview:
4341 * If a logical unit has been reserved by any RESERVE command and is
4342 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4343 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4344 * initiator or service action and shall terminate with a RESERVATION
4347 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4348 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4349 " SPC-2 reservation is held, returning"
4350 " RESERVATION_CONFLICT\n");
4351 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4355 switch (cmd->t_task_cdb[1] & 0x1f) {
4357 ret = core_scsi3_pri_read_keys(cmd);
4359 case PRI_READ_RESERVATION:
4360 ret = core_scsi3_pri_read_reservation(cmd);
4362 case PRI_REPORT_CAPABILITIES:
4363 ret = core_scsi3_pri_report_capabilities(cmd);
4365 case PRI_READ_FULL_STATUS:
4366 ret = core_scsi3_pri_read_full_status(cmd);
4369 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4370 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4371 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4377 task->task_scsi_status = GOOD;
4378 transport_complete_task(task, 1);
4383 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4388 static int core_pt_seq_non_holder(
4396 int core_setup_reservations(struct se_device *dev, int force_pt)
4398 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
4399 struct t10_reservation *rest = &su_dev->t10_pr;
4401 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4402 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4403 * cause a problem because libata and some SATA RAID HBAs appear
4404 * under Linux/SCSI, but to emulate reservations themselves.
4406 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4407 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
4408 rest->res_type = SPC_PASSTHROUGH;
4409 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4410 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4411 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4412 " emulation\n", dev->transport->name);
4416 * If SPC-3 or above is reported by real or emulated struct se_device,
4417 * use emulated Persistent Reservations.
4419 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4420 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4421 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4422 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4423 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4424 " emulation\n", dev->transport->name);
4426 rest->res_type = SPC2_RESERVATIONS;
4427 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4428 rest->pr_ops.t10_seq_non_holder =
4429 &core_scsi2_reservation_seq_non_holder;
4430 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4431 dev->transport->name);