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, int *ret)
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);
164 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
165 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
166 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
167 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
168 core_scsi3_put_pr_reg(pr_reg);
172 core_scsi3_put_pr_reg(pr_reg);
176 * Following spc2r20 5.5.1 Reservations overview:
178 * If a logical unit has executed a PERSISTENT RESERVE OUT
179 * command with the REGISTER or the REGISTER AND IGNORE
180 * EXISTING KEY service action and is still registered by any
181 * initiator, all RESERVE commands and all RELEASE commands
182 * regardless of initiator shall conflict and shall terminate
183 * with a RESERVATION CONFLICT status.
185 spin_lock(&pr_tmpl->registration_lock);
186 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
187 spin_unlock(&pr_tmpl->registration_lock);
191 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
192 " while active SPC-3 registrations exist,"
193 " returning RESERVATION_CONFLICT\n");
194 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
201 int target_scsi2_reservation_release(struct se_task *task)
203 struct se_cmd *cmd = task->task_se_cmd;
204 struct se_device *dev = cmd->se_dev;
205 struct se_session *sess = cmd->se_sess;
206 struct se_portal_group *tpg = sess->se_tpg;
211 if (target_check_scsi2_reservation_conflict(cmd, &ret))
215 spin_lock(&dev->dev_reservation_lock);
216 if (!dev->dev_reserved_node_acl || !sess)
219 if (dev->dev_reserved_node_acl != sess->se_node_acl)
222 dev->dev_reserved_node_acl = NULL;
223 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
224 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
225 dev->dev_res_bin_isid = 0;
226 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
228 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
229 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
230 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
231 sess->se_node_acl->initiatorname);
234 spin_unlock(&dev->dev_reservation_lock);
237 task->task_scsi_status = GOOD;
238 transport_complete_task(task, 1);
243 int target_scsi2_reservation_reserve(struct se_task *task)
245 struct se_cmd *cmd = task->task_se_cmd;
246 struct se_device *dev = cmd->se_dev;
247 struct se_session *sess = cmd->se_sess;
248 struct se_portal_group *tpg = sess->se_tpg;
251 if ((cmd->t_task_cdb[1] & 0x01) &&
252 (cmd->t_task_cdb[1] & 0x02)) {
253 pr_err("LongIO and Obselete Bits set, returning"
254 " ILLEGAL_REQUEST\n");
255 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
260 * This is currently the case for target_core_mod passthrough struct se_cmd
265 if (target_check_scsi2_reservation_conflict(cmd, &ret))
269 spin_lock(&dev->dev_reservation_lock);
270 if (dev->dev_reserved_node_acl &&
271 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
272 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
273 tpg->se_tpg_tfo->get_fabric_name());
274 pr_err("Original reserver LUN: %u %s\n",
275 cmd->se_lun->unpacked_lun,
276 dev->dev_reserved_node_acl->initiatorname);
277 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
278 " from %s \n", cmd->se_lun->unpacked_lun,
279 cmd->se_deve->mapped_lun,
280 sess->se_node_acl->initiatorname);
281 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
286 dev->dev_reserved_node_acl = sess->se_node_acl;
287 dev->dev_flags |= DF_SPC2_RESERVATIONS;
288 if (sess->sess_bin_isid != 0) {
289 dev->dev_res_bin_isid = sess->sess_bin_isid;
290 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
292 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
293 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
294 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
295 sess->se_node_acl->initiatorname);
298 spin_unlock(&dev->dev_reservation_lock);
301 task->task_scsi_status = GOOD;
302 transport_complete_task(task, 1);
309 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
311 * This function is called by those initiator ports who are *NOT*
312 * the active PR reservation holder when a reservation is present.
314 static int core_scsi3_pr_seq_non_holder(
319 struct se_dev_entry *se_deve;
320 struct se_session *se_sess = cmd->se_sess;
321 int other_cdb = 0, ignore_reg;
322 int registered_nexus = 0, ret = 1; /* Conflict by default */
323 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
324 int we = 0; /* Write Exclusive */
325 int legacy = 0; /* Act like a legacy device and return
326 * RESERVATION CONFLICT on some CDBs */
328 * A legacy SPC-2 reservation is being held.
330 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
331 return core_scsi2_reservation_seq_non_holder(cmd,
334 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
336 * Determine if the registration should be ignored due to
337 * non-matching ISIDs in core_scsi3_pr_reservation_check().
339 ignore_reg = (pr_reg_type & 0x80000000);
341 pr_reg_type &= ~0x80000000;
343 switch (pr_reg_type) {
344 case PR_TYPE_WRITE_EXCLUSIVE:
346 case PR_TYPE_EXCLUSIVE_ACCESS:
348 * Some commands are only allowed for the persistent reservation
351 if ((se_deve->def_pr_registered) && !(ignore_reg))
352 registered_nexus = 1;
354 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
356 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
358 * Some commands are only allowed for registered I_T Nexuses.
361 if ((se_deve->def_pr_registered) && !(ignore_reg))
362 registered_nexus = 1;
364 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
366 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
368 * Each registered I_T Nexus is a reservation holder.
371 if ((se_deve->def_pr_registered) && !(ignore_reg))
372 registered_nexus = 1;
378 * Referenced from spc4r17 table 45 for *NON* PR holder access
381 case SECURITY_PROTOCOL_IN:
382 if (registered_nexus)
390 case RECEIVE_DIAGNOSTIC:
395 if (registered_nexus) {
399 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
401 case PERSISTENT_RESERVE_OUT:
403 * This follows PERSISTENT_RESERVE_OUT service actions that
404 * are allowed in the presence of various reservations.
405 * See spc4r17, table 46
407 switch (cdb[1] & 0x1f) {
410 case PRO_PREEMPT_AND_ABORT:
411 ret = (registered_nexus) ? 0 : 1;
414 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
417 case PRO_REGISTER_AND_MOVE:
422 ret = (registered_nexus) ? 0 : 1;
425 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
426 " action: 0x%02x\n", cdb[1] & 0x1f);
432 /* Handled by CRH=1 in target_scsi2_reservation_release() */
437 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
440 case TEST_UNIT_READY:
441 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
444 switch (cdb[1] & 0x1f) {
445 case MI_MANAGEMENT_PROTOCOL_IN:
446 if (registered_nexus) {
450 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
452 case MI_REPORT_SUPPORTED_OPERATION_CODES:
453 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
458 if (registered_nexus) {
462 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
464 case MI_REPORT_ALIASES:
465 case MI_REPORT_IDENTIFYING_INFORMATION:
466 case MI_REPORT_PRIORITY:
467 case MI_REPORT_TARGET_PGS:
468 case MI_REPORT_TIMESTAMP:
469 ret = 0; /* Allowed */
472 pr_err("Unknown MI Service Action: 0x%02x\n",
477 case ACCESS_CONTROL_IN:
478 case ACCESS_CONTROL_OUT:
481 case READ_MEDIA_SERIAL_NUMBER:
484 case PERSISTENT_RESERVE_IN:
485 ret = 0; /*/ Allowed CDBs */
492 * Case where the CDB is explicitly allowed in the above switch
495 if (!ret && !other_cdb) {
497 pr_debug("Allowing explict CDB: 0x%02x for %s"
498 " reservation holder\n", cdb[0],
499 core_scsi3_pr_dump_type(pr_reg_type));
504 * Check if write exclusive initiator ports *NOT* holding the
505 * WRITE_EXCLUSIVE_* reservation.
507 if ((we) && !(registered_nexus)) {
508 if (cmd->data_direction == DMA_TO_DEVICE) {
510 * Conflict for write exclusive
512 pr_debug("%s Conflict for unregistered nexus"
513 " %s CDB: 0x%02x to %s reservation\n",
514 transport_dump_cmd_direction(cmd),
515 se_sess->se_node_acl->initiatorname, cdb[0],
516 core_scsi3_pr_dump_type(pr_reg_type));
520 * Allow non WRITE CDBs for all Write Exclusive
521 * PR TYPEs to pass for registered and
522 * non-registered_nexuxes NOT holding the reservation.
524 * We only make noise for the unregisterd nexuses,
525 * as we expect registered non-reservation holding
526 * nexuses to issue CDBs.
529 if (!registered_nexus) {
530 pr_debug("Allowing implict CDB: 0x%02x"
531 " for %s reservation on unregistered"
533 core_scsi3_pr_dump_type(pr_reg_type));
538 } else if ((reg_only) || (all_reg)) {
539 if (registered_nexus) {
541 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
542 * allow commands from registered nexuses.
545 pr_debug("Allowing implict CDB: 0x%02x for %s"
546 " reservation\n", cdb[0],
547 core_scsi3_pr_dump_type(pr_reg_type));
552 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
553 " for %s reservation\n", transport_dump_cmd_direction(cmd),
554 (registered_nexus) ? "" : "un",
555 se_sess->se_node_acl->initiatorname, cdb[0],
556 core_scsi3_pr_dump_type(pr_reg_type));
558 return 1; /* Conflict by default */
561 static u32 core_scsi3_pr_generation(struct se_device *dev)
563 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
566 * PRGeneration field shall contain the value of a 32-bit wrapping
567 * counter mainted by the device server.
569 * Note that this is done regardless of Active Persist across
570 * Target PowerLoss (APTPL)
572 * See spc4r17 section 6.3.12 READ_KEYS service action
574 spin_lock(&dev->dev_reservation_lock);
575 prg = su_dev->t10_pr.pr_generation++;
576 spin_unlock(&dev->dev_reservation_lock);
581 static int core_scsi3_pr_reservation_check(
585 struct se_device *dev = cmd->se_dev;
586 struct se_session *sess = cmd->se_sess;
592 * A legacy SPC-2 reservation is being held.
594 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
595 return core_scsi2_reservation_check(cmd, pr_reg_type);
597 spin_lock(&dev->dev_reservation_lock);
598 if (!dev->dev_pr_res_holder) {
599 spin_unlock(&dev->dev_reservation_lock);
602 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
603 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
604 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
605 spin_unlock(&dev->dev_reservation_lock);
608 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
609 spin_unlock(&dev->dev_reservation_lock);
612 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
613 sess->sess_bin_isid) ? 0 : -EINVAL;
615 * Use bit in *pr_reg_type to notify ISID mismatch in
616 * core_scsi3_pr_seq_non_holder().
619 *pr_reg_type |= 0x80000000;
620 spin_unlock(&dev->dev_reservation_lock);
625 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
626 struct se_device *dev,
627 struct se_node_acl *nacl,
628 struct se_dev_entry *deve,
634 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
635 struct t10_pr_registration *pr_reg;
637 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
639 pr_err("Unable to allocate struct t10_pr_registration\n");
643 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
645 if (!pr_reg->pr_aptpl_buf) {
646 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
647 kmem_cache_free(t10_pr_reg_cache, pr_reg);
651 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
652 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
653 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
654 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
655 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
656 atomic_set(&pr_reg->pr_res_holders, 0);
657 pr_reg->pr_reg_nacl = nacl;
658 pr_reg->pr_reg_deve = deve;
659 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
660 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
661 pr_reg->pr_res_key = sa_res_key;
662 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
663 pr_reg->pr_reg_aptpl = aptpl;
664 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
666 * If an ISID value for this SCSI Initiator Port exists,
667 * save it to the registration now.
670 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
671 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
672 pr_reg->isid_present_at_reg = 1;
678 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
679 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
682 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
685 static struct t10_pr_registration *__core_scsi3_alloc_registration(
686 struct se_device *dev,
687 struct se_node_acl *nacl,
688 struct se_dev_entry *deve,
694 struct se_dev_entry *deve_tmp;
695 struct se_node_acl *nacl_tmp;
696 struct se_port *port, *port_tmp;
697 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
698 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
701 * Create a registration for the I_T Nexus upon which the
702 * PROUT REGISTER was received.
704 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
705 sa_res_key, all_tg_pt, aptpl);
709 * Return pointer to pr_reg for ALL_TG_PT=0
714 * Create list of matching SCSI Initiator Port registrations
717 spin_lock(&dev->se_port_lock);
718 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
719 atomic_inc(&port->sep_tg_pt_ref_cnt);
720 smp_mb__after_atomic_inc();
721 spin_unlock(&dev->se_port_lock);
723 spin_lock_bh(&port->sep_alua_lock);
724 list_for_each_entry(deve_tmp, &port->sep_alua_list,
727 * This pointer will be NULL for demo mode MappedLUNs
728 * that have not been make explict via a ConfigFS
729 * MappedLUN group for the SCSI Initiator Node ACL.
731 if (!deve_tmp->se_lun_acl)
734 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
736 * Skip the matching struct se_node_acl that is allocated
739 if (nacl == nacl_tmp)
742 * Only perform PR registrations for target ports on
743 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
746 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
749 * Look for a matching Initiator Node ACL in ASCII format
751 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
754 atomic_inc(&deve_tmp->pr_ref_count);
755 smp_mb__after_atomic_inc();
756 spin_unlock_bh(&port->sep_alua_lock);
758 * Grab a configfs group dependency that is released
759 * for the exception path at label out: below, or upon
760 * completion of adding ALL_TG_PT=1 registrations in
761 * __core_scsi3_add_registration()
763 ret = core_scsi3_lunacl_depend_item(deve_tmp);
765 pr_err("core_scsi3_lunacl_depend"
767 atomic_dec(&port->sep_tg_pt_ref_cnt);
768 smp_mb__after_atomic_dec();
769 atomic_dec(&deve_tmp->pr_ref_count);
770 smp_mb__after_atomic_dec();
774 * Located a matching SCSI Initiator Port on a different
775 * port, allocate the pr_reg_atp and attach it to the
776 * pr_reg->pr_reg_atp_list that will be processed once
777 * the original *pr_reg is processed in
778 * __core_scsi3_add_registration()
780 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
781 nacl_tmp, deve_tmp, NULL,
782 sa_res_key, all_tg_pt, aptpl);
784 atomic_dec(&port->sep_tg_pt_ref_cnt);
785 smp_mb__after_atomic_dec();
786 atomic_dec(&deve_tmp->pr_ref_count);
787 smp_mb__after_atomic_dec();
788 core_scsi3_lunacl_undepend_item(deve_tmp);
792 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
793 &pr_reg->pr_reg_atp_list);
794 spin_lock_bh(&port->sep_alua_lock);
796 spin_unlock_bh(&port->sep_alua_lock);
798 spin_lock(&dev->se_port_lock);
799 atomic_dec(&port->sep_tg_pt_ref_cnt);
800 smp_mb__after_atomic_dec();
802 spin_unlock(&dev->se_port_lock);
806 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
807 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
808 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
809 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
810 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
812 kmem_cache_free(t10_pr_reg_cache, pr_reg);
816 int core_scsi3_alloc_aptpl_registration(
817 struct t10_reservation *pr_tmpl,
819 unsigned char *i_port,
822 unsigned char *t_port,
829 struct t10_pr_registration *pr_reg;
831 if (!i_port || !t_port || !sa_res_key) {
832 pr_err("Illegal parameters for APTPL registration\n");
836 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
838 pr_err("Unable to allocate struct t10_pr_registration\n");
841 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
843 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
844 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
845 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
846 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
847 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
848 atomic_set(&pr_reg->pr_res_holders, 0);
849 pr_reg->pr_reg_nacl = NULL;
850 pr_reg->pr_reg_deve = NULL;
851 pr_reg->pr_res_mapped_lun = mapped_lun;
852 pr_reg->pr_aptpl_target_lun = target_lun;
853 pr_reg->pr_res_key = sa_res_key;
854 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
855 pr_reg->pr_reg_aptpl = 1;
856 pr_reg->pr_reg_tg_pt_lun = NULL;
857 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
858 pr_reg->pr_res_type = type;
860 * If an ISID value had been saved in APTPL metadata for this
861 * SCSI Initiator Port, restore it now.
864 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
865 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
866 pr_reg->isid_present_at_reg = 1;
869 * Copy the i_port and t_port information from caller.
871 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
872 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
873 pr_reg->pr_reg_tpgt = tpgt;
875 * Set pr_res_holder from caller, the pr_reg who is the reservation
876 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
877 * the Initiator Node LUN ACL from the fabric module is created for
880 pr_reg->pr_res_holder = res_holder;
882 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
883 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
884 " metadata\n", (res_holder) ? "+reservation" : "");
888 static void core_scsi3_aptpl_reserve(
889 struct se_device *dev,
890 struct se_portal_group *tpg,
891 struct se_node_acl *node_acl,
892 struct t10_pr_registration *pr_reg)
894 char i_buf[PR_REG_ISID_ID_LEN];
897 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
898 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
901 spin_lock(&dev->dev_reservation_lock);
902 dev->dev_pr_res_holder = pr_reg;
903 spin_unlock(&dev->dev_reservation_lock);
905 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
906 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
907 tpg->se_tpg_tfo->get_fabric_name(),
908 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
909 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
910 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
911 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
912 (prf_isid) ? &i_buf[0] : "");
915 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
916 struct t10_pr_registration *, int, int);
918 static int __core_scsi3_check_aptpl_registration(
919 struct se_device *dev,
920 struct se_portal_group *tpg,
923 struct se_node_acl *nacl,
924 struct se_dev_entry *deve)
926 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
927 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
928 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
929 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
932 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
933 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
935 * Copy Initiator Port information from struct se_node_acl
937 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
938 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
939 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
940 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
942 * Look for the matching registrations+reservation from those
943 * created from APTPL metadata. Note that multiple registrations
944 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
947 spin_lock(&pr_tmpl->aptpl_reg_lock);
948 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
950 if (!strcmp(pr_reg->pr_iport, i_port) &&
951 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
952 !(strcmp(pr_reg->pr_tport, t_port)) &&
953 (pr_reg->pr_reg_tpgt == tpgt) &&
954 (pr_reg->pr_aptpl_target_lun == target_lun)) {
956 pr_reg->pr_reg_nacl = nacl;
957 pr_reg->pr_reg_deve = deve;
958 pr_reg->pr_reg_tg_pt_lun = lun;
960 list_del(&pr_reg->pr_reg_aptpl_list);
961 spin_unlock(&pr_tmpl->aptpl_reg_lock);
963 * At this point all of the pointers in *pr_reg will
964 * be setup, so go ahead and add the registration.
967 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
969 * If this registration is the reservation holder,
970 * make that happen now..
972 if (pr_reg->pr_res_holder)
973 core_scsi3_aptpl_reserve(dev, tpg,
976 * Reenable pr_aptpl_active to accept new metadata
977 * updates once the SCSI device is active again..
979 spin_lock(&pr_tmpl->aptpl_reg_lock);
980 pr_tmpl->pr_aptpl_active = 1;
983 spin_unlock(&pr_tmpl->aptpl_reg_lock);
988 int core_scsi3_check_aptpl_registration(
989 struct se_device *dev,
990 struct se_portal_group *tpg,
992 struct se_lun_acl *lun_acl)
994 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
995 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
996 struct se_dev_entry *deve = &nacl->device_list[lun_acl->mapped_lun];
998 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1001 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1002 lun->unpacked_lun, nacl, deve);
1005 static void __core_scsi3_dump_registration(
1006 struct target_core_fabric_ops *tfo,
1007 struct se_device *dev,
1008 struct se_node_acl *nacl,
1009 struct t10_pr_registration *pr_reg,
1012 struct se_portal_group *se_tpg = nacl->se_tpg;
1013 char i_buf[PR_REG_ISID_ID_LEN];
1016 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1017 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1018 PR_REG_ISID_ID_LEN);
1020 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1021 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1022 "_AND_MOVE" : (register_type == 1) ?
1023 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1024 (prf_isid) ? i_buf : "");
1025 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1026 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1027 tfo->tpg_get_tag(se_tpg));
1028 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1029 " Port(s)\n", tfo->get_fabric_name(),
1030 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1031 dev->transport->name);
1032 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1033 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1034 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1035 pr_reg->pr_reg_aptpl);
1039 * this function can be called with struct se_device->dev_reservation_lock
1040 * when register_move = 1
1042 static void __core_scsi3_add_registration(
1043 struct se_device *dev,
1044 struct se_node_acl *nacl,
1045 struct t10_pr_registration *pr_reg,
1049 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1050 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1051 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1052 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1055 * Increment PRgeneration counter for struct se_device upon a successful
1056 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1058 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1059 * action, the struct se_device->dev_reservation_lock will already be held,
1060 * so we do not call core_scsi3_pr_generation() which grabs the lock
1063 pr_reg->pr_res_generation = (register_move) ?
1064 su_dev->t10_pr.pr_generation++ :
1065 core_scsi3_pr_generation(dev);
1067 spin_lock(&pr_tmpl->registration_lock);
1068 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1069 pr_reg->pr_reg_deve->def_pr_registered = 1;
1071 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1072 spin_unlock(&pr_tmpl->registration_lock);
1074 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1076 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1079 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1080 * allocated in __core_scsi3_alloc_registration()
1082 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1083 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1084 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1086 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1088 spin_lock(&pr_tmpl->registration_lock);
1089 list_add_tail(&pr_reg_tmp->pr_reg_list,
1090 &pr_tmpl->registration_list);
1091 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1093 __core_scsi3_dump_registration(tfo, dev,
1094 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1096 spin_unlock(&pr_tmpl->registration_lock);
1098 * Drop configfs group dependency reference from
1099 * __core_scsi3_alloc_registration()
1101 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1105 static int core_scsi3_alloc_registration(
1106 struct se_device *dev,
1107 struct se_node_acl *nacl,
1108 struct se_dev_entry *deve,
1109 unsigned char *isid,
1116 struct t10_pr_registration *pr_reg;
1118 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1119 sa_res_key, all_tg_pt, aptpl);
1123 __core_scsi3_add_registration(dev, nacl, pr_reg,
1124 register_type, register_move);
1128 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1129 struct se_device *dev,
1130 struct se_node_acl *nacl,
1131 unsigned char *isid)
1133 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1134 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1135 struct se_portal_group *tpg;
1137 spin_lock(&pr_tmpl->registration_lock);
1138 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1139 &pr_tmpl->registration_list, pr_reg_list) {
1141 * First look for a matching struct se_node_acl
1143 if (pr_reg->pr_reg_nacl != nacl)
1146 tpg = pr_reg->pr_reg_nacl->se_tpg;
1148 * If this registration does NOT contain a fabric provided
1149 * ISID, then we have found a match.
1151 if (!pr_reg->isid_present_at_reg) {
1153 * Determine if this SCSI device server requires that
1154 * SCSI Intiatior TransportID w/ ISIDs is enforced
1155 * for fabric modules (iSCSI) requiring them.
1157 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1158 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
1161 atomic_inc(&pr_reg->pr_res_holders);
1162 smp_mb__after_atomic_inc();
1163 spin_unlock(&pr_tmpl->registration_lock);
1167 * If the *pr_reg contains a fabric defined ISID for multi-value
1168 * SCSI Initiator Port TransportIDs, then we expect a valid
1169 * matching ISID to be provided by the local SCSI Initiator Port.
1173 if (strcmp(isid, pr_reg->pr_reg_isid))
1176 atomic_inc(&pr_reg->pr_res_holders);
1177 smp_mb__after_atomic_inc();
1178 spin_unlock(&pr_tmpl->registration_lock);
1181 spin_unlock(&pr_tmpl->registration_lock);
1186 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1187 struct se_device *dev,
1188 struct se_node_acl *nacl,
1189 struct se_session *sess)
1191 struct se_portal_group *tpg = nacl->se_tpg;
1192 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1194 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1195 memset(&buf[0], 0, PR_REG_ISID_LEN);
1196 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1201 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1204 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1206 atomic_dec(&pr_reg->pr_res_holders);
1207 smp_mb__after_atomic_dec();
1210 static int core_scsi3_check_implict_release(
1211 struct se_device *dev,
1212 struct t10_pr_registration *pr_reg)
1214 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1215 struct t10_pr_registration *pr_res_holder;
1218 spin_lock(&dev->dev_reservation_lock);
1219 pr_res_holder = dev->dev_pr_res_holder;
1220 if (!pr_res_holder) {
1221 spin_unlock(&dev->dev_reservation_lock);
1224 if (pr_res_holder == pr_reg) {
1226 * Perform an implict RELEASE if the registration that
1227 * is being released is holding the reservation.
1229 * From spc4r17, section 5.7.11.1:
1231 * e) If the I_T nexus is the persistent reservation holder
1232 * and the persistent reservation is not an all registrants
1233 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1234 * service action or REGISTER AND IGNORE EXISTING KEY
1235 * service action with the SERVICE ACTION RESERVATION KEY
1236 * field set to zero (see 5.7.11.3).
1238 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1241 * For 'All Registrants' reservation types, all existing
1242 * registrations are still processed as reservation holders
1243 * in core_scsi3_pr_seq_non_holder() after the initial
1244 * reservation holder is implictly released here.
1246 } else if (pr_reg->pr_reg_all_tg_pt &&
1247 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1248 pr_reg->pr_reg_nacl->initiatorname)) &&
1249 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1250 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1251 " UNREGISTER while existing reservation with matching"
1252 " key 0x%016Lx is present from another SCSI Initiator"
1253 " Port\n", pr_reg->pr_res_key);
1256 spin_unlock(&dev->dev_reservation_lock);
1262 * Called with struct t10_reservation->registration_lock held.
1264 static void __core_scsi3_free_registration(
1265 struct se_device *dev,
1266 struct t10_pr_registration *pr_reg,
1267 struct list_head *preempt_and_abort_list,
1270 struct target_core_fabric_ops *tfo =
1271 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1272 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1273 char i_buf[PR_REG_ISID_ID_LEN];
1276 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1277 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1278 PR_REG_ISID_ID_LEN);
1280 pr_reg->pr_reg_deve->def_pr_registered = 0;
1281 pr_reg->pr_reg_deve->pr_res_key = 0;
1282 list_del(&pr_reg->pr_reg_list);
1284 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1285 * so call core_scsi3_put_pr_reg() to decrement our reference.
1288 core_scsi3_put_pr_reg(pr_reg);
1290 * Wait until all reference from any other I_T nexuses for this
1291 * *pr_reg have been released. Because list_del() is called above,
1292 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1293 * count back to zero, and we release *pr_reg.
1295 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1296 spin_unlock(&pr_tmpl->registration_lock);
1297 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1298 tfo->get_fabric_name());
1300 spin_lock(&pr_tmpl->registration_lock);
1303 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1304 " Node: %s%s\n", tfo->get_fabric_name(),
1305 pr_reg->pr_reg_nacl->initiatorname,
1306 (prf_isid) ? &i_buf[0] : "");
1307 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1308 " Port(s)\n", tfo->get_fabric_name(),
1309 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1310 dev->transport->name);
1311 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1312 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1313 pr_reg->pr_res_generation);
1315 if (!preempt_and_abort_list) {
1316 pr_reg->pr_reg_deve = NULL;
1317 pr_reg->pr_reg_nacl = NULL;
1318 kfree(pr_reg->pr_aptpl_buf);
1319 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1323 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1324 * are released once the ABORT_TASK_SET has completed..
1326 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1329 void core_scsi3_free_pr_reg_from_nacl(
1330 struct se_device *dev,
1331 struct se_node_acl *nacl)
1333 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1334 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1336 * If the passed se_node_acl matches the reservation holder,
1337 * release the reservation.
1339 spin_lock(&dev->dev_reservation_lock);
1340 pr_res_holder = dev->dev_pr_res_holder;
1341 if ((pr_res_holder != NULL) &&
1342 (pr_res_holder->pr_reg_nacl == nacl))
1343 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1344 spin_unlock(&dev->dev_reservation_lock);
1346 * Release any registration associated with the struct se_node_acl.
1348 spin_lock(&pr_tmpl->registration_lock);
1349 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1350 &pr_tmpl->registration_list, pr_reg_list) {
1352 if (pr_reg->pr_reg_nacl != nacl)
1355 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1357 spin_unlock(&pr_tmpl->registration_lock);
1360 void core_scsi3_free_all_registrations(
1361 struct se_device *dev)
1363 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1364 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1366 spin_lock(&dev->dev_reservation_lock);
1367 pr_res_holder = dev->dev_pr_res_holder;
1368 if (pr_res_holder != NULL) {
1369 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1370 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1373 spin_unlock(&dev->dev_reservation_lock);
1375 spin_lock(&pr_tmpl->registration_lock);
1376 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1377 &pr_tmpl->registration_list, pr_reg_list) {
1379 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1381 spin_unlock(&pr_tmpl->registration_lock);
1383 spin_lock(&pr_tmpl->aptpl_reg_lock);
1384 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1385 pr_reg_aptpl_list) {
1386 list_del(&pr_reg->pr_reg_aptpl_list);
1387 kfree(pr_reg->pr_aptpl_buf);
1388 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1390 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1393 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1395 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1396 &tpg->tpg_group.cg_item);
1399 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1401 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1402 &tpg->tpg_group.cg_item);
1404 atomic_dec(&tpg->tpg_pr_ref_count);
1405 smp_mb__after_atomic_dec();
1408 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1410 struct se_portal_group *tpg = nacl->se_tpg;
1412 if (nacl->dynamic_node_acl)
1415 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1416 &nacl->acl_group.cg_item);
1419 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1421 struct se_portal_group *tpg = nacl->se_tpg;
1423 if (nacl->dynamic_node_acl) {
1424 atomic_dec(&nacl->acl_pr_ref_count);
1425 smp_mb__after_atomic_dec();
1429 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1430 &nacl->acl_group.cg_item);
1432 atomic_dec(&nacl->acl_pr_ref_count);
1433 smp_mb__after_atomic_dec();
1436 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1438 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1439 struct se_node_acl *nacl;
1440 struct se_portal_group *tpg;
1442 * For nacl->dynamic_node_acl=1
1447 nacl = lun_acl->se_lun_nacl;
1450 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1451 &lun_acl->se_lun_group.cg_item);
1454 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1456 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1457 struct se_node_acl *nacl;
1458 struct se_portal_group *tpg;
1460 * For nacl->dynamic_node_acl=1
1463 atomic_dec(&se_deve->pr_ref_count);
1464 smp_mb__after_atomic_dec();
1467 nacl = lun_acl->se_lun_nacl;
1470 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1471 &lun_acl->se_lun_group.cg_item);
1473 atomic_dec(&se_deve->pr_ref_count);
1474 smp_mb__after_atomic_dec();
1477 static int core_scsi3_decode_spec_i_port(
1479 struct se_portal_group *tpg,
1480 unsigned char *l_isid,
1485 struct se_device *dev = cmd->se_dev;
1486 struct se_port *tmp_port;
1487 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1488 struct se_session *se_sess = cmd->se_sess;
1489 struct se_node_acl *dest_node_acl = NULL;
1490 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1491 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1492 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1493 struct list_head tid_dest_list;
1494 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1495 struct target_core_fabric_ops *tmp_tf_ops;
1497 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1498 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1499 u32 tpdl, tid_len = 0;
1500 int ret, dest_local_nexus, prf_isid;
1503 memset(dest_iport, 0, 64);
1504 INIT_LIST_HEAD(&tid_dest_list);
1506 local_se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1508 * Allocate a struct pr_transport_id_holder and setup the
1509 * local_node_acl and local_se_deve pointers and add to
1510 * struct list_head tid_dest_list for add registration
1511 * processing in the loop of tid_dest_list below.
1513 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1515 pr_err("Unable to allocate tidh_new\n");
1516 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1519 INIT_LIST_HEAD(&tidh_new->dest_list);
1520 tidh_new->dest_tpg = tpg;
1521 tidh_new->dest_node_acl = se_sess->se_node_acl;
1522 tidh_new->dest_se_deve = local_se_deve;
1524 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1525 se_sess->se_node_acl, local_se_deve, l_isid,
1526 sa_res_key, all_tg_pt, aptpl);
1527 if (!local_pr_reg) {
1529 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1532 tidh_new->dest_pr_reg = local_pr_reg;
1534 * The local I_T nexus does not hold any configfs dependances,
1535 * so we set tid_h->dest_local_nexus=1 to prevent the
1536 * configfs_undepend_item() calls in the tid_dest_list loops below.
1538 tidh_new->dest_local_nexus = 1;
1539 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1541 buf = transport_kmap_first_data_page(cmd);
1543 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1544 * first extract TransportID Parameter Data Length, and make sure
1545 * the value matches up to the SCSI expected data transfer length.
1547 tpdl = (buf[24] & 0xff) << 24;
1548 tpdl |= (buf[25] & 0xff) << 16;
1549 tpdl |= (buf[26] & 0xff) << 8;
1550 tpdl |= buf[27] & 0xff;
1552 if ((tpdl + 28) != cmd->data_length) {
1553 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1554 " does not equal CDB data_length: %u\n", tpdl,
1556 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1561 * Start processing the received transport IDs using the
1562 * receiving I_T Nexus portal's fabric dependent methods to
1563 * obtain the SCSI Initiator Port/Device Identifiers.
1568 proto_ident = (ptr[0] & 0x0f);
1571 spin_lock(&dev->se_port_lock);
1572 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1573 tmp_tpg = tmp_port->sep_tpg;
1576 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1579 if (!tmp_tf_ops->get_fabric_proto_ident ||
1580 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1583 * Look for the matching proto_ident provided by
1584 * the received TransportID
1586 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1587 if (tmp_proto_ident != proto_ident)
1589 dest_rtpi = tmp_port->sep_rtpi;
1591 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1592 tmp_tpg, (const char *)ptr, &tid_len,
1597 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1598 smp_mb__after_atomic_inc();
1599 spin_unlock(&dev->se_port_lock);
1601 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1603 pr_err(" core_scsi3_tpg_depend_item()"
1605 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1606 smp_mb__after_atomic_dec();
1607 cmd->scsi_sense_reason =
1608 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1613 * Locate the desination initiator ACL to be registered
1614 * from the decoded fabric module specific TransportID
1617 spin_lock_irq(&tmp_tpg->acl_node_lock);
1618 dest_node_acl = __core_tpg_get_initiator_node_acl(
1620 if (dest_node_acl) {
1621 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1622 smp_mb__after_atomic_inc();
1624 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1626 if (!dest_node_acl) {
1627 core_scsi3_tpg_undepend_item(tmp_tpg);
1628 spin_lock(&dev->se_port_lock);
1632 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1634 pr_err("configfs_depend_item() failed"
1635 " for dest_node_acl->acl_group\n");
1636 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1637 smp_mb__after_atomic_dec();
1638 core_scsi3_tpg_undepend_item(tmp_tpg);
1639 cmd->scsi_sense_reason =
1640 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1646 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1647 " %s Port RTPI: %hu\n",
1648 dest_tpg->se_tpg_tfo->get_fabric_name(),
1649 dest_node_acl->initiatorname, dest_rtpi);
1651 spin_lock(&dev->se_port_lock);
1654 spin_unlock(&dev->se_port_lock);
1657 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1659 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1664 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1665 " tid_len: %d for %s + %s\n",
1666 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1667 tpdl, tid_len, i_str, iport_ptr);
1669 if (tid_len > tpdl) {
1670 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1671 " %u for Transport ID: %s\n", tid_len, ptr);
1672 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1673 core_scsi3_tpg_undepend_item(dest_tpg);
1674 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1679 * Locate the desintation struct se_dev_entry pointer for matching
1680 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1683 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1685 if (!dest_se_deve) {
1686 pr_err("Unable to locate %s dest_se_deve"
1687 " from destination RTPI: %hu\n",
1688 dest_tpg->se_tpg_tfo->get_fabric_name(),
1691 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1692 core_scsi3_tpg_undepend_item(dest_tpg);
1693 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1698 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1700 pr_err("core_scsi3_lunacl_depend_item()"
1702 atomic_dec(&dest_se_deve->pr_ref_count);
1703 smp_mb__after_atomic_dec();
1704 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1705 core_scsi3_tpg_undepend_item(dest_tpg);
1706 cmd->scsi_sense_reason =
1707 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1712 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1713 " dest_se_deve mapped_lun: %u\n",
1714 dest_tpg->se_tpg_tfo->get_fabric_name(),
1715 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1718 * Skip any TransportIDs that already have a registration for
1721 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1724 core_scsi3_put_pr_reg(pr_reg_e);
1725 core_scsi3_lunacl_undepend_item(dest_se_deve);
1726 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1727 core_scsi3_tpg_undepend_item(dest_tpg);
1734 * Allocate a struct pr_transport_id_holder and setup
1735 * the dest_node_acl and dest_se_deve pointers for the
1738 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1741 pr_err("Unable to allocate tidh_new\n");
1742 core_scsi3_lunacl_undepend_item(dest_se_deve);
1743 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1744 core_scsi3_tpg_undepend_item(dest_tpg);
1745 cmd->scsi_sense_reason =
1746 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1750 INIT_LIST_HEAD(&tidh_new->dest_list);
1751 tidh_new->dest_tpg = dest_tpg;
1752 tidh_new->dest_node_acl = dest_node_acl;
1753 tidh_new->dest_se_deve = dest_se_deve;
1756 * Allocate, but do NOT add the registration for the
1757 * TransportID referenced SCSI Initiator port. This
1758 * done because of the following from spc4r17 in section
1759 * 6.14.3 wrt SPEC_I_PT:
1761 * "If a registration fails for any initiator port (e.g., if th
1762 * logical unit does not have enough resources available to
1763 * hold the registration information), no registrations shall be
1764 * made, and the command shall be terminated with
1765 * CHECK CONDITION status."
1767 * That means we call __core_scsi3_alloc_registration() here,
1768 * and then call __core_scsi3_add_registration() in the
1769 * 2nd loop which will never fail.
1771 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1772 dest_node_acl, dest_se_deve, iport_ptr,
1773 sa_res_key, all_tg_pt, aptpl);
1775 core_scsi3_lunacl_undepend_item(dest_se_deve);
1776 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1777 core_scsi3_tpg_undepend_item(dest_tpg);
1779 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1783 tidh_new->dest_pr_reg = dest_pr_reg;
1784 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1792 transport_kunmap_first_data_page(cmd);
1795 * Go ahead and create a registrations from tid_dest_list for the
1796 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1799 * The SA Reservation Key from the PROUT is set for the
1800 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1801 * means that the TransportID Initiator port will be
1802 * registered on all of the target ports in the SCSI target device
1803 * ALL_TG_PT=0 means the registration will only be for the
1804 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1807 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1808 dest_tpg = tidh->dest_tpg;
1809 dest_node_acl = tidh->dest_node_acl;
1810 dest_se_deve = tidh->dest_se_deve;
1811 dest_pr_reg = tidh->dest_pr_reg;
1812 dest_local_nexus = tidh->dest_local_nexus;
1814 list_del(&tidh->dest_list);
1817 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1818 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1819 PR_REG_ISID_ID_LEN);
1821 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1824 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1825 " registered Transport ID for Node: %s%s Mapped LUN:"
1826 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1827 dest_node_acl->initiatorname, (prf_isid) ?
1828 &i_buf[0] : "", dest_se_deve->mapped_lun);
1830 if (dest_local_nexus)
1833 core_scsi3_lunacl_undepend_item(dest_se_deve);
1834 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1835 core_scsi3_tpg_undepend_item(dest_tpg);
1840 transport_kunmap_first_data_page(cmd);
1842 * For the failure case, release everything from tid_dest_list
1843 * including *dest_pr_reg and the configfs dependances..
1845 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1846 dest_tpg = tidh->dest_tpg;
1847 dest_node_acl = tidh->dest_node_acl;
1848 dest_se_deve = tidh->dest_se_deve;
1849 dest_pr_reg = tidh->dest_pr_reg;
1850 dest_local_nexus = tidh->dest_local_nexus;
1852 list_del(&tidh->dest_list);
1855 * Release any extra ALL_TG_PT=1 registrations for
1856 * the SPEC_I_PT=1 case.
1858 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1859 &dest_pr_reg->pr_reg_atp_list,
1860 pr_reg_atp_mem_list) {
1861 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1862 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1863 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1866 kfree(dest_pr_reg->pr_aptpl_buf);
1867 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1869 if (dest_local_nexus)
1872 core_scsi3_lunacl_undepend_item(dest_se_deve);
1873 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1874 core_scsi3_tpg_undepend_item(dest_tpg);
1880 * Called with struct se_device->dev_reservation_lock held
1882 static int __core_scsi3_update_aptpl_buf(
1883 struct se_device *dev,
1885 u32 pr_aptpl_buf_len,
1886 int clear_aptpl_metadata)
1889 struct se_portal_group *tpg;
1890 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1891 struct t10_pr_registration *pr_reg;
1892 unsigned char tmp[512], isid_buf[32];
1896 memset(buf, 0, pr_aptpl_buf_len);
1898 * Called to clear metadata once APTPL has been deactivated.
1900 if (clear_aptpl_metadata) {
1901 snprintf(buf, pr_aptpl_buf_len,
1902 "No Registrations or Reservations\n");
1906 * Walk the registration list..
1908 spin_lock(&su_dev->t10_pr.registration_lock);
1909 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1914 tpg = pr_reg->pr_reg_nacl->se_tpg;
1915 lun = pr_reg->pr_reg_tg_pt_lun;
1917 * Write out any ISID value to APTPL metadata that was included
1918 * in the original registration.
1920 if (pr_reg->isid_present_at_reg)
1921 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1922 pr_reg->pr_reg_isid);
1924 * Include special metadata if the pr_reg matches the
1925 * reservation holder.
1927 if (dev->dev_pr_res_holder == pr_reg) {
1928 snprintf(tmp, 512, "PR_REG_START: %d"
1929 "\ninitiator_fabric=%s\n"
1930 "initiator_node=%s\n%s"
1932 "res_holder=1\nres_type=%02x\n"
1933 "res_scope=%02x\nres_all_tg_pt=%d\n"
1934 "mapped_lun=%u\n", reg_count,
1935 tpg->se_tpg_tfo->get_fabric_name(),
1936 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1937 pr_reg->pr_res_key, pr_reg->pr_res_type,
1938 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1939 pr_reg->pr_res_mapped_lun);
1941 snprintf(tmp, 512, "PR_REG_START: %d\n"
1942 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1943 "sa_res_key=%llu\nres_holder=0\n"
1944 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1945 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1946 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1947 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1948 pr_reg->pr_res_mapped_lun);
1951 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1952 pr_err("Unable to update renaming"
1953 " APTPL metadata\n");
1954 spin_unlock(&su_dev->t10_pr.registration_lock);
1957 len += sprintf(buf+len, "%s", tmp);
1960 * Include information about the associated SCSI target port.
1962 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1963 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1964 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1965 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1966 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1967 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1969 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1970 pr_err("Unable to update renaming"
1971 " APTPL metadata\n");
1972 spin_unlock(&su_dev->t10_pr.registration_lock);
1975 len += sprintf(buf+len, "%s", tmp);
1978 spin_unlock(&su_dev->t10_pr.registration_lock);
1981 len += sprintf(buf+len, "No Registrations or Reservations");
1986 static int core_scsi3_update_aptpl_buf(
1987 struct se_device *dev,
1989 u32 pr_aptpl_buf_len,
1990 int clear_aptpl_metadata)
1994 spin_lock(&dev->dev_reservation_lock);
1995 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
1996 clear_aptpl_metadata);
1997 spin_unlock(&dev->dev_reservation_lock);
2003 * Called with struct se_device->aptpl_file_mutex held
2005 static int __core_scsi3_write_aptpl_to_file(
2006 struct se_device *dev,
2008 u32 pr_aptpl_buf_len)
2010 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
2012 struct iovec iov[1];
2013 mm_segment_t old_fs;
2014 int flags = O_RDWR | O_CREAT | O_TRUNC;
2018 memset(iov, 0, sizeof(struct iovec));
2019 memset(path, 0, 512);
2021 if (strlen(&wwn->unit_serial[0]) >= 512) {
2022 pr_err("WWN value for struct se_device does not fit"
2023 " into path buffer\n");
2027 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2028 file = filp_open(path, flags, 0600);
2029 if (IS_ERR(file) || !file || !file->f_dentry) {
2030 pr_err("filp_open(%s) for APTPL metadata"
2032 return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
2035 iov[0].iov_base = &buf[0];
2036 if (!pr_aptpl_buf_len)
2037 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2039 iov[0].iov_len = pr_aptpl_buf_len;
2043 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2047 pr_debug("Error writing APTPL metadata file: %s\n", path);
2048 filp_close(file, NULL);
2051 filp_close(file, NULL);
2056 static int core_scsi3_update_and_write_aptpl(
2057 struct se_device *dev,
2058 unsigned char *in_buf,
2059 u32 in_pr_aptpl_buf_len)
2061 unsigned char null_buf[64], *buf;
2062 u32 pr_aptpl_buf_len;
2063 int ret, clear_aptpl_metadata = 0;
2065 * Can be called with a NULL pointer from PROUT service action CLEAR
2068 memset(null_buf, 0, 64);
2071 * This will clear the APTPL metadata to:
2072 * "No Registrations or Reservations" status
2074 pr_aptpl_buf_len = 64;
2075 clear_aptpl_metadata = 1;
2078 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2081 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2082 clear_aptpl_metadata);
2086 * __core_scsi3_write_aptpl_to_file() will call strlen()
2087 * on the passed buf to determine pr_aptpl_buf_len.
2089 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2096 static int core_scsi3_emulate_pro_register(
2105 struct se_session *se_sess = cmd->se_sess;
2106 struct se_device *dev = cmd->se_dev;
2107 struct se_dev_entry *se_deve;
2108 struct se_lun *se_lun = cmd->se_lun;
2109 struct se_portal_group *se_tpg;
2110 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2111 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2112 /* Used for APTPL metadata w/ UNREGISTER */
2113 unsigned char *pr_aptpl_buf = NULL;
2114 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2115 int pr_holder = 0, ret = 0, type;
2117 if (!se_sess || !se_lun) {
2118 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2119 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2122 se_tpg = se_sess->se_tpg;
2123 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2125 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2126 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2127 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2129 isid_ptr = &isid_buf[0];
2132 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2134 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2137 pr_warn("SPC-3 PR: Reservation Key non-zero"
2138 " for SA REGISTER, returning CONFLICT\n");
2139 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2143 * Do nothing but return GOOD status.
2150 * Perform the Service Action REGISTER on the Initiator
2151 * Port Endpoint that the PRO was received from on the
2152 * Logical Unit of the SCSI device server.
2154 ret = core_scsi3_alloc_registration(cmd->se_dev,
2155 se_sess->se_node_acl, se_deve, isid_ptr,
2156 sa_res_key, all_tg_pt, aptpl,
2159 pr_err("Unable to allocate"
2160 " struct t10_pr_registration\n");
2161 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2166 * Register both the Initiator port that received
2167 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2168 * TransportID from Parameter list and loop through
2169 * fabric dependent parameter list while calling
2170 * logic from of core_scsi3_alloc_registration() for
2171 * each TransportID provided SCSI Initiator Port/Device
2173 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2174 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2179 * Nothing left to do for the APTPL=0 case.
2182 pr_tmpl->pr_aptpl_active = 0;
2183 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2184 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2189 * Locate the newly allocated local I_T Nexus *pr_reg, and
2190 * update the APTPL metadata information using its
2191 * preallocated *pr_reg->pr_aptpl_buf.
2193 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2194 se_sess->se_node_acl, se_sess);
2196 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2197 &pr_reg->pr_aptpl_buf[0],
2198 pr_tmpl->pr_aptpl_buf_len);
2200 pr_tmpl->pr_aptpl_active = 1;
2201 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2204 core_scsi3_put_pr_reg(pr_reg);
2208 * Locate the existing *pr_reg via struct se_node_acl pointers
2211 type = pr_reg->pr_res_type;
2214 if (res_key != pr_reg->pr_res_key) {
2215 pr_err("SPC-3 PR REGISTER: Received"
2216 " res_key: 0x%016Lx does not match"
2217 " existing SA REGISTER res_key:"
2218 " 0x%016Lx\n", res_key,
2219 pr_reg->pr_res_key);
2220 core_scsi3_put_pr_reg(pr_reg);
2221 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2226 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2227 " set while sa_res_key=0\n");
2228 core_scsi3_put_pr_reg(pr_reg);
2229 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2233 * An existing ALL_TG_PT=1 registration being released
2234 * must also set ALL_TG_PT=1 in the incoming PROUT.
2236 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2237 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2238 " registration exists, but ALL_TG_PT=1 bit not"
2239 " present in received PROUT\n");
2240 core_scsi3_put_pr_reg(pr_reg);
2241 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2245 * Allocate APTPL metadata buffer used for UNREGISTER ops
2248 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2250 if (!pr_aptpl_buf) {
2251 pr_err("Unable to allocate"
2253 core_scsi3_put_pr_reg(pr_reg);
2254 cmd->scsi_sense_reason =
2255 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2260 * sa_res_key=0 Unregister Reservation Key for registered I_T
2261 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2265 pr_holder = core_scsi3_check_implict_release(
2266 cmd->se_dev, pr_reg);
2267 if (pr_holder < 0) {
2268 kfree(pr_aptpl_buf);
2269 core_scsi3_put_pr_reg(pr_reg);
2270 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2274 spin_lock(&pr_tmpl->registration_lock);
2276 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2277 * and matching pr_res_key.
2279 if (pr_reg->pr_reg_all_tg_pt) {
2280 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2281 &pr_tmpl->registration_list,
2284 if (!pr_reg_p->pr_reg_all_tg_pt)
2287 if (pr_reg_p->pr_res_key != res_key)
2290 if (pr_reg == pr_reg_p)
2293 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2294 pr_reg_p->pr_reg_nacl->initiatorname))
2297 __core_scsi3_free_registration(dev,
2302 * Release the calling I_T Nexus registration now..
2304 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2307 * From spc4r17, section 5.7.11.3 Unregistering
2309 * If the persistent reservation is a registrants only
2310 * type, the device server shall establish a unit
2311 * attention condition for the initiator port associated
2312 * with every registered I_T nexus except for the I_T
2313 * nexus on which the PERSISTENT RESERVE OUT command was
2314 * received, with the additional sense code set to
2315 * RESERVATIONS RELEASED.
2318 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2319 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2320 list_for_each_entry(pr_reg_p,
2321 &pr_tmpl->registration_list,
2324 core_scsi3_ua_allocate(
2325 pr_reg_p->pr_reg_nacl,
2326 pr_reg_p->pr_res_mapped_lun,
2328 ASCQ_2AH_RESERVATIONS_RELEASED);
2331 spin_unlock(&pr_tmpl->registration_lock);
2334 pr_tmpl->pr_aptpl_active = 0;
2335 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2336 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2337 " for UNREGISTER\n");
2341 ret = core_scsi3_update_and_write_aptpl(dev,
2343 pr_tmpl->pr_aptpl_buf_len);
2345 pr_tmpl->pr_aptpl_active = 1;
2346 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2347 " for UNREGISTER\n");
2350 kfree(pr_aptpl_buf);
2354 * Increment PRgeneration counter for struct se_device"
2355 * upon a successful REGISTER, see spc4r17 section 6.3.2
2356 * READ_KEYS service action.
2358 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2360 pr_reg->pr_res_key = sa_res_key;
2361 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2362 " Key for %s to: 0x%016Lx PRgeneration:"
2363 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2364 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2365 pr_reg->pr_reg_nacl->initiatorname,
2366 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2369 pr_tmpl->pr_aptpl_active = 0;
2370 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2371 core_scsi3_put_pr_reg(pr_reg);
2372 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2377 ret = core_scsi3_update_and_write_aptpl(dev,
2379 pr_tmpl->pr_aptpl_buf_len);
2381 pr_tmpl->pr_aptpl_active = 1;
2382 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2386 kfree(pr_aptpl_buf);
2387 core_scsi3_put_pr_reg(pr_reg);
2393 unsigned char *core_scsi3_pr_dump_type(int type)
2396 case PR_TYPE_WRITE_EXCLUSIVE:
2397 return "Write Exclusive Access";
2398 case PR_TYPE_EXCLUSIVE_ACCESS:
2399 return "Exclusive Access";
2400 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2401 return "Write Exclusive Access, Registrants Only";
2402 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2403 return "Exclusive Access, Registrants Only";
2404 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2405 return "Write Exclusive Access, All Registrants";
2406 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2407 return "Exclusive Access, All Registrants";
2412 return "Unknown SPC-3 PR Type";
2415 static int core_scsi3_pro_reserve(
2417 struct se_device *dev,
2422 struct se_session *se_sess = cmd->se_sess;
2423 struct se_dev_entry *se_deve;
2424 struct se_lun *se_lun = cmd->se_lun;
2425 struct se_portal_group *se_tpg;
2426 struct t10_pr_registration *pr_reg, *pr_res_holder;
2427 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2428 char i_buf[PR_REG_ISID_ID_LEN];
2431 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2433 if (!se_sess || !se_lun) {
2434 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2435 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2438 se_tpg = se_sess->se_tpg;
2439 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2441 * Locate the existing *pr_reg via struct se_node_acl pointers
2443 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2446 pr_err("SPC-3 PR: Unable to locate"
2447 " PR_REGISTERED *pr_reg for RESERVE\n");
2448 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2452 * From spc4r17 Section 5.7.9: Reserving:
2454 * An application client creates a persistent reservation by issuing
2455 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2456 * a registered I_T nexus with the following parameters:
2457 * a) RESERVATION KEY set to the value of the reservation key that is
2458 * registered with the logical unit for the I_T nexus; and
2460 if (res_key != pr_reg->pr_res_key) {
2461 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2462 " does not match existing SA REGISTER res_key:"
2463 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2464 core_scsi3_put_pr_reg(pr_reg);
2465 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2469 * From spc4r17 Section 5.7.9: Reserving:
2472 * b) TYPE field and SCOPE field set to the persistent reservation
2475 * Only one persistent reservation is allowed at a time per logical unit
2476 * and that persistent reservation has a scope of LU_SCOPE.
2478 if (scope != PR_SCOPE_LU_SCOPE) {
2479 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2480 core_scsi3_put_pr_reg(pr_reg);
2481 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2485 * See if we have an existing PR reservation holder pointer at
2486 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2489 spin_lock(&dev->dev_reservation_lock);
2490 pr_res_holder = dev->dev_pr_res_holder;
2491 if ((pr_res_holder)) {
2493 * From spc4r17 Section 5.7.9: Reserving:
2495 * If the device server receives a PERSISTENT RESERVE OUT
2496 * command from an I_T nexus other than a persistent reservation
2497 * holder (see 5.7.10) that attempts to create a persistent
2498 * reservation when a persistent reservation already exists for
2499 * the logical unit, then the command shall be completed with
2500 * RESERVATION CONFLICT status.
2502 if (pr_res_holder != pr_reg) {
2503 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2504 pr_err("SPC-3 PR: Attempted RESERVE from"
2505 " [%s]: %s while reservation already held by"
2506 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2507 cmd->se_tfo->get_fabric_name(),
2508 se_sess->se_node_acl->initiatorname,
2509 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2510 pr_res_holder->pr_reg_nacl->initiatorname);
2512 spin_unlock(&dev->dev_reservation_lock);
2513 core_scsi3_put_pr_reg(pr_reg);
2514 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2518 * From spc4r17 Section 5.7.9: Reserving:
2520 * If a persistent reservation holder attempts to modify the
2521 * type or scope of an existing persistent reservation, the
2522 * command shall be completed with RESERVATION CONFLICT status.
2524 if ((pr_res_holder->pr_res_type != type) ||
2525 (pr_res_holder->pr_res_scope != scope)) {
2526 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2527 pr_err("SPC-3 PR: Attempted RESERVE from"
2528 " [%s]: %s trying to change TYPE and/or SCOPE,"
2529 " while reservation already held by [%s]: %s,"
2530 " returning RESERVATION_CONFLICT\n",
2531 cmd->se_tfo->get_fabric_name(),
2532 se_sess->se_node_acl->initiatorname,
2533 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2534 pr_res_holder->pr_reg_nacl->initiatorname);
2536 spin_unlock(&dev->dev_reservation_lock);
2537 core_scsi3_put_pr_reg(pr_reg);
2538 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2542 * From spc4r17 Section 5.7.9: Reserving:
2544 * If the device server receives a PERSISTENT RESERVE OUT
2545 * command with RESERVE service action where the TYPE field and
2546 * the SCOPE field contain the same values as the existing type
2547 * and scope from a persistent reservation holder, it shall not
2548 * make any change to the existing persistent reservation and
2549 * shall completethe command with GOOD status.
2551 spin_unlock(&dev->dev_reservation_lock);
2552 core_scsi3_put_pr_reg(pr_reg);
2556 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2557 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2559 pr_reg->pr_res_scope = scope;
2560 pr_reg->pr_res_type = type;
2561 pr_reg->pr_res_holder = 1;
2562 dev->dev_pr_res_holder = pr_reg;
2563 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2564 PR_REG_ISID_ID_LEN);
2566 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2567 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2568 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2569 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2570 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2571 cmd->se_tfo->get_fabric_name(),
2572 se_sess->se_node_acl->initiatorname,
2573 (prf_isid) ? &i_buf[0] : "");
2574 spin_unlock(&dev->dev_reservation_lock);
2576 if (pr_tmpl->pr_aptpl_active) {
2577 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2578 &pr_reg->pr_aptpl_buf[0],
2579 pr_tmpl->pr_aptpl_buf_len);
2581 pr_debug("SPC-3 PR: Updated APTPL metadata"
2585 core_scsi3_put_pr_reg(pr_reg);
2589 static int core_scsi3_emulate_pro_reserve(
2595 struct se_device *dev = cmd->se_dev;
2599 case PR_TYPE_WRITE_EXCLUSIVE:
2600 case PR_TYPE_EXCLUSIVE_ACCESS:
2601 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2602 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2603 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2604 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2605 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2608 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2610 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2618 * Called with struct se_device->dev_reservation_lock held.
2620 static void __core_scsi3_complete_pro_release(
2621 struct se_device *dev,
2622 struct se_node_acl *se_nacl,
2623 struct t10_pr_registration *pr_reg,
2626 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2627 char i_buf[PR_REG_ISID_ID_LEN];
2630 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2631 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2632 PR_REG_ISID_ID_LEN);
2634 * Go ahead and release the current PR reservation holder.
2636 dev->dev_pr_res_holder = NULL;
2638 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2639 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2640 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2641 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2642 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2643 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2644 tfo->get_fabric_name(), se_nacl->initiatorname,
2645 (prf_isid) ? &i_buf[0] : "");
2647 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2649 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2652 static int core_scsi3_emulate_pro_release(
2658 struct se_device *dev = cmd->se_dev;
2659 struct se_session *se_sess = cmd->se_sess;
2660 struct se_lun *se_lun = cmd->se_lun;
2661 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2662 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2663 int ret, all_reg = 0;
2665 if (!se_sess || !se_lun) {
2666 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2667 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2671 * Locate the existing *pr_reg via struct se_node_acl pointers
2673 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2675 pr_err("SPC-3 PR: Unable to locate"
2676 " PR_REGISTERED *pr_reg for RELEASE\n");
2677 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2681 * From spc4r17 Section 5.7.11.2 Releasing:
2683 * If there is no persistent reservation or in response to a persistent
2684 * reservation release request from a registered I_T nexus that is not a
2685 * persistent reservation holder (see 5.7.10), the device server shall
2688 * a) Not release the persistent reservation, if any;
2689 * b) Not remove any registrations; and
2690 * c) Complete the command with GOOD status.
2692 spin_lock(&dev->dev_reservation_lock);
2693 pr_res_holder = dev->dev_pr_res_holder;
2694 if (!pr_res_holder) {
2696 * No persistent reservation, return GOOD status.
2698 spin_unlock(&dev->dev_reservation_lock);
2699 core_scsi3_put_pr_reg(pr_reg);
2702 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2703 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2706 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2708 * Non 'All Registrants' PR Type cases..
2709 * Release request from a registered I_T nexus that is not a
2710 * persistent reservation holder. return GOOD status.
2712 spin_unlock(&dev->dev_reservation_lock);
2713 core_scsi3_put_pr_reg(pr_reg);
2717 * From spc4r17 Section 5.7.11.2 Releasing:
2719 * Only the persistent reservation holder (see 5.7.10) is allowed to
2720 * release a persistent reservation.
2722 * An application client releases the persistent reservation by issuing
2723 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2724 * an I_T nexus that is a persistent reservation holder with the
2725 * following parameters:
2727 * a) RESERVATION KEY field set to the value of the reservation key
2728 * that is registered with the logical unit for the I_T nexus;
2730 if (res_key != pr_reg->pr_res_key) {
2731 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2732 " does not match existing SA REGISTER res_key:"
2733 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2734 spin_unlock(&dev->dev_reservation_lock);
2735 core_scsi3_put_pr_reg(pr_reg);
2736 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2740 * From spc4r17 Section 5.7.11.2 Releasing and above:
2742 * b) TYPE field and SCOPE field set to match the persistent
2743 * reservation being released.
2745 if ((pr_res_holder->pr_res_type != type) ||
2746 (pr_res_holder->pr_res_scope != scope)) {
2747 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2748 pr_err("SPC-3 PR RELEASE: Attempted to release"
2749 " reservation from [%s]: %s with different TYPE "
2750 "and/or SCOPE while reservation already held by"
2751 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2752 cmd->se_tfo->get_fabric_name(),
2753 se_sess->se_node_acl->initiatorname,
2754 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2755 pr_res_holder->pr_reg_nacl->initiatorname);
2757 spin_unlock(&dev->dev_reservation_lock);
2758 core_scsi3_put_pr_reg(pr_reg);
2759 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2763 * In response to a persistent reservation release request from the
2764 * persistent reservation holder the device server shall perform a
2765 * release by doing the following as an uninterrupted series of actions:
2766 * a) Release the persistent reservation;
2767 * b) Not remove any registration(s);
2768 * c) If the released persistent reservation is a registrants only type
2769 * or all registrants type persistent reservation,
2770 * the device server shall establish a unit attention condition for
2771 * the initiator port associated with every regis-
2772 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2773 * RESERVE OUT command with RELEASE service action was received,
2774 * with the additional sense code set to RESERVATIONS RELEASED; and
2775 * d) If the persistent reservation is of any other type, the device
2776 * server shall not establish a unit attention condition.
2778 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2781 spin_unlock(&dev->dev_reservation_lock);
2783 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2784 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2785 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2786 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2788 * If no UNIT ATTENTION conditions will be established for
2789 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2790 * go ahead and check for APTPL=1 update+write below
2795 spin_lock(&pr_tmpl->registration_lock);
2796 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2799 * Do not establish a UNIT ATTENTION condition
2800 * for the calling I_T Nexus
2802 if (pr_reg_p == pr_reg)
2805 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2806 pr_reg_p->pr_res_mapped_lun,
2807 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2809 spin_unlock(&pr_tmpl->registration_lock);
2812 if (pr_tmpl->pr_aptpl_active) {
2813 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2814 &pr_reg->pr_aptpl_buf[0],
2815 pr_tmpl->pr_aptpl_buf_len);
2817 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2820 core_scsi3_put_pr_reg(pr_reg);
2824 static int core_scsi3_emulate_pro_clear(
2828 struct se_device *dev = cmd->se_dev;
2829 struct se_node_acl *pr_reg_nacl;
2830 struct se_session *se_sess = cmd->se_sess;
2831 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2832 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2833 u32 pr_res_mapped_lun = 0;
2834 int calling_it_nexus = 0;
2836 * Locate the existing *pr_reg via struct se_node_acl pointers
2838 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2839 se_sess->se_node_acl, se_sess);
2841 pr_err("SPC-3 PR: Unable to locate"
2842 " PR_REGISTERED *pr_reg for CLEAR\n");
2843 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2847 * From spc4r17 section 5.7.11.6, Clearing:
2849 * Any application client may release the persistent reservation and
2850 * remove all registrations from a device server by issuing a
2851 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2852 * registered I_T nexus with the following parameter:
2854 * a) RESERVATION KEY field set to the value of the reservation key
2855 * that is registered with the logical unit for the I_T nexus.
2857 if (res_key != pr_reg_n->pr_res_key) {
2858 pr_err("SPC-3 PR REGISTER: Received"
2859 " res_key: 0x%016Lx does not match"
2860 " existing SA REGISTER res_key:"
2861 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2862 core_scsi3_put_pr_reg(pr_reg_n);
2863 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2867 * a) Release the persistent reservation, if any;
2869 spin_lock(&dev->dev_reservation_lock);
2870 pr_res_holder = dev->dev_pr_res_holder;
2871 if (pr_res_holder) {
2872 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2873 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2876 spin_unlock(&dev->dev_reservation_lock);
2878 * b) Remove all registration(s) (see spc4r17 5.7.7);
2880 spin_lock(&pr_tmpl->registration_lock);
2881 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2882 &pr_tmpl->registration_list, pr_reg_list) {
2884 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2885 pr_reg_nacl = pr_reg->pr_reg_nacl;
2886 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2887 __core_scsi3_free_registration(dev, pr_reg, NULL,
2890 * e) Establish a unit attention condition for the initiator
2891 * port associated with every registered I_T nexus other
2892 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2893 * command with CLEAR service action was received, with the
2894 * additional sense code set to RESERVATIONS PREEMPTED.
2896 if (!calling_it_nexus)
2897 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2898 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2900 spin_unlock(&pr_tmpl->registration_lock);
2902 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2903 cmd->se_tfo->get_fabric_name());
2905 if (pr_tmpl->pr_aptpl_active) {
2906 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2907 pr_debug("SPC-3 PR: Updated APTPL metadata"
2911 core_scsi3_pr_generation(dev);
2916 * Called with struct se_device->dev_reservation_lock held.
2918 static void __core_scsi3_complete_pro_preempt(
2919 struct se_device *dev,
2920 struct t10_pr_registration *pr_reg,
2921 struct list_head *preempt_and_abort_list,
2926 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2927 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2928 char i_buf[PR_REG_ISID_ID_LEN];
2931 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2932 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2933 PR_REG_ISID_ID_LEN);
2935 * Do an implict RELEASE of the existing reservation.
2937 if (dev->dev_pr_res_holder)
2938 __core_scsi3_complete_pro_release(dev, nacl,
2939 dev->dev_pr_res_holder, 0);
2941 dev->dev_pr_res_holder = pr_reg;
2942 pr_reg->pr_res_holder = 1;
2943 pr_reg->pr_res_type = type;
2944 pr_reg->pr_res_scope = scope;
2946 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2947 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2948 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2949 core_scsi3_pr_dump_type(type),
2950 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2951 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2952 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2953 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2955 * For PREEMPT_AND_ABORT, add the preempting reservation's
2956 * struct t10_pr_registration to the list that will be compared
2957 * against received CDBs..
2959 if (preempt_and_abort_list)
2960 list_add_tail(&pr_reg->pr_reg_abort_list,
2961 preempt_and_abort_list);
2964 static void core_scsi3_release_preempt_and_abort(
2965 struct list_head *preempt_and_abort_list,
2966 struct t10_pr_registration *pr_reg_holder)
2968 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2970 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2971 pr_reg_abort_list) {
2973 list_del(&pr_reg->pr_reg_abort_list);
2974 if (pr_reg_holder == pr_reg)
2976 if (pr_reg->pr_res_holder) {
2977 pr_warn("pr_reg->pr_res_holder still set\n");
2981 pr_reg->pr_reg_deve = NULL;
2982 pr_reg->pr_reg_nacl = NULL;
2983 kfree(pr_reg->pr_aptpl_buf);
2984 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2988 int core_scsi3_check_cdb_abort_and_preempt(
2989 struct list_head *preempt_and_abort_list,
2992 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2994 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2995 pr_reg_abort_list) {
2996 if (pr_reg->pr_res_key == cmd->pr_res_key)
3003 static int core_scsi3_pro_preempt(
3011 struct se_device *dev = cmd->se_dev;
3012 struct se_dev_entry *se_deve;
3013 struct se_node_acl *pr_reg_nacl;
3014 struct se_session *se_sess = cmd->se_sess;
3015 struct list_head preempt_and_abort_list;
3016 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
3017 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3018 u32 pr_res_mapped_lun = 0;
3019 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3020 int prh_type = 0, prh_scope = 0, ret;
3023 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3027 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3028 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3031 pr_err("SPC-3 PR: Unable to locate"
3032 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3033 (abort) ? "_AND_ABORT" : "");
3034 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3037 if (pr_reg_n->pr_res_key != res_key) {
3038 core_scsi3_put_pr_reg(pr_reg_n);
3039 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3042 if (scope != PR_SCOPE_LU_SCOPE) {
3043 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3044 core_scsi3_put_pr_reg(pr_reg_n);
3045 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3048 INIT_LIST_HEAD(&preempt_and_abort_list);
3050 spin_lock(&dev->dev_reservation_lock);
3051 pr_res_holder = dev->dev_pr_res_holder;
3052 if (pr_res_holder &&
3053 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3054 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3057 if (!all_reg && !sa_res_key) {
3058 spin_unlock(&dev->dev_reservation_lock);
3059 core_scsi3_put_pr_reg(pr_reg_n);
3060 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3064 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3066 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3067 * persistent reservation holder or there is no persistent reservation
3068 * holder (i.e., there is no persistent reservation), then the device
3069 * server shall perform a preempt by doing the following in an
3070 * uninterrupted series of actions. (See below..)
3072 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3074 * No existing or SA Reservation Key matching reservations..
3076 * PROUT SA PREEMPT with All Registrant type reservations are
3077 * allowed to be processed without a matching SA Reservation Key
3079 spin_lock(&pr_tmpl->registration_lock);
3080 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3081 &pr_tmpl->registration_list, pr_reg_list) {
3083 * Removing of registrations in non all registrants
3084 * type reservations without a matching SA reservation
3087 * a) Remove the registrations for all I_T nexuses
3088 * specified by the SERVICE ACTION RESERVATION KEY
3090 * b) Ignore the contents of the SCOPE and TYPE fields;
3091 * c) Process tasks as defined in 5.7.1; and
3092 * d) Establish a unit attention condition for the
3093 * initiator port associated with every I_T nexus
3094 * that lost its registration other than the I_T
3095 * nexus on which the PERSISTENT RESERVE OUT command
3096 * was received, with the additional sense code set
3097 * to REGISTRATIONS PREEMPTED.
3100 if (pr_reg->pr_res_key != sa_res_key)
3103 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3104 pr_reg_nacl = pr_reg->pr_reg_nacl;
3105 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3106 __core_scsi3_free_registration(dev, pr_reg,
3107 (abort) ? &preempt_and_abort_list :
3108 NULL, calling_it_nexus);
3112 * Case for any existing all registrants type
3113 * reservation, follow logic in spc4r17 section
3114 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3116 * For a ZERO SA Reservation key, release
3117 * all other registrations and do an implict
3118 * release of active persistent reservation.
3120 * For a non-ZERO SA Reservation key, only
3121 * release the matching reservation key from
3125 (pr_reg->pr_res_key != sa_res_key))
3128 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3129 if (calling_it_nexus)
3132 pr_reg_nacl = pr_reg->pr_reg_nacl;
3133 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3134 __core_scsi3_free_registration(dev, pr_reg,
3135 (abort) ? &preempt_and_abort_list :
3139 if (!calling_it_nexus)
3140 core_scsi3_ua_allocate(pr_reg_nacl,
3141 pr_res_mapped_lun, 0x2A,
3142 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3144 spin_unlock(&pr_tmpl->registration_lock);
3146 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3147 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3148 * RESERVATION KEY field to a value that does not match any
3149 * registered reservation key, then the device server shall
3150 * complete the command with RESERVATION CONFLICT status.
3152 if (!released_regs) {
3153 spin_unlock(&dev->dev_reservation_lock);
3154 core_scsi3_put_pr_reg(pr_reg_n);
3155 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3159 * For an existing all registrants type reservation
3160 * with a zero SA rservation key, preempt the existing
3161 * reservation with the new PR type and scope.
3163 if (pr_res_holder && all_reg && !(sa_res_key)) {
3164 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3165 (abort) ? &preempt_and_abort_list : NULL,
3166 type, scope, abort);
3169 core_scsi3_release_preempt_and_abort(
3170 &preempt_and_abort_list, pr_reg_n);
3172 spin_unlock(&dev->dev_reservation_lock);
3174 if (pr_tmpl->pr_aptpl_active) {
3175 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3176 &pr_reg_n->pr_aptpl_buf[0],
3177 pr_tmpl->pr_aptpl_buf_len);
3179 pr_debug("SPC-3 PR: Updated APTPL"
3180 " metadata for PREEMPT%s\n", (abort) ?
3184 core_scsi3_put_pr_reg(pr_reg_n);
3185 core_scsi3_pr_generation(cmd->se_dev);
3189 * The PREEMPTing SA reservation key matches that of the
3190 * existing persistent reservation, first, we check if
3191 * we are preempting our own reservation.
3192 * From spc4r17, section 5.7.11.4.3 Preempting
3193 * persistent reservations and registration handling
3195 * If an all registrants persistent reservation is not
3196 * present, it is not an error for the persistent
3197 * reservation holder to preempt itself (i.e., a
3198 * PERSISTENT RESERVE OUT with a PREEMPT service action
3199 * or a PREEMPT AND ABORT service action with the
3200 * SERVICE ACTION RESERVATION KEY value equal to the
3201 * persistent reservation holder's reservation key that
3202 * is received from the persistent reservation holder).
3203 * In that case, the device server shall establish the
3204 * new persistent reservation and maintain the
3207 prh_type = pr_res_holder->pr_res_type;
3208 prh_scope = pr_res_holder->pr_res_scope;
3210 * If the SERVICE ACTION RESERVATION KEY field identifies a
3211 * persistent reservation holder (see 5.7.10), the device
3212 * server shall perform a preempt by doing the following as
3213 * an uninterrupted series of actions:
3215 * a) Release the persistent reservation for the holder
3216 * identified by the SERVICE ACTION RESERVATION KEY field;
3218 if (pr_reg_n != pr_res_holder)
3219 __core_scsi3_complete_pro_release(dev,
3220 pr_res_holder->pr_reg_nacl,
3221 dev->dev_pr_res_holder, 0);
3223 * b) Remove the registrations for all I_T nexuses identified
3224 * by the SERVICE ACTION RESERVATION KEY field, except the
3225 * I_T nexus that is being used for the PERSISTENT RESERVE
3226 * OUT command. If an all registrants persistent reservation
3227 * is present and the SERVICE ACTION RESERVATION KEY field
3228 * is set to zero, then all registrations shall be removed
3229 * except for that of the I_T nexus that is being used for
3230 * the PERSISTENT RESERVE OUT command;
3232 spin_lock(&pr_tmpl->registration_lock);
3233 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3234 &pr_tmpl->registration_list, pr_reg_list) {
3236 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3237 if (calling_it_nexus)
3240 if (pr_reg->pr_res_key != sa_res_key)
3243 pr_reg_nacl = pr_reg->pr_reg_nacl;
3244 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3245 __core_scsi3_free_registration(dev, pr_reg,
3246 (abort) ? &preempt_and_abort_list : NULL,
3249 * e) Establish a unit attention condition for the initiator
3250 * port associated with every I_T nexus that lost its
3251 * persistent reservation and/or registration, with the
3252 * additional sense code set to REGISTRATIONS PREEMPTED;
3254 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3255 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3257 spin_unlock(&pr_tmpl->registration_lock);
3259 * c) Establish a persistent reservation for the preempting
3260 * I_T nexus using the contents of the SCOPE and TYPE fields;
3262 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3263 (abort) ? &preempt_and_abort_list : NULL,
3264 type, scope, abort);
3266 * d) Process tasks as defined in 5.7.1;
3268 * f) If the type or scope has changed, then for every I_T nexus
3269 * whose reservation key was not removed, except for the I_T
3270 * nexus on which the PERSISTENT RESERVE OUT command was
3271 * received, the device server shall establish a unit
3272 * attention condition for the initiator port associated with
3273 * that I_T nexus, with the additional sense code set to
3274 * RESERVATIONS RELEASED. If the type or scope have not
3275 * changed, then no unit attention condition(s) shall be
3276 * established for this reason.
3278 if ((prh_type != type) || (prh_scope != scope)) {
3279 spin_lock(&pr_tmpl->registration_lock);
3280 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3281 &pr_tmpl->registration_list, pr_reg_list) {
3283 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3284 if (calling_it_nexus)
3287 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3288 pr_reg->pr_res_mapped_lun, 0x2A,
3289 ASCQ_2AH_RESERVATIONS_RELEASED);
3291 spin_unlock(&pr_tmpl->registration_lock);
3293 spin_unlock(&dev->dev_reservation_lock);
3295 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3296 * All received CDBs for the matching existing reservation and
3297 * registrations undergo ABORT_TASK logic.
3299 * From there, core_scsi3_release_preempt_and_abort() will
3300 * release every registration in the list (which have already
3301 * been removed from the primary pr_reg list), except the
3302 * new persistent reservation holder, the calling Initiator Port.
3305 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3306 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3310 if (pr_tmpl->pr_aptpl_active) {
3311 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3312 &pr_reg_n->pr_aptpl_buf[0],
3313 pr_tmpl->pr_aptpl_buf_len);
3315 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3316 "%s\n", (abort) ? "_AND_ABORT" : "");
3319 core_scsi3_put_pr_reg(pr_reg_n);
3320 core_scsi3_pr_generation(cmd->se_dev);
3324 static int core_scsi3_emulate_pro_preempt(
3335 case PR_TYPE_WRITE_EXCLUSIVE:
3336 case PR_TYPE_EXCLUSIVE_ACCESS:
3337 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3338 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3339 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3340 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3341 ret = core_scsi3_pro_preempt(cmd, type, scope,
3342 res_key, sa_res_key, abort);
3345 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3346 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3347 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3355 static int core_scsi3_emulate_pro_register_and_move(
3362 struct se_session *se_sess = cmd->se_sess;
3363 struct se_device *dev = cmd->se_dev;
3364 struct se_dev_entry *se_deve, *dest_se_deve = NULL;
3365 struct se_lun *se_lun = cmd->se_lun;
3366 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3367 struct se_port *se_port;
3368 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3369 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3370 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3371 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3373 unsigned char *initiator_str;
3374 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3375 u32 tid_len, tmp_tid_len;
3376 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3377 unsigned short rtpi;
3378 unsigned char proto_ident;
3380 if (!se_sess || !se_lun) {
3381 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3382 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3385 memset(dest_iport, 0, 64);
3386 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3387 se_tpg = se_sess->se_tpg;
3388 tf_ops = se_tpg->se_tpg_tfo;
3389 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3391 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3392 * Register behaviors for a REGISTER AND MOVE service action
3394 * Locate the existing *pr_reg via struct se_node_acl pointers
3396 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3399 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3400 " *pr_reg for REGISTER_AND_MOVE\n");
3401 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3405 * The provided reservation key much match the existing reservation key
3406 * provided during this initiator's I_T nexus registration.
3408 if (res_key != pr_reg->pr_res_key) {
3409 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3410 " res_key: 0x%016Lx does not match existing SA REGISTER"
3411 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3412 core_scsi3_put_pr_reg(pr_reg);
3413 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3417 * The service active reservation key needs to be non zero
3420 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3422 core_scsi3_put_pr_reg(pr_reg);
3423 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3428 * Determine the Relative Target Port Identifier where the reservation
3429 * will be moved to for the TransportID containing SCSI initiator WWN
3432 buf = transport_kmap_first_data_page(cmd);
3433 rtpi = (buf[18] & 0xff) << 8;
3434 rtpi |= buf[19] & 0xff;
3435 tid_len = (buf[20] & 0xff) << 24;
3436 tid_len |= (buf[21] & 0xff) << 16;
3437 tid_len |= (buf[22] & 0xff) << 8;
3438 tid_len |= buf[23] & 0xff;
3439 transport_kunmap_first_data_page(cmd);
3442 if ((tid_len + 24) != cmd->data_length) {
3443 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3444 " does not equal CDB data_length: %u\n", tid_len,
3446 core_scsi3_put_pr_reg(pr_reg);
3447 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3451 spin_lock(&dev->se_port_lock);
3452 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3453 if (se_port->sep_rtpi != rtpi)
3455 dest_se_tpg = se_port->sep_tpg;
3458 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3462 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3463 smp_mb__after_atomic_inc();
3464 spin_unlock(&dev->se_port_lock);
3466 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3468 pr_err("core_scsi3_tpg_depend_item() failed"
3469 " for dest_se_tpg\n");
3470 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3471 smp_mb__after_atomic_dec();
3472 core_scsi3_put_pr_reg(pr_reg);
3473 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3477 spin_lock(&dev->se_port_lock);
3480 spin_unlock(&dev->se_port_lock);
3482 if (!dest_se_tpg || !dest_tf_ops) {
3483 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3484 " fabric ops from Relative Target Port Identifier:"
3486 core_scsi3_put_pr_reg(pr_reg);
3487 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3491 buf = transport_kmap_first_data_page(cmd);
3492 proto_ident = (buf[24] & 0x0f);
3494 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3495 " 0x%02x\n", proto_ident);
3497 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3498 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3499 " proto_ident: 0x%02x does not match ident: 0x%02x"
3500 " from fabric: %s\n", proto_ident,
3501 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3502 dest_tf_ops->get_fabric_name());
3503 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3507 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3508 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3509 " containg a valid tpg_parse_pr_out_transport_id"
3510 " function pointer\n");
3511 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3515 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3516 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3517 if (!initiator_str) {
3518 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3519 " initiator_str from Transport ID\n");
3520 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3525 transport_kunmap_first_data_page(cmd);
3528 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3529 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3530 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3533 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3534 * action specifies a TransportID that is the same as the initiator port
3535 * of the I_T nexus for the command received, then the command shall
3536 * be terminated with CHECK CONDITION status, with the sense key set to
3537 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3538 * IN PARAMETER LIST.
3540 pr_reg_nacl = pr_reg->pr_reg_nacl;
3541 matching_iname = (!strcmp(initiator_str,
3542 pr_reg_nacl->initiatorname)) ? 1 : 0;
3543 if (!matching_iname)
3544 goto after_iport_check;
3546 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3547 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3548 " matches: %s on received I_T Nexus\n", initiator_str,
3549 pr_reg_nacl->initiatorname);
3550 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3554 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3555 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3556 " matches: %s %s on received I_T Nexus\n",
3557 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3558 pr_reg->pr_reg_isid);
3559 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3565 * Locate the destination struct se_node_acl from the received Transport ID
3567 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3568 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3570 if (dest_node_acl) {
3571 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3572 smp_mb__after_atomic_inc();
3574 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3576 if (!dest_node_acl) {
3577 pr_err("Unable to locate %s dest_node_acl for"
3578 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3580 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3584 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3586 pr_err("core_scsi3_nodeacl_depend_item() for"
3587 " dest_node_acl\n");
3588 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3589 smp_mb__after_atomic_dec();
3590 dest_node_acl = NULL;
3591 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3596 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3597 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3598 dest_node_acl->initiatorname);
3601 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3604 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3605 if (!dest_se_deve) {
3606 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3607 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3608 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3613 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3615 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3616 atomic_dec(&dest_se_deve->pr_ref_count);
3617 smp_mb__after_atomic_dec();
3618 dest_se_deve = NULL;
3619 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3624 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3625 " ACL for dest_se_deve->mapped_lun: %u\n",
3626 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3627 dest_se_deve->mapped_lun);
3630 * A persistent reservation needs to already existing in order to
3631 * successfully complete the REGISTER_AND_MOVE service action..
3633 spin_lock(&dev->dev_reservation_lock);
3634 pr_res_holder = dev->dev_pr_res_holder;
3635 if (!pr_res_holder) {
3636 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3637 " currently held\n");
3638 spin_unlock(&dev->dev_reservation_lock);
3639 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3644 * The received on I_T Nexus must be the reservation holder.
3646 * From spc4r17 section 5.7.8 Table 50 --
3647 * Register behaviors for a REGISTER AND MOVE service action
3649 if (pr_res_holder != pr_reg) {
3650 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3651 " Nexus is not reservation holder\n");
3652 spin_unlock(&dev->dev_reservation_lock);
3653 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3658 * From spc4r17 section 5.7.8: registering and moving reservation
3660 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3661 * action is received and the established persistent reservation is a
3662 * Write Exclusive - All Registrants type or Exclusive Access -
3663 * All Registrants type reservation, then the command shall be completed
3664 * with RESERVATION CONFLICT status.
3666 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3667 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3668 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3669 " reservation for type: %s\n",
3670 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3671 spin_unlock(&dev->dev_reservation_lock);
3672 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3676 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3678 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3680 type = pr_res_holder->pr_res_type;
3681 scope = pr_res_holder->pr_res_type;
3683 * c) Associate the reservation key specified in the SERVICE ACTION
3684 * RESERVATION KEY field with the I_T nexus specified as the
3685 * destination of the register and move, where:
3686 * A) The I_T nexus is specified by the TransportID and the
3687 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3688 * B) Regardless of the TransportID format used, the association for
3689 * the initiator port is based on either the initiator port name
3690 * (see 3.1.71) on SCSI transport protocols where port names are
3691 * required or the initiator port identifier (see 3.1.70) on SCSI
3692 * transport protocols where port names are not required;
3693 * d) Register the reservation key specified in the SERVICE ACTION
3694 * RESERVATION KEY field;
3695 * e) Retain the reservation key specified in the SERVICE ACTION
3696 * RESERVATION KEY field and associated information;
3698 * Also, It is not an error for a REGISTER AND MOVE service action to
3699 * register an I_T nexus that is already registered with the same
3700 * reservation key or a different reservation key.
3702 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3705 ret = core_scsi3_alloc_registration(cmd->se_dev,
3706 dest_node_acl, dest_se_deve, iport_ptr,
3707 sa_res_key, 0, aptpl, 2, 1);
3709 spin_unlock(&dev->dev_reservation_lock);
3710 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3714 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3719 * f) Release the persistent reservation for the persistent reservation
3720 * holder (i.e., the I_T nexus on which the
3722 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3723 dev->dev_pr_res_holder, 0);
3725 * g) Move the persistent reservation to the specified I_T nexus using
3726 * the same scope and type as the persistent reservation released in
3729 dev->dev_pr_res_holder = dest_pr_reg;
3730 dest_pr_reg->pr_res_holder = 1;
3731 dest_pr_reg->pr_res_type = type;
3732 pr_reg->pr_res_scope = scope;
3733 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3734 PR_REG_ISID_ID_LEN);
3736 * Increment PRGeneration for existing registrations..
3739 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3740 spin_unlock(&dev->dev_reservation_lock);
3742 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3743 " created new reservation holder TYPE: %s on object RTPI:"
3744 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3745 core_scsi3_pr_dump_type(type), rtpi,
3746 dest_pr_reg->pr_res_generation);
3747 pr_debug("SPC-3 PR Successfully moved reservation from"
3748 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3749 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3750 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3751 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3754 * It is now safe to release configfs group dependencies for destination
3755 * of Transport ID Initiator Device/Port Identifier
3757 core_scsi3_lunacl_undepend_item(dest_se_deve);
3758 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3759 core_scsi3_tpg_undepend_item(dest_se_tpg);
3761 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3762 * nexus on which PERSISTENT RESERVE OUT command was received.
3765 spin_lock(&pr_tmpl->registration_lock);
3766 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3767 spin_unlock(&pr_tmpl->registration_lock);
3769 core_scsi3_put_pr_reg(pr_reg);
3772 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3773 * write out the updated metadata to struct file for this SCSI device.
3776 pr_tmpl->pr_aptpl_active = 0;
3777 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3778 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3779 " REGISTER_AND_MOVE\n");
3781 pr_tmpl->pr_aptpl_active = 1;
3782 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3783 &dest_pr_reg->pr_aptpl_buf[0],
3784 pr_tmpl->pr_aptpl_buf_len);
3786 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3787 " REGISTER_AND_MOVE\n");
3790 transport_kunmap_first_data_page(cmd);
3792 core_scsi3_put_pr_reg(dest_pr_reg);
3796 transport_kunmap_first_data_page(cmd);
3798 core_scsi3_lunacl_undepend_item(dest_se_deve);
3800 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3801 core_scsi3_tpg_undepend_item(dest_se_tpg);
3802 core_scsi3_put_pr_reg(pr_reg);
3806 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3808 unsigned int __v1, __v2;
3810 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3811 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3813 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3817 * See spc4r17 section 6.14 Table 170
3819 int target_scsi3_emulate_pr_out(struct se_task *task)
3821 struct se_cmd *cmd = task->task_se_cmd;
3822 unsigned char *cdb = &cmd->t_task_cdb[0];
3824 u64 res_key, sa_res_key;
3825 int sa, scope, type, aptpl;
3826 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3830 * Following spc2r20 5.5.1 Reservations overview:
3832 * If a logical unit has been reserved by any RESERVE command and is
3833 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3834 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3835 * initiator or service action and shall terminate with a RESERVATION
3838 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3839 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3840 " SPC-2 reservation is held, returning"
3841 " RESERVATION_CONFLICT\n");
3842 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3848 * FIXME: A NULL struct se_session pointer means an this is not coming from
3849 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3851 if (!cmd->se_sess) {
3852 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3856 if (cmd->data_length < 24) {
3857 pr_warn("SPC-PR: Received PR OUT parameter list"
3858 " length too small: %u\n", cmd->data_length);
3859 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3864 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3866 sa = (cdb[1] & 0x1f);
3867 scope = (cdb[2] & 0xf0);
3868 type = (cdb[2] & 0x0f);
3870 buf = transport_kmap_first_data_page(cmd);
3872 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3874 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3875 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3877 * REGISTER_AND_MOVE uses a different SA parameter list containing
3878 * SCSI TransportIDs.
3880 if (sa != PRO_REGISTER_AND_MOVE) {
3881 spec_i_pt = (buf[20] & 0x08);
3882 all_tg_pt = (buf[20] & 0x04);
3883 aptpl = (buf[20] & 0x01);
3885 aptpl = (buf[17] & 0x01);
3886 unreg = (buf[17] & 0x02);
3888 transport_kunmap_first_data_page(cmd);
3892 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3894 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3895 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3901 * From spc4r17 section 6.14:
3903 * If the SPEC_I_PT bit is set to zero, the service action is not
3904 * REGISTER AND MOVE, and the parameter list length is not 24, then
3905 * the command shall be terminated with CHECK CONDITION status, with
3906 * the sense key set to ILLEGAL REQUEST, and the additional sense
3907 * code set to PARAMETER LIST LENGTH ERROR.
3909 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3910 (cmd->data_length != 24)) {
3911 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3912 " list length: %u\n", cmd->data_length);
3913 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3918 * (core_scsi3_emulate_pro_* function parameters
3919 * are defined by spc4r17 Table 174:
3920 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3924 ret = core_scsi3_emulate_pro_register(cmd,
3925 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3928 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3931 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3934 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3937 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3938 res_key, sa_res_key, 0);
3940 case PRO_PREEMPT_AND_ABORT:
3941 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3942 res_key, sa_res_key, 1);
3944 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3945 ret = core_scsi3_emulate_pro_register(cmd,
3946 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3948 case PRO_REGISTER_AND_MOVE:
3949 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3950 sa_res_key, aptpl, unreg);
3953 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3954 " action: 0x%02x\n", cdb[1] & 0x1f);
3955 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3962 task->task_scsi_status = GOOD;
3963 transport_complete_task(task, 1);
3969 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3971 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3973 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3975 struct se_device *se_dev = cmd->se_dev;
3976 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
3977 struct t10_pr_registration *pr_reg;
3979 u32 add_len = 0, off = 8;
3981 if (cmd->data_length < 8) {
3982 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3983 " too small\n", cmd->data_length);
3984 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3988 buf = transport_kmap_first_data_page(cmd);
3989 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3990 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3991 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3992 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
3994 spin_lock(&su_dev->t10_pr.registration_lock);
3995 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
3998 * Check for overflow of 8byte PRI READ_KEYS payload and
3999 * next reservation key list descriptor.
4001 if ((add_len + 8) > (cmd->data_length - 8))
4004 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4005 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4006 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4007 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4008 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4009 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4010 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4011 buf[off++] = (pr_reg->pr_res_key & 0xff);
4015 spin_unlock(&su_dev->t10_pr.registration_lock);
4017 buf[4] = ((add_len >> 24) & 0xff);
4018 buf[5] = ((add_len >> 16) & 0xff);
4019 buf[6] = ((add_len >> 8) & 0xff);
4020 buf[7] = (add_len & 0xff);
4022 transport_kunmap_first_data_page(cmd);
4028 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4030 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4032 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4034 struct se_device *se_dev = cmd->se_dev;
4035 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4036 struct t10_pr_registration *pr_reg;
4039 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4041 if (cmd->data_length < 8) {
4042 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4043 " too small\n", cmd->data_length);
4044 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4048 buf = transport_kmap_first_data_page(cmd);
4049 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4050 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4051 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4052 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4054 spin_lock(&se_dev->dev_reservation_lock);
4055 pr_reg = se_dev->dev_pr_res_holder;
4058 * Set the hardcoded Additional Length
4060 buf[4] = ((add_len >> 24) & 0xff);
4061 buf[5] = ((add_len >> 16) & 0xff);
4062 buf[6] = ((add_len >> 8) & 0xff);
4063 buf[7] = (add_len & 0xff);
4065 if (cmd->data_length < 22)
4069 * Set the Reservation key.
4071 * From spc4r17, section 5.7.10:
4072 * A persistent reservation holder has its reservation key
4073 * returned in the parameter data from a PERSISTENT
4074 * RESERVE IN command with READ RESERVATION service action as
4076 * a) For a persistent reservation of the type Write Exclusive
4077 * - All Registrants or Exclusive Access  All Regitrants,
4078 * the reservation key shall be set to zero; or
4079 * b) For all other persistent reservation types, the
4080 * reservation key shall be set to the registered
4081 * reservation key for the I_T nexus that holds the
4082 * persistent reservation.
4084 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4085 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4088 pr_res_key = pr_reg->pr_res_key;
4090 buf[8] = ((pr_res_key >> 56) & 0xff);
4091 buf[9] = ((pr_res_key >> 48) & 0xff);
4092 buf[10] = ((pr_res_key >> 40) & 0xff);
4093 buf[11] = ((pr_res_key >> 32) & 0xff);
4094 buf[12] = ((pr_res_key >> 24) & 0xff);
4095 buf[13] = ((pr_res_key >> 16) & 0xff);
4096 buf[14] = ((pr_res_key >> 8) & 0xff);
4097 buf[15] = (pr_res_key & 0xff);
4099 * Set the SCOPE and TYPE
4101 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4102 (pr_reg->pr_res_type & 0x0f);
4106 spin_unlock(&se_dev->dev_reservation_lock);
4107 transport_kunmap_first_data_page(cmd);
4113 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4115 * See spc4r17 section 6.13.4 Table 165
4117 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4119 struct se_device *dev = cmd->se_dev;
4120 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
4122 u16 add_len = 8; /* Hardcoded to 8. */
4124 if (cmd->data_length < 6) {
4125 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4126 " %u too small\n", cmd->data_length);
4127 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4131 buf = transport_kmap_first_data_page(cmd);
4133 buf[0] = ((add_len << 8) & 0xff);
4134 buf[1] = (add_len & 0xff);
4135 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4136 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4137 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4138 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4140 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4141 * set the TMV: Task Mask Valid bit.
4145 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4147 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4149 * PTPL_A: Persistence across Target Power Loss Active bit
4151 if (pr_tmpl->pr_aptpl_active)
4154 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4156 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4157 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4158 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4159 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4160 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4161 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4163 transport_kunmap_first_data_page(cmd);
4169 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4171 * See spc4r17 section 6.13.5 Table 168 and 169
4173 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4175 struct se_device *se_dev = cmd->se_dev;
4176 struct se_node_acl *se_nacl;
4177 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4178 struct se_portal_group *se_tpg;
4179 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4180 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
4182 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4183 u32 off = 8; /* off into first Full Status descriptor */
4184 int format_code = 0;
4186 if (cmd->data_length < 8) {
4187 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4188 " too small\n", cmd->data_length);
4189 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4193 buf = transport_kmap_first_data_page(cmd);
4195 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4196 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4197 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4198 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4200 spin_lock(&pr_tmpl->registration_lock);
4201 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4202 &pr_tmpl->registration_list, pr_reg_list) {
4204 se_nacl = pr_reg->pr_reg_nacl;
4205 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4208 atomic_inc(&pr_reg->pr_res_holders);
4209 smp_mb__after_atomic_inc();
4210 spin_unlock(&pr_tmpl->registration_lock);
4212 * Determine expected length of $FABRIC_MOD specific
4213 * TransportID full status descriptor..
4215 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4216 se_tpg, se_nacl, pr_reg, &format_code);
4218 if ((exp_desc_len + add_len) > cmd->data_length) {
4219 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4220 " out of buffer: %d\n", cmd->data_length);
4221 spin_lock(&pr_tmpl->registration_lock);
4222 atomic_dec(&pr_reg->pr_res_holders);
4223 smp_mb__after_atomic_dec();
4227 * Set RESERVATION KEY
4229 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4230 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4231 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4232 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4233 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4234 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4235 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4236 buf[off++] = (pr_reg->pr_res_key & 0xff);
4237 off += 4; /* Skip Over Reserved area */
4240 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4242 if (pr_reg->pr_reg_all_tg_pt)
4245 * The struct se_lun pointer will be present for the
4246 * reservation holder for PR_HOLDER bit.
4248 * Also, if this registration is the reservation
4249 * holder, fill in SCOPE and TYPE in the next byte.
4251 if (pr_reg->pr_res_holder) {
4253 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4254 (pr_reg->pr_res_type & 0x0f);
4258 off += 4; /* Skip over reserved area */
4260 * From spc4r17 6.3.15:
4262 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4263 * IDENTIFIER field contains the relative port identifier (see
4264 * 3.1.120) of the target port that is part of the I_T nexus
4265 * described by this full status descriptor. If the ALL_TG_PT
4266 * bit is set to one, the contents of the RELATIVE TARGET PORT
4267 * IDENTIFIER field are not defined by this standard.
4269 if (!pr_reg->pr_reg_all_tg_pt) {
4270 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4272 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4273 buf[off++] = (port->sep_rtpi & 0xff);
4275 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4278 * Now, have the $FABRIC_MOD fill in the protocol identifier
4280 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4281 se_nacl, pr_reg, &format_code, &buf[off+4]);
4283 spin_lock(&pr_tmpl->registration_lock);
4284 atomic_dec(&pr_reg->pr_res_holders);
4285 smp_mb__after_atomic_dec();
4287 * Set the ADDITIONAL DESCRIPTOR LENGTH
4289 buf[off++] = ((desc_len >> 24) & 0xff);
4290 buf[off++] = ((desc_len >> 16) & 0xff);
4291 buf[off++] = ((desc_len >> 8) & 0xff);
4292 buf[off++] = (desc_len & 0xff);
4294 * Size of full desctipor header minus TransportID
4295 * containing $FABRIC_MOD specific) initiator device/port
4298 * See spc4r17 Section 6.13.5 Table 169
4300 add_desc_len = (24 + desc_len);
4303 add_len += add_desc_len;
4305 spin_unlock(&pr_tmpl->registration_lock);
4307 * Set ADDITIONAL_LENGTH
4309 buf[4] = ((add_len >> 24) & 0xff);
4310 buf[5] = ((add_len >> 16) & 0xff);
4311 buf[6] = ((add_len >> 8) & 0xff);
4312 buf[7] = (add_len & 0xff);
4314 transport_kunmap_first_data_page(cmd);
4319 int target_scsi3_emulate_pr_in(struct se_task *task)
4321 struct se_cmd *cmd = task->task_se_cmd;
4325 * Following spc2r20 5.5.1 Reservations overview:
4327 * If a logical unit has been reserved by any RESERVE command and is
4328 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4329 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4330 * initiator or service action and shall terminate with a RESERVATION
4333 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4334 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4335 " SPC-2 reservation is held, returning"
4336 " RESERVATION_CONFLICT\n");
4337 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4341 switch (cmd->t_task_cdb[1] & 0x1f) {
4343 ret = core_scsi3_pri_read_keys(cmd);
4345 case PRI_READ_RESERVATION:
4346 ret = core_scsi3_pri_read_reservation(cmd);
4348 case PRI_REPORT_CAPABILITIES:
4349 ret = core_scsi3_pri_report_capabilities(cmd);
4351 case PRI_READ_FULL_STATUS:
4352 ret = core_scsi3_pri_read_full_status(cmd);
4355 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4356 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4357 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4363 task->task_scsi_status = GOOD;
4364 transport_complete_task(task, 1);
4369 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4374 static int core_pt_seq_non_holder(
4382 int core_setup_reservations(struct se_device *dev, int force_pt)
4384 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
4385 struct t10_reservation *rest = &su_dev->t10_pr;
4387 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4388 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4389 * cause a problem because libata and some SATA RAID HBAs appear
4390 * under Linux/SCSI, but to emulate reservations themselves.
4392 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4393 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
4394 rest->res_type = SPC_PASSTHROUGH;
4395 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4396 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4397 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4398 " emulation\n", dev->transport->name);
4402 * If SPC-3 or above is reported by real or emulated struct se_device,
4403 * use emulated Persistent Reservations.
4405 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4406 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4407 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4408 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4409 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4410 " emulation\n", dev->transport->name);
4412 rest->res_type = SPC2_RESERVATIONS;
4413 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4414 rest->pr_ops.t10_seq_non_holder =
4415 &core_scsi2_reservation_seq_non_holder;
4416 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4417 dev->transport->name);