1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@kernel.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ******************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_pr.h"
42 #include "target_core_ua.h"
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 struct pr_transport_id_holder {
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
56 int core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
61 if (!pr_reg->isid_present_at_reg)
64 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
68 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69 struct t10_pr_registration *, int);
71 static int core_scsi2_reservation_seq_non_holder(
88 static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
90 struct se_device *dev = cmd->se_dev;
91 struct se_session *sess = cmd->se_sess;
97 spin_lock(&dev->dev_reservation_lock);
98 if (!dev->dev_reserved_node_acl || !sess) {
99 spin_unlock(&dev->dev_reservation_lock);
102 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103 spin_unlock(&dev->dev_reservation_lock);
106 if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
107 spin_unlock(&dev->dev_reservation_lock);
110 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
111 spin_unlock(&dev->dev_reservation_lock);
116 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117 struct se_node_acl *, struct se_session *);
118 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
120 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
122 struct se_session *se_sess = cmd->se_sess;
123 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
124 struct t10_pr_registration *pr_reg;
125 struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
126 int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
132 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
156 if (pr_reg->pr_res_holder) {
157 core_scsi3_put_pr_reg(pr_reg);
160 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
161 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
162 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
163 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
164 core_scsi3_put_pr_reg(pr_reg);
167 core_scsi3_put_pr_reg(pr_reg);
171 * Following spc2r20 5.5.1 Reservations overview:
173 * If a logical unit has executed a PERSISTENT RESERVE OUT
174 * command with the REGISTER or the REGISTER AND IGNORE
175 * EXISTING KEY service action and is still registered by any
176 * initiator, all RESERVE commands and all RELEASE commands
177 * regardless of initiator shall conflict and shall terminate
178 * with a RESERVATION CONFLICT status.
180 spin_lock(&pr_tmpl->registration_lock);
181 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
182 spin_unlock(&pr_tmpl->registration_lock);
186 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
187 " while active SPC-3 registrations exist,"
188 " returning RESERVATION_CONFLICT\n");
189 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
196 int target_scsi2_reservation_release(struct se_task *task)
198 struct se_cmd *cmd = task->task_se_cmd;
199 struct se_device *dev = cmd->se_dev;
200 struct se_session *sess = cmd->se_sess;
201 struct se_portal_group *tpg = sess->se_tpg;
206 rc = target_check_scsi2_reservation_conflict(cmd);
210 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
216 spin_lock(&dev->dev_reservation_lock);
217 if (!dev->dev_reserved_node_acl || !sess)
220 if (dev->dev_reserved_node_acl != sess->se_node_acl)
223 dev->dev_reserved_node_acl = NULL;
224 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
225 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
226 dev->dev_res_bin_isid = 0;
227 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
229 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
230 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
231 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
232 sess->se_node_acl->initiatorname);
235 spin_unlock(&dev->dev_reservation_lock);
238 task->task_scsi_status = GOOD;
239 transport_complete_task(task, 1);
244 int target_scsi2_reservation_reserve(struct se_task *task)
246 struct se_cmd *cmd = task->task_se_cmd;
247 struct se_device *dev = cmd->se_dev;
248 struct se_session *sess = cmd->se_sess;
249 struct se_portal_group *tpg = sess->se_tpg;
252 if ((cmd->t_task_cdb[1] & 0x01) &&
253 (cmd->t_task_cdb[1] & 0x02)) {
254 pr_err("LongIO and Obselete Bits set, returning"
255 " ILLEGAL_REQUEST\n");
256 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
261 * This is currently the case for target_core_mod passthrough struct se_cmd
266 rc = target_check_scsi2_reservation_conflict(cmd);
270 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
276 spin_lock(&dev->dev_reservation_lock);
277 if (dev->dev_reserved_node_acl &&
278 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
279 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
280 tpg->se_tpg_tfo->get_fabric_name());
281 pr_err("Original reserver LUN: %u %s\n",
282 cmd->se_lun->unpacked_lun,
283 dev->dev_reserved_node_acl->initiatorname);
284 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
285 " from %s \n", cmd->se_lun->unpacked_lun,
286 cmd->se_deve->mapped_lun,
287 sess->se_node_acl->initiatorname);
288 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
293 dev->dev_reserved_node_acl = sess->se_node_acl;
294 dev->dev_flags |= DF_SPC2_RESERVATIONS;
295 if (sess->sess_bin_isid != 0) {
296 dev->dev_res_bin_isid = sess->sess_bin_isid;
297 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
299 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
300 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
301 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
302 sess->se_node_acl->initiatorname);
305 spin_unlock(&dev->dev_reservation_lock);
308 task->task_scsi_status = GOOD;
309 transport_complete_task(task, 1);
316 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
318 * This function is called by those initiator ports who are *NOT*
319 * the active PR reservation holder when a reservation is present.
321 static int core_scsi3_pr_seq_non_holder(
326 struct se_dev_entry *se_deve;
327 struct se_session *se_sess = cmd->se_sess;
328 int other_cdb = 0, ignore_reg;
329 int registered_nexus = 0, ret = 1; /* Conflict by default */
330 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
331 int we = 0; /* Write Exclusive */
332 int legacy = 0; /* Act like a legacy device and return
333 * RESERVATION CONFLICT on some CDBs */
335 * A legacy SPC-2 reservation is being held.
337 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
338 return core_scsi2_reservation_seq_non_holder(cmd,
341 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
343 * Determine if the registration should be ignored due to
344 * non-matching ISIDs in core_scsi3_pr_reservation_check().
346 ignore_reg = (pr_reg_type & 0x80000000);
348 pr_reg_type &= ~0x80000000;
350 switch (pr_reg_type) {
351 case PR_TYPE_WRITE_EXCLUSIVE:
353 case PR_TYPE_EXCLUSIVE_ACCESS:
355 * Some commands are only allowed for the persistent reservation
358 if ((se_deve->def_pr_registered) && !(ignore_reg))
359 registered_nexus = 1;
361 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
363 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
365 * Some commands are only allowed for registered I_T Nexuses.
368 if ((se_deve->def_pr_registered) && !(ignore_reg))
369 registered_nexus = 1;
371 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
373 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
375 * Each registered I_T Nexus is a reservation holder.
378 if ((se_deve->def_pr_registered) && !(ignore_reg))
379 registered_nexus = 1;
385 * Referenced from spc4r17 table 45 for *NON* PR holder access
388 case SECURITY_PROTOCOL_IN:
389 if (registered_nexus)
397 case RECEIVE_DIAGNOSTIC:
402 if (registered_nexus) {
406 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
408 case PERSISTENT_RESERVE_OUT:
410 * This follows PERSISTENT_RESERVE_OUT service actions that
411 * are allowed in the presence of various reservations.
412 * See spc4r17, table 46
414 switch (cdb[1] & 0x1f) {
417 case PRO_PREEMPT_AND_ABORT:
418 ret = (registered_nexus) ? 0 : 1;
421 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
424 case PRO_REGISTER_AND_MOVE:
429 ret = (registered_nexus) ? 0 : 1;
432 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
433 " action: 0x%02x\n", cdb[1] & 0x1f);
439 /* Handled by CRH=1 in target_scsi2_reservation_release() */
444 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
447 case TEST_UNIT_READY:
448 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
451 switch (cdb[1] & 0x1f) {
452 case MI_MANAGEMENT_PROTOCOL_IN:
453 if (registered_nexus) {
457 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
459 case MI_REPORT_SUPPORTED_OPERATION_CODES:
460 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
465 if (registered_nexus) {
469 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
471 case MI_REPORT_ALIASES:
472 case MI_REPORT_IDENTIFYING_INFORMATION:
473 case MI_REPORT_PRIORITY:
474 case MI_REPORT_TARGET_PGS:
475 case MI_REPORT_TIMESTAMP:
476 ret = 0; /* Allowed */
479 pr_err("Unknown MI Service Action: 0x%02x\n",
484 case ACCESS_CONTROL_IN:
485 case ACCESS_CONTROL_OUT:
488 case READ_MEDIA_SERIAL_NUMBER:
491 case PERSISTENT_RESERVE_IN:
492 ret = 0; /*/ Allowed CDBs */
499 * Case where the CDB is explicitly allowed in the above switch
502 if (!ret && !other_cdb) {
503 pr_debug("Allowing explict CDB: 0x%02x for %s"
504 " reservation holder\n", cdb[0],
505 core_scsi3_pr_dump_type(pr_reg_type));
510 * Check if write exclusive initiator ports *NOT* holding the
511 * WRITE_EXCLUSIVE_* reservation.
513 if ((we) && !(registered_nexus)) {
514 if (cmd->data_direction == DMA_TO_DEVICE) {
516 * Conflict for write exclusive
518 pr_debug("%s Conflict for unregistered nexus"
519 " %s CDB: 0x%02x to %s reservation\n",
520 transport_dump_cmd_direction(cmd),
521 se_sess->se_node_acl->initiatorname, cdb[0],
522 core_scsi3_pr_dump_type(pr_reg_type));
526 * Allow non WRITE CDBs for all Write Exclusive
527 * PR TYPEs to pass for registered and
528 * non-registered_nexuxes NOT holding the reservation.
530 * We only make noise for the unregisterd nexuses,
531 * as we expect registered non-reservation holding
532 * nexuses to issue CDBs.
535 if (!registered_nexus) {
536 pr_debug("Allowing implict CDB: 0x%02x"
537 " for %s reservation on unregistered"
539 core_scsi3_pr_dump_type(pr_reg_type));
544 } else if ((reg_only) || (all_reg)) {
545 if (registered_nexus) {
547 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
548 * allow commands from registered nexuses.
551 pr_debug("Allowing implict CDB: 0x%02x for %s"
552 " reservation\n", cdb[0],
553 core_scsi3_pr_dump_type(pr_reg_type));
558 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
559 " for %s reservation\n", transport_dump_cmd_direction(cmd),
560 (registered_nexus) ? "" : "un",
561 se_sess->se_node_acl->initiatorname, cdb[0],
562 core_scsi3_pr_dump_type(pr_reg_type));
564 return 1; /* Conflict by default */
567 static u32 core_scsi3_pr_generation(struct se_device *dev)
569 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
572 * PRGeneration field shall contain the value of a 32-bit wrapping
573 * counter mainted by the device server.
575 * Note that this is done regardless of Active Persist across
576 * Target PowerLoss (APTPL)
578 * See spc4r17 section 6.3.12 READ_KEYS service action
580 spin_lock(&dev->dev_reservation_lock);
581 prg = su_dev->t10_pr.pr_generation++;
582 spin_unlock(&dev->dev_reservation_lock);
587 static int core_scsi3_pr_reservation_check(
591 struct se_device *dev = cmd->se_dev;
592 struct se_session *sess = cmd->se_sess;
598 * A legacy SPC-2 reservation is being held.
600 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
601 return core_scsi2_reservation_check(cmd, pr_reg_type);
603 spin_lock(&dev->dev_reservation_lock);
604 if (!dev->dev_pr_res_holder) {
605 spin_unlock(&dev->dev_reservation_lock);
608 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
609 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
610 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
611 spin_unlock(&dev->dev_reservation_lock);
614 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
615 spin_unlock(&dev->dev_reservation_lock);
618 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
619 sess->sess_bin_isid) ? 0 : -EINVAL;
621 * Use bit in *pr_reg_type to notify ISID mismatch in
622 * core_scsi3_pr_seq_non_holder().
625 *pr_reg_type |= 0x80000000;
626 spin_unlock(&dev->dev_reservation_lock);
631 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
632 struct se_device *dev,
633 struct se_node_acl *nacl,
634 struct se_dev_entry *deve,
640 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
641 struct t10_pr_registration *pr_reg;
643 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
645 pr_err("Unable to allocate struct t10_pr_registration\n");
649 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
651 if (!pr_reg->pr_aptpl_buf) {
652 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
653 kmem_cache_free(t10_pr_reg_cache, pr_reg);
657 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
658 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
659 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
660 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
661 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
662 atomic_set(&pr_reg->pr_res_holders, 0);
663 pr_reg->pr_reg_nacl = nacl;
664 pr_reg->pr_reg_deve = deve;
665 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
666 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
667 pr_reg->pr_res_key = sa_res_key;
668 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
669 pr_reg->pr_reg_aptpl = aptpl;
670 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
672 * If an ISID value for this SCSI Initiator Port exists,
673 * save it to the registration now.
676 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
677 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
678 pr_reg->isid_present_at_reg = 1;
684 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
685 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
688 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
691 static struct t10_pr_registration *__core_scsi3_alloc_registration(
692 struct se_device *dev,
693 struct se_node_acl *nacl,
694 struct se_dev_entry *deve,
700 struct se_dev_entry *deve_tmp;
701 struct se_node_acl *nacl_tmp;
702 struct se_port *port, *port_tmp;
703 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
704 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
707 * Create a registration for the I_T Nexus upon which the
708 * PROUT REGISTER was received.
710 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
711 sa_res_key, all_tg_pt, aptpl);
715 * Return pointer to pr_reg for ALL_TG_PT=0
720 * Create list of matching SCSI Initiator Port registrations
723 spin_lock(&dev->se_port_lock);
724 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
725 atomic_inc(&port->sep_tg_pt_ref_cnt);
726 smp_mb__after_atomic_inc();
727 spin_unlock(&dev->se_port_lock);
729 spin_lock_bh(&port->sep_alua_lock);
730 list_for_each_entry(deve_tmp, &port->sep_alua_list,
733 * This pointer will be NULL for demo mode MappedLUNs
734 * that have not been make explict via a ConfigFS
735 * MappedLUN group for the SCSI Initiator Node ACL.
737 if (!deve_tmp->se_lun_acl)
740 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
742 * Skip the matching struct se_node_acl that is allocated
745 if (nacl == nacl_tmp)
748 * Only perform PR registrations for target ports on
749 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
752 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
755 * Look for a matching Initiator Node ACL in ASCII format
757 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
760 atomic_inc(&deve_tmp->pr_ref_count);
761 smp_mb__after_atomic_inc();
762 spin_unlock_bh(&port->sep_alua_lock);
764 * Grab a configfs group dependency that is released
765 * for the exception path at label out: below, or upon
766 * completion of adding ALL_TG_PT=1 registrations in
767 * __core_scsi3_add_registration()
769 ret = core_scsi3_lunacl_depend_item(deve_tmp);
771 pr_err("core_scsi3_lunacl_depend"
773 atomic_dec(&port->sep_tg_pt_ref_cnt);
774 smp_mb__after_atomic_dec();
775 atomic_dec(&deve_tmp->pr_ref_count);
776 smp_mb__after_atomic_dec();
780 * Located a matching SCSI Initiator Port on a different
781 * port, allocate the pr_reg_atp and attach it to the
782 * pr_reg->pr_reg_atp_list that will be processed once
783 * the original *pr_reg is processed in
784 * __core_scsi3_add_registration()
786 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
787 nacl_tmp, deve_tmp, NULL,
788 sa_res_key, all_tg_pt, aptpl);
790 atomic_dec(&port->sep_tg_pt_ref_cnt);
791 smp_mb__after_atomic_dec();
792 atomic_dec(&deve_tmp->pr_ref_count);
793 smp_mb__after_atomic_dec();
794 core_scsi3_lunacl_undepend_item(deve_tmp);
798 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
799 &pr_reg->pr_reg_atp_list);
800 spin_lock_bh(&port->sep_alua_lock);
802 spin_unlock_bh(&port->sep_alua_lock);
804 spin_lock(&dev->se_port_lock);
805 atomic_dec(&port->sep_tg_pt_ref_cnt);
806 smp_mb__after_atomic_dec();
808 spin_unlock(&dev->se_port_lock);
812 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
813 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
814 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
815 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
816 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
818 kmem_cache_free(t10_pr_reg_cache, pr_reg);
822 int core_scsi3_alloc_aptpl_registration(
823 struct t10_reservation *pr_tmpl,
825 unsigned char *i_port,
828 unsigned char *t_port,
835 struct t10_pr_registration *pr_reg;
837 if (!i_port || !t_port || !sa_res_key) {
838 pr_err("Illegal parameters for APTPL registration\n");
842 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
844 pr_err("Unable to allocate struct t10_pr_registration\n");
847 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
849 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
850 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
851 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
852 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
853 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
854 atomic_set(&pr_reg->pr_res_holders, 0);
855 pr_reg->pr_reg_nacl = NULL;
856 pr_reg->pr_reg_deve = NULL;
857 pr_reg->pr_res_mapped_lun = mapped_lun;
858 pr_reg->pr_aptpl_target_lun = target_lun;
859 pr_reg->pr_res_key = sa_res_key;
860 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
861 pr_reg->pr_reg_aptpl = 1;
862 pr_reg->pr_reg_tg_pt_lun = NULL;
863 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
864 pr_reg->pr_res_type = type;
866 * If an ISID value had been saved in APTPL metadata for this
867 * SCSI Initiator Port, restore it now.
870 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
871 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
872 pr_reg->isid_present_at_reg = 1;
875 * Copy the i_port and t_port information from caller.
877 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
878 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
879 pr_reg->pr_reg_tpgt = tpgt;
881 * Set pr_res_holder from caller, the pr_reg who is the reservation
882 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
883 * the Initiator Node LUN ACL from the fabric module is created for
886 pr_reg->pr_res_holder = res_holder;
888 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
889 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
890 " metadata\n", (res_holder) ? "+reservation" : "");
894 static void core_scsi3_aptpl_reserve(
895 struct se_device *dev,
896 struct se_portal_group *tpg,
897 struct se_node_acl *node_acl,
898 struct t10_pr_registration *pr_reg)
900 char i_buf[PR_REG_ISID_ID_LEN];
903 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
904 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
907 spin_lock(&dev->dev_reservation_lock);
908 dev->dev_pr_res_holder = pr_reg;
909 spin_unlock(&dev->dev_reservation_lock);
911 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
912 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
913 tpg->se_tpg_tfo->get_fabric_name(),
914 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
915 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
916 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
917 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
918 (prf_isid) ? &i_buf[0] : "");
921 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
922 struct t10_pr_registration *, int, int);
924 static int __core_scsi3_check_aptpl_registration(
925 struct se_device *dev,
926 struct se_portal_group *tpg,
929 struct se_node_acl *nacl,
930 struct se_dev_entry *deve)
932 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
933 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
934 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
935 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
938 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
939 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
941 * Copy Initiator Port information from struct se_node_acl
943 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
944 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
945 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
946 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
948 * Look for the matching registrations+reservation from those
949 * created from APTPL metadata. Note that multiple registrations
950 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
953 spin_lock(&pr_tmpl->aptpl_reg_lock);
954 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
956 if (!strcmp(pr_reg->pr_iport, i_port) &&
957 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
958 !(strcmp(pr_reg->pr_tport, t_port)) &&
959 (pr_reg->pr_reg_tpgt == tpgt) &&
960 (pr_reg->pr_aptpl_target_lun == target_lun)) {
962 pr_reg->pr_reg_nacl = nacl;
963 pr_reg->pr_reg_deve = deve;
964 pr_reg->pr_reg_tg_pt_lun = lun;
966 list_del(&pr_reg->pr_reg_aptpl_list);
967 spin_unlock(&pr_tmpl->aptpl_reg_lock);
969 * At this point all of the pointers in *pr_reg will
970 * be setup, so go ahead and add the registration.
973 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
975 * If this registration is the reservation holder,
976 * make that happen now..
978 if (pr_reg->pr_res_holder)
979 core_scsi3_aptpl_reserve(dev, tpg,
982 * Reenable pr_aptpl_active to accept new metadata
983 * updates once the SCSI device is active again..
985 spin_lock(&pr_tmpl->aptpl_reg_lock);
986 pr_tmpl->pr_aptpl_active = 1;
989 spin_unlock(&pr_tmpl->aptpl_reg_lock);
994 int core_scsi3_check_aptpl_registration(
995 struct se_device *dev,
996 struct se_portal_group *tpg,
998 struct se_lun_acl *lun_acl)
1000 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1001 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
1002 struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
1004 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1007 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1008 lun->unpacked_lun, nacl, deve);
1011 static void __core_scsi3_dump_registration(
1012 struct target_core_fabric_ops *tfo,
1013 struct se_device *dev,
1014 struct se_node_acl *nacl,
1015 struct t10_pr_registration *pr_reg,
1018 struct se_portal_group *se_tpg = nacl->se_tpg;
1019 char i_buf[PR_REG_ISID_ID_LEN];
1022 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1023 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1024 PR_REG_ISID_ID_LEN);
1026 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1027 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1028 "_AND_MOVE" : (register_type == 1) ?
1029 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1030 (prf_isid) ? i_buf : "");
1031 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1032 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1033 tfo->tpg_get_tag(se_tpg));
1034 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1035 " Port(s)\n", tfo->get_fabric_name(),
1036 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1037 dev->transport->name);
1038 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1039 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1040 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1041 pr_reg->pr_reg_aptpl);
1045 * this function can be called with struct se_device->dev_reservation_lock
1046 * when register_move = 1
1048 static void __core_scsi3_add_registration(
1049 struct se_device *dev,
1050 struct se_node_acl *nacl,
1051 struct t10_pr_registration *pr_reg,
1055 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1056 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1057 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1058 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1061 * Increment PRgeneration counter for struct se_device upon a successful
1062 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1064 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1065 * action, the struct se_device->dev_reservation_lock will already be held,
1066 * so we do not call core_scsi3_pr_generation() which grabs the lock
1069 pr_reg->pr_res_generation = (register_move) ?
1070 su_dev->t10_pr.pr_generation++ :
1071 core_scsi3_pr_generation(dev);
1073 spin_lock(&pr_tmpl->registration_lock);
1074 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1075 pr_reg->pr_reg_deve->def_pr_registered = 1;
1077 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1078 spin_unlock(&pr_tmpl->registration_lock);
1080 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1082 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1085 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1086 * allocated in __core_scsi3_alloc_registration()
1088 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1089 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1090 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1092 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1094 spin_lock(&pr_tmpl->registration_lock);
1095 list_add_tail(&pr_reg_tmp->pr_reg_list,
1096 &pr_tmpl->registration_list);
1097 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1099 __core_scsi3_dump_registration(tfo, dev,
1100 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1102 spin_unlock(&pr_tmpl->registration_lock);
1104 * Drop configfs group dependency reference from
1105 * __core_scsi3_alloc_registration()
1107 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1111 static int core_scsi3_alloc_registration(
1112 struct se_device *dev,
1113 struct se_node_acl *nacl,
1114 struct se_dev_entry *deve,
1115 unsigned char *isid,
1122 struct t10_pr_registration *pr_reg;
1124 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1125 sa_res_key, all_tg_pt, aptpl);
1129 __core_scsi3_add_registration(dev, nacl, pr_reg,
1130 register_type, register_move);
1134 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1135 struct se_device *dev,
1136 struct se_node_acl *nacl,
1137 unsigned char *isid)
1139 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1140 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1141 struct se_portal_group *tpg;
1143 spin_lock(&pr_tmpl->registration_lock);
1144 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1145 &pr_tmpl->registration_list, pr_reg_list) {
1147 * First look for a matching struct se_node_acl
1149 if (pr_reg->pr_reg_nacl != nacl)
1152 tpg = pr_reg->pr_reg_nacl->se_tpg;
1154 * If this registration does NOT contain a fabric provided
1155 * ISID, then we have found a match.
1157 if (!pr_reg->isid_present_at_reg) {
1159 * Determine if this SCSI device server requires that
1160 * SCSI Intiatior TransportID w/ ISIDs is enforced
1161 * for fabric modules (iSCSI) requiring them.
1163 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1164 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
1167 atomic_inc(&pr_reg->pr_res_holders);
1168 smp_mb__after_atomic_inc();
1169 spin_unlock(&pr_tmpl->registration_lock);
1173 * If the *pr_reg contains a fabric defined ISID for multi-value
1174 * SCSI Initiator Port TransportIDs, then we expect a valid
1175 * matching ISID to be provided by the local SCSI Initiator Port.
1179 if (strcmp(isid, pr_reg->pr_reg_isid))
1182 atomic_inc(&pr_reg->pr_res_holders);
1183 smp_mb__after_atomic_inc();
1184 spin_unlock(&pr_tmpl->registration_lock);
1187 spin_unlock(&pr_tmpl->registration_lock);
1192 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1193 struct se_device *dev,
1194 struct se_node_acl *nacl,
1195 struct se_session *sess)
1197 struct se_portal_group *tpg = nacl->se_tpg;
1198 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1200 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1201 memset(&buf[0], 0, PR_REG_ISID_LEN);
1202 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1207 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1210 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1212 atomic_dec(&pr_reg->pr_res_holders);
1213 smp_mb__after_atomic_dec();
1216 static int core_scsi3_check_implict_release(
1217 struct se_device *dev,
1218 struct t10_pr_registration *pr_reg)
1220 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1221 struct t10_pr_registration *pr_res_holder;
1224 spin_lock(&dev->dev_reservation_lock);
1225 pr_res_holder = dev->dev_pr_res_holder;
1226 if (!pr_res_holder) {
1227 spin_unlock(&dev->dev_reservation_lock);
1230 if (pr_res_holder == pr_reg) {
1232 * Perform an implict RELEASE if the registration that
1233 * is being released is holding the reservation.
1235 * From spc4r17, section 5.7.11.1:
1237 * e) If the I_T nexus is the persistent reservation holder
1238 * and the persistent reservation is not an all registrants
1239 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1240 * service action or REGISTER AND IGNORE EXISTING KEY
1241 * service action with the SERVICE ACTION RESERVATION KEY
1242 * field set to zero (see 5.7.11.3).
1244 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1247 * For 'All Registrants' reservation types, all existing
1248 * registrations are still processed as reservation holders
1249 * in core_scsi3_pr_seq_non_holder() after the initial
1250 * reservation holder is implictly released here.
1252 } else if (pr_reg->pr_reg_all_tg_pt &&
1253 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1254 pr_reg->pr_reg_nacl->initiatorname)) &&
1255 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1256 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1257 " UNREGISTER while existing reservation with matching"
1258 " key 0x%016Lx is present from another SCSI Initiator"
1259 " Port\n", pr_reg->pr_res_key);
1262 spin_unlock(&dev->dev_reservation_lock);
1268 * Called with struct t10_reservation->registration_lock held.
1270 static void __core_scsi3_free_registration(
1271 struct se_device *dev,
1272 struct t10_pr_registration *pr_reg,
1273 struct list_head *preempt_and_abort_list,
1276 struct target_core_fabric_ops *tfo =
1277 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1278 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1279 char i_buf[PR_REG_ISID_ID_LEN];
1282 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1283 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1284 PR_REG_ISID_ID_LEN);
1286 pr_reg->pr_reg_deve->def_pr_registered = 0;
1287 pr_reg->pr_reg_deve->pr_res_key = 0;
1288 list_del(&pr_reg->pr_reg_list);
1290 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1291 * so call core_scsi3_put_pr_reg() to decrement our reference.
1294 core_scsi3_put_pr_reg(pr_reg);
1296 * Wait until all reference from any other I_T nexuses for this
1297 * *pr_reg have been released. Because list_del() is called above,
1298 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1299 * count back to zero, and we release *pr_reg.
1301 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1302 spin_unlock(&pr_tmpl->registration_lock);
1303 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1304 tfo->get_fabric_name());
1306 spin_lock(&pr_tmpl->registration_lock);
1309 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1310 " Node: %s%s\n", tfo->get_fabric_name(),
1311 pr_reg->pr_reg_nacl->initiatorname,
1312 (prf_isid) ? &i_buf[0] : "");
1313 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1314 " Port(s)\n", tfo->get_fabric_name(),
1315 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1316 dev->transport->name);
1317 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1318 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1319 pr_reg->pr_res_generation);
1321 if (!preempt_and_abort_list) {
1322 pr_reg->pr_reg_deve = NULL;
1323 pr_reg->pr_reg_nacl = NULL;
1324 kfree(pr_reg->pr_aptpl_buf);
1325 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1329 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1330 * are released once the ABORT_TASK_SET has completed..
1332 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1335 void core_scsi3_free_pr_reg_from_nacl(
1336 struct se_device *dev,
1337 struct se_node_acl *nacl)
1339 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1340 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1342 * If the passed se_node_acl matches the reservation holder,
1343 * release the reservation.
1345 spin_lock(&dev->dev_reservation_lock);
1346 pr_res_holder = dev->dev_pr_res_holder;
1347 if ((pr_res_holder != NULL) &&
1348 (pr_res_holder->pr_reg_nacl == nacl))
1349 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1350 spin_unlock(&dev->dev_reservation_lock);
1352 * Release any registration associated with the struct se_node_acl.
1354 spin_lock(&pr_tmpl->registration_lock);
1355 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1356 &pr_tmpl->registration_list, pr_reg_list) {
1358 if (pr_reg->pr_reg_nacl != nacl)
1361 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1363 spin_unlock(&pr_tmpl->registration_lock);
1366 void core_scsi3_free_all_registrations(
1367 struct se_device *dev)
1369 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1370 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1372 spin_lock(&dev->dev_reservation_lock);
1373 pr_res_holder = dev->dev_pr_res_holder;
1374 if (pr_res_holder != NULL) {
1375 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1376 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1379 spin_unlock(&dev->dev_reservation_lock);
1381 spin_lock(&pr_tmpl->registration_lock);
1382 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1383 &pr_tmpl->registration_list, pr_reg_list) {
1385 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1387 spin_unlock(&pr_tmpl->registration_lock);
1389 spin_lock(&pr_tmpl->aptpl_reg_lock);
1390 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1391 pr_reg_aptpl_list) {
1392 list_del(&pr_reg->pr_reg_aptpl_list);
1393 kfree(pr_reg->pr_aptpl_buf);
1394 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1396 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1399 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1401 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1402 &tpg->tpg_group.cg_item);
1405 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1407 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1408 &tpg->tpg_group.cg_item);
1410 atomic_dec(&tpg->tpg_pr_ref_count);
1411 smp_mb__after_atomic_dec();
1414 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1416 struct se_portal_group *tpg = nacl->se_tpg;
1418 if (nacl->dynamic_node_acl)
1421 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1422 &nacl->acl_group.cg_item);
1425 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1427 struct se_portal_group *tpg = nacl->se_tpg;
1429 if (nacl->dynamic_node_acl) {
1430 atomic_dec(&nacl->acl_pr_ref_count);
1431 smp_mb__after_atomic_dec();
1435 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1436 &nacl->acl_group.cg_item);
1438 atomic_dec(&nacl->acl_pr_ref_count);
1439 smp_mb__after_atomic_dec();
1442 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1444 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1445 struct se_node_acl *nacl;
1446 struct se_portal_group *tpg;
1448 * For nacl->dynamic_node_acl=1
1453 nacl = lun_acl->se_lun_nacl;
1456 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1457 &lun_acl->se_lun_group.cg_item);
1460 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1462 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1463 struct se_node_acl *nacl;
1464 struct se_portal_group *tpg;
1466 * For nacl->dynamic_node_acl=1
1469 atomic_dec(&se_deve->pr_ref_count);
1470 smp_mb__after_atomic_dec();
1473 nacl = lun_acl->se_lun_nacl;
1476 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1477 &lun_acl->se_lun_group.cg_item);
1479 atomic_dec(&se_deve->pr_ref_count);
1480 smp_mb__after_atomic_dec();
1483 static int core_scsi3_decode_spec_i_port(
1485 struct se_portal_group *tpg,
1486 unsigned char *l_isid,
1491 struct se_device *dev = cmd->se_dev;
1492 struct se_port *tmp_port;
1493 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1494 struct se_session *se_sess = cmd->se_sess;
1495 struct se_node_acl *dest_node_acl = NULL;
1496 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1497 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1498 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1499 LIST_HEAD(tid_dest_list);
1500 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1501 struct target_core_fabric_ops *tmp_tf_ops;
1503 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1504 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1505 u32 tpdl, tid_len = 0;
1506 int ret, dest_local_nexus, prf_isid;
1509 memset(dest_iport, 0, 64);
1511 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1513 * Allocate a struct pr_transport_id_holder and setup the
1514 * local_node_acl and local_se_deve pointers and add to
1515 * struct list_head tid_dest_list for add registration
1516 * processing in the loop of tid_dest_list below.
1518 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1520 pr_err("Unable to allocate tidh_new\n");
1521 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1524 INIT_LIST_HEAD(&tidh_new->dest_list);
1525 tidh_new->dest_tpg = tpg;
1526 tidh_new->dest_node_acl = se_sess->se_node_acl;
1527 tidh_new->dest_se_deve = local_se_deve;
1529 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1530 se_sess->se_node_acl, local_se_deve, l_isid,
1531 sa_res_key, all_tg_pt, aptpl);
1532 if (!local_pr_reg) {
1534 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1537 tidh_new->dest_pr_reg = local_pr_reg;
1539 * The local I_T nexus does not hold any configfs dependances,
1540 * so we set tid_h->dest_local_nexus=1 to prevent the
1541 * configfs_undepend_item() calls in the tid_dest_list loops below.
1543 tidh_new->dest_local_nexus = 1;
1544 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1546 buf = transport_kmap_data_sg(cmd);
1548 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1549 * first extract TransportID Parameter Data Length, and make sure
1550 * the value matches up to the SCSI expected data transfer length.
1552 tpdl = (buf[24] & 0xff) << 24;
1553 tpdl |= (buf[25] & 0xff) << 16;
1554 tpdl |= (buf[26] & 0xff) << 8;
1555 tpdl |= buf[27] & 0xff;
1557 if ((tpdl + 28) != cmd->data_length) {
1558 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1559 " does not equal CDB data_length: %u\n", tpdl,
1561 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1566 * Start processing the received transport IDs using the
1567 * receiving I_T Nexus portal's fabric dependent methods to
1568 * obtain the SCSI Initiator Port/Device Identifiers.
1573 proto_ident = (ptr[0] & 0x0f);
1576 spin_lock(&dev->se_port_lock);
1577 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1578 tmp_tpg = tmp_port->sep_tpg;
1581 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1584 if (!tmp_tf_ops->get_fabric_proto_ident ||
1585 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1588 * Look for the matching proto_ident provided by
1589 * the received TransportID
1591 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1592 if (tmp_proto_ident != proto_ident)
1594 dest_rtpi = tmp_port->sep_rtpi;
1596 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1597 tmp_tpg, (const char *)ptr, &tid_len,
1602 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1603 smp_mb__after_atomic_inc();
1604 spin_unlock(&dev->se_port_lock);
1606 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1608 pr_err(" core_scsi3_tpg_depend_item()"
1610 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1611 smp_mb__after_atomic_dec();
1612 cmd->scsi_sense_reason =
1613 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1618 * Locate the desination initiator ACL to be registered
1619 * from the decoded fabric module specific TransportID
1622 spin_lock_irq(&tmp_tpg->acl_node_lock);
1623 dest_node_acl = __core_tpg_get_initiator_node_acl(
1625 if (dest_node_acl) {
1626 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1627 smp_mb__after_atomic_inc();
1629 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1631 if (!dest_node_acl) {
1632 core_scsi3_tpg_undepend_item(tmp_tpg);
1633 spin_lock(&dev->se_port_lock);
1637 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1639 pr_err("configfs_depend_item() failed"
1640 " for dest_node_acl->acl_group\n");
1641 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1642 smp_mb__after_atomic_dec();
1643 core_scsi3_tpg_undepend_item(tmp_tpg);
1644 cmd->scsi_sense_reason =
1645 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1651 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1652 " %s Port RTPI: %hu\n",
1653 dest_tpg->se_tpg_tfo->get_fabric_name(),
1654 dest_node_acl->initiatorname, dest_rtpi);
1656 spin_lock(&dev->se_port_lock);
1659 spin_unlock(&dev->se_port_lock);
1662 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1664 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1669 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1670 " tid_len: %d for %s + %s\n",
1671 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1672 tpdl, tid_len, i_str, iport_ptr);
1674 if (tid_len > tpdl) {
1675 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1676 " %u for Transport ID: %s\n", tid_len, ptr);
1677 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1678 core_scsi3_tpg_undepend_item(dest_tpg);
1679 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1684 * Locate the desintation struct se_dev_entry pointer for matching
1685 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1688 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1690 if (!dest_se_deve) {
1691 pr_err("Unable to locate %s dest_se_deve"
1692 " from destination RTPI: %hu\n",
1693 dest_tpg->se_tpg_tfo->get_fabric_name(),
1696 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1697 core_scsi3_tpg_undepend_item(dest_tpg);
1698 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1703 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1705 pr_err("core_scsi3_lunacl_depend_item()"
1707 atomic_dec(&dest_se_deve->pr_ref_count);
1708 smp_mb__after_atomic_dec();
1709 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1710 core_scsi3_tpg_undepend_item(dest_tpg);
1711 cmd->scsi_sense_reason =
1712 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1717 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1718 " dest_se_deve mapped_lun: %u\n",
1719 dest_tpg->se_tpg_tfo->get_fabric_name(),
1720 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1723 * Skip any TransportIDs that already have a registration for
1726 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1729 core_scsi3_put_pr_reg(pr_reg_e);
1730 core_scsi3_lunacl_undepend_item(dest_se_deve);
1731 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1732 core_scsi3_tpg_undepend_item(dest_tpg);
1739 * Allocate a struct pr_transport_id_holder and setup
1740 * the dest_node_acl and dest_se_deve pointers for the
1743 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1746 pr_err("Unable to allocate tidh_new\n");
1747 core_scsi3_lunacl_undepend_item(dest_se_deve);
1748 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1749 core_scsi3_tpg_undepend_item(dest_tpg);
1750 cmd->scsi_sense_reason =
1751 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1755 INIT_LIST_HEAD(&tidh_new->dest_list);
1756 tidh_new->dest_tpg = dest_tpg;
1757 tidh_new->dest_node_acl = dest_node_acl;
1758 tidh_new->dest_se_deve = dest_se_deve;
1761 * Allocate, but do NOT add the registration for the
1762 * TransportID referenced SCSI Initiator port. This
1763 * done because of the following from spc4r17 in section
1764 * 6.14.3 wrt SPEC_I_PT:
1766 * "If a registration fails for any initiator port (e.g., if th
1767 * logical unit does not have enough resources available to
1768 * hold the registration information), no registrations shall be
1769 * made, and the command shall be terminated with
1770 * CHECK CONDITION status."
1772 * That means we call __core_scsi3_alloc_registration() here,
1773 * and then call __core_scsi3_add_registration() in the
1774 * 2nd loop which will never fail.
1776 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1777 dest_node_acl, dest_se_deve, iport_ptr,
1778 sa_res_key, all_tg_pt, aptpl);
1780 core_scsi3_lunacl_undepend_item(dest_se_deve);
1781 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1782 core_scsi3_tpg_undepend_item(dest_tpg);
1784 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1788 tidh_new->dest_pr_reg = dest_pr_reg;
1789 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1797 transport_kunmap_data_sg(cmd);
1800 * Go ahead and create a registrations from tid_dest_list for the
1801 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1804 * The SA Reservation Key from the PROUT is set for the
1805 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1806 * means that the TransportID Initiator port will be
1807 * registered on all of the target ports in the SCSI target device
1808 * ALL_TG_PT=0 means the registration will only be for the
1809 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1812 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1813 dest_tpg = tidh->dest_tpg;
1814 dest_node_acl = tidh->dest_node_acl;
1815 dest_se_deve = tidh->dest_se_deve;
1816 dest_pr_reg = tidh->dest_pr_reg;
1817 dest_local_nexus = tidh->dest_local_nexus;
1819 list_del(&tidh->dest_list);
1822 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1823 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1824 PR_REG_ISID_ID_LEN);
1826 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1829 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1830 " registered Transport ID for Node: %s%s Mapped LUN:"
1831 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1832 dest_node_acl->initiatorname, (prf_isid) ?
1833 &i_buf[0] : "", dest_se_deve->mapped_lun);
1835 if (dest_local_nexus)
1838 core_scsi3_lunacl_undepend_item(dest_se_deve);
1839 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1840 core_scsi3_tpg_undepend_item(dest_tpg);
1845 transport_kunmap_data_sg(cmd);
1847 * For the failure case, release everything from tid_dest_list
1848 * including *dest_pr_reg and the configfs dependances..
1850 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1851 dest_tpg = tidh->dest_tpg;
1852 dest_node_acl = tidh->dest_node_acl;
1853 dest_se_deve = tidh->dest_se_deve;
1854 dest_pr_reg = tidh->dest_pr_reg;
1855 dest_local_nexus = tidh->dest_local_nexus;
1857 list_del(&tidh->dest_list);
1860 * Release any extra ALL_TG_PT=1 registrations for
1861 * the SPEC_I_PT=1 case.
1863 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1864 &dest_pr_reg->pr_reg_atp_list,
1865 pr_reg_atp_mem_list) {
1866 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1867 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1868 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1871 kfree(dest_pr_reg->pr_aptpl_buf);
1872 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1874 if (dest_local_nexus)
1877 core_scsi3_lunacl_undepend_item(dest_se_deve);
1878 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1879 core_scsi3_tpg_undepend_item(dest_tpg);
1885 * Called with struct se_device->dev_reservation_lock held
1887 static int __core_scsi3_update_aptpl_buf(
1888 struct se_device *dev,
1890 u32 pr_aptpl_buf_len,
1891 int clear_aptpl_metadata)
1894 struct se_portal_group *tpg;
1895 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1896 struct t10_pr_registration *pr_reg;
1897 unsigned char tmp[512], isid_buf[32];
1901 memset(buf, 0, pr_aptpl_buf_len);
1903 * Called to clear metadata once APTPL has been deactivated.
1905 if (clear_aptpl_metadata) {
1906 snprintf(buf, pr_aptpl_buf_len,
1907 "No Registrations or Reservations\n");
1911 * Walk the registration list..
1913 spin_lock(&su_dev->t10_pr.registration_lock);
1914 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1919 tpg = pr_reg->pr_reg_nacl->se_tpg;
1920 lun = pr_reg->pr_reg_tg_pt_lun;
1922 * Write out any ISID value to APTPL metadata that was included
1923 * in the original registration.
1925 if (pr_reg->isid_present_at_reg)
1926 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1927 pr_reg->pr_reg_isid);
1929 * Include special metadata if the pr_reg matches the
1930 * reservation holder.
1932 if (dev->dev_pr_res_holder == pr_reg) {
1933 snprintf(tmp, 512, "PR_REG_START: %d"
1934 "\ninitiator_fabric=%s\n"
1935 "initiator_node=%s\n%s"
1937 "res_holder=1\nres_type=%02x\n"
1938 "res_scope=%02x\nres_all_tg_pt=%d\n"
1939 "mapped_lun=%u\n", reg_count,
1940 tpg->se_tpg_tfo->get_fabric_name(),
1941 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1942 pr_reg->pr_res_key, pr_reg->pr_res_type,
1943 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1944 pr_reg->pr_res_mapped_lun);
1946 snprintf(tmp, 512, "PR_REG_START: %d\n"
1947 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1948 "sa_res_key=%llu\nres_holder=0\n"
1949 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1950 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1951 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1952 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1953 pr_reg->pr_res_mapped_lun);
1956 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1957 pr_err("Unable to update renaming"
1958 " APTPL metadata\n");
1959 spin_unlock(&su_dev->t10_pr.registration_lock);
1962 len += sprintf(buf+len, "%s", tmp);
1965 * Include information about the associated SCSI target port.
1967 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1968 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1969 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1970 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1971 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1972 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1974 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1975 pr_err("Unable to update renaming"
1976 " APTPL metadata\n");
1977 spin_unlock(&su_dev->t10_pr.registration_lock);
1980 len += sprintf(buf+len, "%s", tmp);
1983 spin_unlock(&su_dev->t10_pr.registration_lock);
1986 len += sprintf(buf+len, "No Registrations or Reservations");
1991 static int core_scsi3_update_aptpl_buf(
1992 struct se_device *dev,
1994 u32 pr_aptpl_buf_len,
1995 int clear_aptpl_metadata)
1999 spin_lock(&dev->dev_reservation_lock);
2000 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2001 clear_aptpl_metadata);
2002 spin_unlock(&dev->dev_reservation_lock);
2008 * Called with struct se_device->aptpl_file_mutex held
2010 static int __core_scsi3_write_aptpl_to_file(
2011 struct se_device *dev,
2013 u32 pr_aptpl_buf_len)
2015 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
2017 struct iovec iov[1];
2018 mm_segment_t old_fs;
2019 int flags = O_RDWR | O_CREAT | O_TRUNC;
2023 memset(iov, 0, sizeof(struct iovec));
2024 memset(path, 0, 512);
2026 if (strlen(&wwn->unit_serial[0]) >= 512) {
2027 pr_err("WWN value for struct se_device does not fit"
2028 " into path buffer\n");
2032 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2033 file = filp_open(path, flags, 0600);
2034 if (IS_ERR(file) || !file || !file->f_dentry) {
2035 pr_err("filp_open(%s) for APTPL metadata"
2037 return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
2040 iov[0].iov_base = &buf[0];
2041 if (!pr_aptpl_buf_len)
2042 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2044 iov[0].iov_len = pr_aptpl_buf_len;
2048 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2052 pr_debug("Error writing APTPL metadata file: %s\n", path);
2053 filp_close(file, NULL);
2056 filp_close(file, NULL);
2061 static int core_scsi3_update_and_write_aptpl(
2062 struct se_device *dev,
2063 unsigned char *in_buf,
2064 u32 in_pr_aptpl_buf_len)
2066 unsigned char null_buf[64], *buf;
2067 u32 pr_aptpl_buf_len;
2068 int ret, clear_aptpl_metadata = 0;
2070 * Can be called with a NULL pointer from PROUT service action CLEAR
2073 memset(null_buf, 0, 64);
2076 * This will clear the APTPL metadata to:
2077 * "No Registrations or Reservations" status
2079 pr_aptpl_buf_len = 64;
2080 clear_aptpl_metadata = 1;
2083 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2086 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2087 clear_aptpl_metadata);
2091 * __core_scsi3_write_aptpl_to_file() will call strlen()
2092 * on the passed buf to determine pr_aptpl_buf_len.
2094 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2101 static int core_scsi3_emulate_pro_register(
2110 struct se_session *se_sess = cmd->se_sess;
2111 struct se_device *dev = cmd->se_dev;
2112 struct se_dev_entry *se_deve;
2113 struct se_lun *se_lun = cmd->se_lun;
2114 struct se_portal_group *se_tpg;
2115 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2116 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2117 /* Used for APTPL metadata w/ UNREGISTER */
2118 unsigned char *pr_aptpl_buf = NULL;
2119 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2120 int pr_holder = 0, ret = 0, type;
2122 if (!se_sess || !se_lun) {
2123 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2124 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2127 se_tpg = se_sess->se_tpg;
2128 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2130 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2131 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2132 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2134 isid_ptr = &isid_buf[0];
2137 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2139 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2142 pr_warn("SPC-3 PR: Reservation Key non-zero"
2143 " for SA REGISTER, returning CONFLICT\n");
2144 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2148 * Do nothing but return GOOD status.
2155 * Perform the Service Action REGISTER on the Initiator
2156 * Port Endpoint that the PRO was received from on the
2157 * Logical Unit of the SCSI device server.
2159 ret = core_scsi3_alloc_registration(cmd->se_dev,
2160 se_sess->se_node_acl, se_deve, isid_ptr,
2161 sa_res_key, all_tg_pt, aptpl,
2164 pr_err("Unable to allocate"
2165 " struct t10_pr_registration\n");
2166 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2171 * Register both the Initiator port that received
2172 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2173 * TransportID from Parameter list and loop through
2174 * fabric dependent parameter list while calling
2175 * logic from of core_scsi3_alloc_registration() for
2176 * each TransportID provided SCSI Initiator Port/Device
2178 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2179 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2184 * Nothing left to do for the APTPL=0 case.
2187 pr_tmpl->pr_aptpl_active = 0;
2188 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2189 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2194 * Locate the newly allocated local I_T Nexus *pr_reg, and
2195 * update the APTPL metadata information using its
2196 * preallocated *pr_reg->pr_aptpl_buf.
2198 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2199 se_sess->se_node_acl, se_sess);
2201 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2202 &pr_reg->pr_aptpl_buf[0],
2203 pr_tmpl->pr_aptpl_buf_len);
2205 pr_tmpl->pr_aptpl_active = 1;
2206 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2209 core_scsi3_put_pr_reg(pr_reg);
2213 * Locate the existing *pr_reg via struct se_node_acl pointers
2216 type = pr_reg->pr_res_type;
2219 if (res_key != pr_reg->pr_res_key) {
2220 pr_err("SPC-3 PR REGISTER: Received"
2221 " res_key: 0x%016Lx does not match"
2222 " existing SA REGISTER res_key:"
2223 " 0x%016Lx\n", res_key,
2224 pr_reg->pr_res_key);
2225 core_scsi3_put_pr_reg(pr_reg);
2226 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2231 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2232 " set while sa_res_key=0\n");
2233 core_scsi3_put_pr_reg(pr_reg);
2234 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2238 * An existing ALL_TG_PT=1 registration being released
2239 * must also set ALL_TG_PT=1 in the incoming PROUT.
2241 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2242 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2243 " registration exists, but ALL_TG_PT=1 bit not"
2244 " present in received PROUT\n");
2245 core_scsi3_put_pr_reg(pr_reg);
2246 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2250 * Allocate APTPL metadata buffer used for UNREGISTER ops
2253 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2255 if (!pr_aptpl_buf) {
2256 pr_err("Unable to allocate"
2258 core_scsi3_put_pr_reg(pr_reg);
2259 cmd->scsi_sense_reason =
2260 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2265 * sa_res_key=0 Unregister Reservation Key for registered I_T
2266 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2270 pr_holder = core_scsi3_check_implict_release(
2271 cmd->se_dev, pr_reg);
2272 if (pr_holder < 0) {
2273 kfree(pr_aptpl_buf);
2274 core_scsi3_put_pr_reg(pr_reg);
2275 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2279 spin_lock(&pr_tmpl->registration_lock);
2281 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2282 * and matching pr_res_key.
2284 if (pr_reg->pr_reg_all_tg_pt) {
2285 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2286 &pr_tmpl->registration_list,
2289 if (!pr_reg_p->pr_reg_all_tg_pt)
2292 if (pr_reg_p->pr_res_key != res_key)
2295 if (pr_reg == pr_reg_p)
2298 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2299 pr_reg_p->pr_reg_nacl->initiatorname))
2302 __core_scsi3_free_registration(dev,
2307 * Release the calling I_T Nexus registration now..
2309 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2312 * From spc4r17, section 5.7.11.3 Unregistering
2314 * If the persistent reservation is a registrants only
2315 * type, the device server shall establish a unit
2316 * attention condition for the initiator port associated
2317 * with every registered I_T nexus except for the I_T
2318 * nexus on which the PERSISTENT RESERVE OUT command was
2319 * received, with the additional sense code set to
2320 * RESERVATIONS RELEASED.
2323 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2324 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2325 list_for_each_entry(pr_reg_p,
2326 &pr_tmpl->registration_list,
2329 core_scsi3_ua_allocate(
2330 pr_reg_p->pr_reg_nacl,
2331 pr_reg_p->pr_res_mapped_lun,
2333 ASCQ_2AH_RESERVATIONS_RELEASED);
2336 spin_unlock(&pr_tmpl->registration_lock);
2339 pr_tmpl->pr_aptpl_active = 0;
2340 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2341 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2342 " for UNREGISTER\n");
2346 ret = core_scsi3_update_and_write_aptpl(dev,
2348 pr_tmpl->pr_aptpl_buf_len);
2350 pr_tmpl->pr_aptpl_active = 1;
2351 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2352 " for UNREGISTER\n");
2355 kfree(pr_aptpl_buf);
2359 * Increment PRgeneration counter for struct se_device"
2360 * upon a successful REGISTER, see spc4r17 section 6.3.2
2361 * READ_KEYS service action.
2363 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2365 pr_reg->pr_res_key = sa_res_key;
2366 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2367 " Key for %s to: 0x%016Lx PRgeneration:"
2368 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2369 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2370 pr_reg->pr_reg_nacl->initiatorname,
2371 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2374 pr_tmpl->pr_aptpl_active = 0;
2375 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2376 core_scsi3_put_pr_reg(pr_reg);
2377 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2382 ret = core_scsi3_update_and_write_aptpl(dev,
2384 pr_tmpl->pr_aptpl_buf_len);
2386 pr_tmpl->pr_aptpl_active = 1;
2387 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2391 kfree(pr_aptpl_buf);
2392 core_scsi3_put_pr_reg(pr_reg);
2398 unsigned char *core_scsi3_pr_dump_type(int type)
2401 case PR_TYPE_WRITE_EXCLUSIVE:
2402 return "Write Exclusive Access";
2403 case PR_TYPE_EXCLUSIVE_ACCESS:
2404 return "Exclusive Access";
2405 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2406 return "Write Exclusive Access, Registrants Only";
2407 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2408 return "Exclusive Access, Registrants Only";
2409 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2410 return "Write Exclusive Access, All Registrants";
2411 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2412 return "Exclusive Access, All Registrants";
2417 return "Unknown SPC-3 PR Type";
2420 static int core_scsi3_pro_reserve(
2422 struct se_device *dev,
2427 struct se_session *se_sess = cmd->se_sess;
2428 struct se_lun *se_lun = cmd->se_lun;
2429 struct t10_pr_registration *pr_reg, *pr_res_holder;
2430 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2431 char i_buf[PR_REG_ISID_ID_LEN];
2434 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2436 if (!se_sess || !se_lun) {
2437 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2438 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2442 * Locate the existing *pr_reg via struct se_node_acl pointers
2444 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2447 pr_err("SPC-3 PR: Unable to locate"
2448 " PR_REGISTERED *pr_reg for RESERVE\n");
2449 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2453 * From spc4r17 Section 5.7.9: Reserving:
2455 * An application client creates a persistent reservation by issuing
2456 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2457 * a registered I_T nexus with the following parameters:
2458 * a) RESERVATION KEY set to the value of the reservation key that is
2459 * registered with the logical unit for the I_T nexus; and
2461 if (res_key != pr_reg->pr_res_key) {
2462 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2463 " does not match existing SA REGISTER res_key:"
2464 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2465 core_scsi3_put_pr_reg(pr_reg);
2466 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2470 * From spc4r17 Section 5.7.9: Reserving:
2473 * b) TYPE field and SCOPE field set to the persistent reservation
2476 * Only one persistent reservation is allowed at a time per logical unit
2477 * and that persistent reservation has a scope of LU_SCOPE.
2479 if (scope != PR_SCOPE_LU_SCOPE) {
2480 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2481 core_scsi3_put_pr_reg(pr_reg);
2482 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2486 * See if we have an existing PR reservation holder pointer at
2487 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2490 spin_lock(&dev->dev_reservation_lock);
2491 pr_res_holder = dev->dev_pr_res_holder;
2492 if ((pr_res_holder)) {
2494 * From spc4r17 Section 5.7.9: Reserving:
2496 * If the device server receives a PERSISTENT RESERVE OUT
2497 * command from an I_T nexus other than a persistent reservation
2498 * holder (see 5.7.10) that attempts to create a persistent
2499 * reservation when a persistent reservation already exists for
2500 * the logical unit, then the command shall be completed with
2501 * RESERVATION CONFLICT status.
2503 if (pr_res_holder != pr_reg) {
2504 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2505 pr_err("SPC-3 PR: Attempted RESERVE from"
2506 " [%s]: %s while reservation already held by"
2507 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2508 cmd->se_tfo->get_fabric_name(),
2509 se_sess->se_node_acl->initiatorname,
2510 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2511 pr_res_holder->pr_reg_nacl->initiatorname);
2513 spin_unlock(&dev->dev_reservation_lock);
2514 core_scsi3_put_pr_reg(pr_reg);
2515 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2519 * From spc4r17 Section 5.7.9: Reserving:
2521 * If a persistent reservation holder attempts to modify the
2522 * type or scope of an existing persistent reservation, the
2523 * command shall be completed with RESERVATION CONFLICT status.
2525 if ((pr_res_holder->pr_res_type != type) ||
2526 (pr_res_holder->pr_res_scope != scope)) {
2527 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2528 pr_err("SPC-3 PR: Attempted RESERVE from"
2529 " [%s]: %s trying to change TYPE and/or SCOPE,"
2530 " while reservation already held by [%s]: %s,"
2531 " returning RESERVATION_CONFLICT\n",
2532 cmd->se_tfo->get_fabric_name(),
2533 se_sess->se_node_acl->initiatorname,
2534 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2535 pr_res_holder->pr_reg_nacl->initiatorname);
2537 spin_unlock(&dev->dev_reservation_lock);
2538 core_scsi3_put_pr_reg(pr_reg);
2539 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2543 * From spc4r17 Section 5.7.9: Reserving:
2545 * If the device server receives a PERSISTENT RESERVE OUT
2546 * command with RESERVE service action where the TYPE field and
2547 * the SCOPE field contain the same values as the existing type
2548 * and scope from a persistent reservation holder, it shall not
2549 * make any change to the existing persistent reservation and
2550 * shall completethe command with GOOD status.
2552 spin_unlock(&dev->dev_reservation_lock);
2553 core_scsi3_put_pr_reg(pr_reg);
2557 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2558 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2560 pr_reg->pr_res_scope = scope;
2561 pr_reg->pr_res_type = type;
2562 pr_reg->pr_res_holder = 1;
2563 dev->dev_pr_res_holder = pr_reg;
2564 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2565 PR_REG_ISID_ID_LEN);
2567 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2568 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2569 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2570 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2571 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2572 cmd->se_tfo->get_fabric_name(),
2573 se_sess->se_node_acl->initiatorname,
2574 (prf_isid) ? &i_buf[0] : "");
2575 spin_unlock(&dev->dev_reservation_lock);
2577 if (pr_tmpl->pr_aptpl_active) {
2578 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2579 &pr_reg->pr_aptpl_buf[0],
2580 pr_tmpl->pr_aptpl_buf_len);
2582 pr_debug("SPC-3 PR: Updated APTPL metadata"
2586 core_scsi3_put_pr_reg(pr_reg);
2590 static int core_scsi3_emulate_pro_reserve(
2596 struct se_device *dev = cmd->se_dev;
2600 case PR_TYPE_WRITE_EXCLUSIVE:
2601 case PR_TYPE_EXCLUSIVE_ACCESS:
2602 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2603 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2604 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2605 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2606 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2609 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2611 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2619 * Called with struct se_device->dev_reservation_lock held.
2621 static void __core_scsi3_complete_pro_release(
2622 struct se_device *dev,
2623 struct se_node_acl *se_nacl,
2624 struct t10_pr_registration *pr_reg,
2627 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2628 char i_buf[PR_REG_ISID_ID_LEN];
2631 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2632 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2633 PR_REG_ISID_ID_LEN);
2635 * Go ahead and release the current PR reservation holder.
2637 dev->dev_pr_res_holder = NULL;
2639 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2640 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2641 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2642 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2643 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2644 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2645 tfo->get_fabric_name(), se_nacl->initiatorname,
2646 (prf_isid) ? &i_buf[0] : "");
2648 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2650 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2653 static int core_scsi3_emulate_pro_release(
2659 struct se_device *dev = cmd->se_dev;
2660 struct se_session *se_sess = cmd->se_sess;
2661 struct se_lun *se_lun = cmd->se_lun;
2662 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2663 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2664 int ret, all_reg = 0;
2666 if (!se_sess || !se_lun) {
2667 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2668 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2672 * Locate the existing *pr_reg via struct se_node_acl pointers
2674 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2676 pr_err("SPC-3 PR: Unable to locate"
2677 " PR_REGISTERED *pr_reg for RELEASE\n");
2678 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2682 * From spc4r17 Section 5.7.11.2 Releasing:
2684 * If there is no persistent reservation or in response to a persistent
2685 * reservation release request from a registered I_T nexus that is not a
2686 * persistent reservation holder (see 5.7.10), the device server shall
2689 * a) Not release the persistent reservation, if any;
2690 * b) Not remove any registrations; and
2691 * c) Complete the command with GOOD status.
2693 spin_lock(&dev->dev_reservation_lock);
2694 pr_res_holder = dev->dev_pr_res_holder;
2695 if (!pr_res_holder) {
2697 * No persistent reservation, return GOOD status.
2699 spin_unlock(&dev->dev_reservation_lock);
2700 core_scsi3_put_pr_reg(pr_reg);
2703 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2704 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2707 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2709 * Non 'All Registrants' PR Type cases..
2710 * Release request from a registered I_T nexus that is not a
2711 * persistent reservation holder. return GOOD status.
2713 spin_unlock(&dev->dev_reservation_lock);
2714 core_scsi3_put_pr_reg(pr_reg);
2718 * From spc4r17 Section 5.7.11.2 Releasing:
2720 * Only the persistent reservation holder (see 5.7.10) is allowed to
2721 * release a persistent reservation.
2723 * An application client releases the persistent reservation by issuing
2724 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2725 * an I_T nexus that is a persistent reservation holder with the
2726 * following parameters:
2728 * a) RESERVATION KEY field set to the value of the reservation key
2729 * that is registered with the logical unit for the I_T nexus;
2731 if (res_key != pr_reg->pr_res_key) {
2732 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2733 " does not match existing SA REGISTER res_key:"
2734 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2735 spin_unlock(&dev->dev_reservation_lock);
2736 core_scsi3_put_pr_reg(pr_reg);
2737 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2741 * From spc4r17 Section 5.7.11.2 Releasing and above:
2743 * b) TYPE field and SCOPE field set to match the persistent
2744 * reservation being released.
2746 if ((pr_res_holder->pr_res_type != type) ||
2747 (pr_res_holder->pr_res_scope != scope)) {
2748 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2749 pr_err("SPC-3 PR RELEASE: Attempted to release"
2750 " reservation from [%s]: %s with different TYPE "
2751 "and/or SCOPE while reservation already held by"
2752 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2753 cmd->se_tfo->get_fabric_name(),
2754 se_sess->se_node_acl->initiatorname,
2755 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2756 pr_res_holder->pr_reg_nacl->initiatorname);
2758 spin_unlock(&dev->dev_reservation_lock);
2759 core_scsi3_put_pr_reg(pr_reg);
2760 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2764 * In response to a persistent reservation release request from the
2765 * persistent reservation holder the device server shall perform a
2766 * release by doing the following as an uninterrupted series of actions:
2767 * a) Release the persistent reservation;
2768 * b) Not remove any registration(s);
2769 * c) If the released persistent reservation is a registrants only type
2770 * or all registrants type persistent reservation,
2771 * the device server shall establish a unit attention condition for
2772 * the initiator port associated with every regis-
2773 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2774 * RESERVE OUT command with RELEASE service action was received,
2775 * with the additional sense code set to RESERVATIONS RELEASED; and
2776 * d) If the persistent reservation is of any other type, the device
2777 * server shall not establish a unit attention condition.
2779 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2782 spin_unlock(&dev->dev_reservation_lock);
2784 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2785 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2786 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2787 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2789 * If no UNIT ATTENTION conditions will be established for
2790 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2791 * go ahead and check for APTPL=1 update+write below
2796 spin_lock(&pr_tmpl->registration_lock);
2797 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2800 * Do not establish a UNIT ATTENTION condition
2801 * for the calling I_T Nexus
2803 if (pr_reg_p == pr_reg)
2806 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2807 pr_reg_p->pr_res_mapped_lun,
2808 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2810 spin_unlock(&pr_tmpl->registration_lock);
2813 if (pr_tmpl->pr_aptpl_active) {
2814 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2815 &pr_reg->pr_aptpl_buf[0],
2816 pr_tmpl->pr_aptpl_buf_len);
2818 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2821 core_scsi3_put_pr_reg(pr_reg);
2825 static int core_scsi3_emulate_pro_clear(
2829 struct se_device *dev = cmd->se_dev;
2830 struct se_node_acl *pr_reg_nacl;
2831 struct se_session *se_sess = cmd->se_sess;
2832 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2833 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2834 u32 pr_res_mapped_lun = 0;
2835 int calling_it_nexus = 0;
2837 * Locate the existing *pr_reg via struct se_node_acl pointers
2839 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2840 se_sess->se_node_acl, se_sess);
2842 pr_err("SPC-3 PR: Unable to locate"
2843 " PR_REGISTERED *pr_reg for CLEAR\n");
2844 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2848 * From spc4r17 section 5.7.11.6, Clearing:
2850 * Any application client may release the persistent reservation and
2851 * remove all registrations from a device server by issuing a
2852 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2853 * registered I_T nexus with the following parameter:
2855 * a) RESERVATION KEY field set to the value of the reservation key
2856 * that is registered with the logical unit for the I_T nexus.
2858 if (res_key != pr_reg_n->pr_res_key) {
2859 pr_err("SPC-3 PR REGISTER: Received"
2860 " res_key: 0x%016Lx does not match"
2861 " existing SA REGISTER res_key:"
2862 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2863 core_scsi3_put_pr_reg(pr_reg_n);
2864 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2868 * a) Release the persistent reservation, if any;
2870 spin_lock(&dev->dev_reservation_lock);
2871 pr_res_holder = dev->dev_pr_res_holder;
2872 if (pr_res_holder) {
2873 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2874 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2877 spin_unlock(&dev->dev_reservation_lock);
2879 * b) Remove all registration(s) (see spc4r17 5.7.7);
2881 spin_lock(&pr_tmpl->registration_lock);
2882 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2883 &pr_tmpl->registration_list, pr_reg_list) {
2885 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2886 pr_reg_nacl = pr_reg->pr_reg_nacl;
2887 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2888 __core_scsi3_free_registration(dev, pr_reg, NULL,
2891 * e) Establish a unit attention condition for the initiator
2892 * port associated with every registered I_T nexus other
2893 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2894 * command with CLEAR service action was received, with the
2895 * additional sense code set to RESERVATIONS PREEMPTED.
2897 if (!calling_it_nexus)
2898 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2899 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2901 spin_unlock(&pr_tmpl->registration_lock);
2903 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2904 cmd->se_tfo->get_fabric_name());
2906 if (pr_tmpl->pr_aptpl_active) {
2907 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2908 pr_debug("SPC-3 PR: Updated APTPL metadata"
2912 core_scsi3_pr_generation(dev);
2917 * Called with struct se_device->dev_reservation_lock held.
2919 static void __core_scsi3_complete_pro_preempt(
2920 struct se_device *dev,
2921 struct t10_pr_registration *pr_reg,
2922 struct list_head *preempt_and_abort_list,
2927 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2928 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2929 char i_buf[PR_REG_ISID_ID_LEN];
2932 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2933 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2934 PR_REG_ISID_ID_LEN);
2936 * Do an implict RELEASE of the existing reservation.
2938 if (dev->dev_pr_res_holder)
2939 __core_scsi3_complete_pro_release(dev, nacl,
2940 dev->dev_pr_res_holder, 0);
2942 dev->dev_pr_res_holder = pr_reg;
2943 pr_reg->pr_res_holder = 1;
2944 pr_reg->pr_res_type = type;
2945 pr_reg->pr_res_scope = scope;
2947 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2948 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2949 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2950 core_scsi3_pr_dump_type(type),
2951 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2952 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2953 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2954 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2956 * For PREEMPT_AND_ABORT, add the preempting reservation's
2957 * struct t10_pr_registration to the list that will be compared
2958 * against received CDBs..
2960 if (preempt_and_abort_list)
2961 list_add_tail(&pr_reg->pr_reg_abort_list,
2962 preempt_and_abort_list);
2965 static void core_scsi3_release_preempt_and_abort(
2966 struct list_head *preempt_and_abort_list,
2967 struct t10_pr_registration *pr_reg_holder)
2969 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2971 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2972 pr_reg_abort_list) {
2974 list_del(&pr_reg->pr_reg_abort_list);
2975 if (pr_reg_holder == pr_reg)
2977 if (pr_reg->pr_res_holder) {
2978 pr_warn("pr_reg->pr_res_holder still set\n");
2982 pr_reg->pr_reg_deve = NULL;
2983 pr_reg->pr_reg_nacl = NULL;
2984 kfree(pr_reg->pr_aptpl_buf);
2985 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2989 static int core_scsi3_pro_preempt(
2997 struct se_device *dev = cmd->se_dev;
2998 struct se_node_acl *pr_reg_nacl;
2999 struct se_session *se_sess = cmd->se_sess;
3000 LIST_HEAD(preempt_and_abort_list);
3001 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
3002 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3003 u32 pr_res_mapped_lun = 0;
3004 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3005 int prh_type = 0, prh_scope = 0, ret;
3008 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3012 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3015 pr_err("SPC-3 PR: Unable to locate"
3016 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3017 (abort) ? "_AND_ABORT" : "");
3018 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3021 if (pr_reg_n->pr_res_key != res_key) {
3022 core_scsi3_put_pr_reg(pr_reg_n);
3023 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3026 if (scope != PR_SCOPE_LU_SCOPE) {
3027 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3028 core_scsi3_put_pr_reg(pr_reg_n);
3029 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3033 spin_lock(&dev->dev_reservation_lock);
3034 pr_res_holder = dev->dev_pr_res_holder;
3035 if (pr_res_holder &&
3036 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3037 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3040 if (!all_reg && !sa_res_key) {
3041 spin_unlock(&dev->dev_reservation_lock);
3042 core_scsi3_put_pr_reg(pr_reg_n);
3043 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3047 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3049 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3050 * persistent reservation holder or there is no persistent reservation
3051 * holder (i.e., there is no persistent reservation), then the device
3052 * server shall perform a preempt by doing the following in an
3053 * uninterrupted series of actions. (See below..)
3055 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3057 * No existing or SA Reservation Key matching reservations..
3059 * PROUT SA PREEMPT with All Registrant type reservations are
3060 * allowed to be processed without a matching SA Reservation Key
3062 spin_lock(&pr_tmpl->registration_lock);
3063 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3064 &pr_tmpl->registration_list, pr_reg_list) {
3066 * Removing of registrations in non all registrants
3067 * type reservations without a matching SA reservation
3070 * a) Remove the registrations for all I_T nexuses
3071 * specified by the SERVICE ACTION RESERVATION KEY
3073 * b) Ignore the contents of the SCOPE and TYPE fields;
3074 * c) Process tasks as defined in 5.7.1; and
3075 * d) Establish a unit attention condition for the
3076 * initiator port associated with every I_T nexus
3077 * that lost its registration other than the I_T
3078 * nexus on which the PERSISTENT RESERVE OUT command
3079 * was received, with the additional sense code set
3080 * to REGISTRATIONS PREEMPTED.
3083 if (pr_reg->pr_res_key != sa_res_key)
3086 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3087 pr_reg_nacl = pr_reg->pr_reg_nacl;
3088 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3089 __core_scsi3_free_registration(dev, pr_reg,
3090 (abort) ? &preempt_and_abort_list :
3091 NULL, calling_it_nexus);
3095 * Case for any existing all registrants type
3096 * reservation, follow logic in spc4r17 section
3097 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3099 * For a ZERO SA Reservation key, release
3100 * all other registrations and do an implict
3101 * release of active persistent reservation.
3103 * For a non-ZERO SA Reservation key, only
3104 * release the matching reservation key from
3108 (pr_reg->pr_res_key != sa_res_key))
3111 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3112 if (calling_it_nexus)
3115 pr_reg_nacl = pr_reg->pr_reg_nacl;
3116 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3117 __core_scsi3_free_registration(dev, pr_reg,
3118 (abort) ? &preempt_and_abort_list :
3122 if (!calling_it_nexus)
3123 core_scsi3_ua_allocate(pr_reg_nacl,
3124 pr_res_mapped_lun, 0x2A,
3125 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3127 spin_unlock(&pr_tmpl->registration_lock);
3129 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3130 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3131 * RESERVATION KEY field to a value that does not match any
3132 * registered reservation key, then the device server shall
3133 * complete the command with RESERVATION CONFLICT status.
3135 if (!released_regs) {
3136 spin_unlock(&dev->dev_reservation_lock);
3137 core_scsi3_put_pr_reg(pr_reg_n);
3138 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3142 * For an existing all registrants type reservation
3143 * with a zero SA rservation key, preempt the existing
3144 * reservation with the new PR type and scope.
3146 if (pr_res_holder && all_reg && !(sa_res_key)) {
3147 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3148 (abort) ? &preempt_and_abort_list : NULL,
3149 type, scope, abort);
3152 core_scsi3_release_preempt_and_abort(
3153 &preempt_and_abort_list, pr_reg_n);
3155 spin_unlock(&dev->dev_reservation_lock);
3157 if (pr_tmpl->pr_aptpl_active) {
3158 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3159 &pr_reg_n->pr_aptpl_buf[0],
3160 pr_tmpl->pr_aptpl_buf_len);
3162 pr_debug("SPC-3 PR: Updated APTPL"
3163 " metadata for PREEMPT%s\n", (abort) ?
3167 core_scsi3_put_pr_reg(pr_reg_n);
3168 core_scsi3_pr_generation(cmd->se_dev);
3172 * The PREEMPTing SA reservation key matches that of the
3173 * existing persistent reservation, first, we check if
3174 * we are preempting our own reservation.
3175 * From spc4r17, section 5.7.11.4.3 Preempting
3176 * persistent reservations and registration handling
3178 * If an all registrants persistent reservation is not
3179 * present, it is not an error for the persistent
3180 * reservation holder to preempt itself (i.e., a
3181 * PERSISTENT RESERVE OUT with a PREEMPT service action
3182 * or a PREEMPT AND ABORT service action with the
3183 * SERVICE ACTION RESERVATION KEY value equal to the
3184 * persistent reservation holder's reservation key that
3185 * is received from the persistent reservation holder).
3186 * In that case, the device server shall establish the
3187 * new persistent reservation and maintain the
3190 prh_type = pr_res_holder->pr_res_type;
3191 prh_scope = pr_res_holder->pr_res_scope;
3193 * If the SERVICE ACTION RESERVATION KEY field identifies a
3194 * persistent reservation holder (see 5.7.10), the device
3195 * server shall perform a preempt by doing the following as
3196 * an uninterrupted series of actions:
3198 * a) Release the persistent reservation for the holder
3199 * identified by the SERVICE ACTION RESERVATION KEY field;
3201 if (pr_reg_n != pr_res_holder)
3202 __core_scsi3_complete_pro_release(dev,
3203 pr_res_holder->pr_reg_nacl,
3204 dev->dev_pr_res_holder, 0);
3206 * b) Remove the registrations for all I_T nexuses identified
3207 * by the SERVICE ACTION RESERVATION KEY field, except the
3208 * I_T nexus that is being used for the PERSISTENT RESERVE
3209 * OUT command. If an all registrants persistent reservation
3210 * is present and the SERVICE ACTION RESERVATION KEY field
3211 * is set to zero, then all registrations shall be removed
3212 * except for that of the I_T nexus that is being used for
3213 * the PERSISTENT RESERVE OUT command;
3215 spin_lock(&pr_tmpl->registration_lock);
3216 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3217 &pr_tmpl->registration_list, pr_reg_list) {
3219 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3220 if (calling_it_nexus)
3223 if (pr_reg->pr_res_key != sa_res_key)
3226 pr_reg_nacl = pr_reg->pr_reg_nacl;
3227 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3228 __core_scsi3_free_registration(dev, pr_reg,
3229 (abort) ? &preempt_and_abort_list : NULL,
3232 * e) Establish a unit attention condition for the initiator
3233 * port associated with every I_T nexus that lost its
3234 * persistent reservation and/or registration, with the
3235 * additional sense code set to REGISTRATIONS PREEMPTED;
3237 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3238 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3240 spin_unlock(&pr_tmpl->registration_lock);
3242 * c) Establish a persistent reservation for the preempting
3243 * I_T nexus using the contents of the SCOPE and TYPE fields;
3245 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3246 (abort) ? &preempt_and_abort_list : NULL,
3247 type, scope, abort);
3249 * d) Process tasks as defined in 5.7.1;
3251 * f) If the type or scope has changed, then for every I_T nexus
3252 * whose reservation key was not removed, except for the I_T
3253 * nexus on which the PERSISTENT RESERVE OUT command was
3254 * received, the device server shall establish a unit
3255 * attention condition for the initiator port associated with
3256 * that I_T nexus, with the additional sense code set to
3257 * RESERVATIONS RELEASED. If the type or scope have not
3258 * changed, then no unit attention condition(s) shall be
3259 * established for this reason.
3261 if ((prh_type != type) || (prh_scope != scope)) {
3262 spin_lock(&pr_tmpl->registration_lock);
3263 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3264 &pr_tmpl->registration_list, pr_reg_list) {
3266 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3267 if (calling_it_nexus)
3270 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3271 pr_reg->pr_res_mapped_lun, 0x2A,
3272 ASCQ_2AH_RESERVATIONS_RELEASED);
3274 spin_unlock(&pr_tmpl->registration_lock);
3276 spin_unlock(&dev->dev_reservation_lock);
3278 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3279 * All received CDBs for the matching existing reservation and
3280 * registrations undergo ABORT_TASK logic.
3282 * From there, core_scsi3_release_preempt_and_abort() will
3283 * release every registration in the list (which have already
3284 * been removed from the primary pr_reg list), except the
3285 * new persistent reservation holder, the calling Initiator Port.
3288 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3289 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3293 if (pr_tmpl->pr_aptpl_active) {
3294 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3295 &pr_reg_n->pr_aptpl_buf[0],
3296 pr_tmpl->pr_aptpl_buf_len);
3298 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3299 "%s\n", (abort) ? "_AND_ABORT" : "");
3302 core_scsi3_put_pr_reg(pr_reg_n);
3303 core_scsi3_pr_generation(cmd->se_dev);
3307 static int core_scsi3_emulate_pro_preempt(
3318 case PR_TYPE_WRITE_EXCLUSIVE:
3319 case PR_TYPE_EXCLUSIVE_ACCESS:
3320 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3321 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3322 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3323 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3324 ret = core_scsi3_pro_preempt(cmd, type, scope,
3325 res_key, sa_res_key, abort);
3328 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3329 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3330 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3338 static int core_scsi3_emulate_pro_register_and_move(
3345 struct se_session *se_sess = cmd->se_sess;
3346 struct se_device *dev = cmd->se_dev;
3347 struct se_dev_entry *dest_se_deve = NULL;
3348 struct se_lun *se_lun = cmd->se_lun;
3349 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3350 struct se_port *se_port;
3351 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3352 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3353 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3354 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3356 unsigned char *initiator_str;
3357 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3358 u32 tid_len, tmp_tid_len;
3359 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3360 unsigned short rtpi;
3361 unsigned char proto_ident;
3363 if (!se_sess || !se_lun) {
3364 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3365 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3368 memset(dest_iport, 0, 64);
3369 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3370 se_tpg = se_sess->se_tpg;
3371 tf_ops = se_tpg->se_tpg_tfo;
3373 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3374 * Register behaviors for a REGISTER AND MOVE service action
3376 * Locate the existing *pr_reg via struct se_node_acl pointers
3378 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3381 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3382 " *pr_reg for REGISTER_AND_MOVE\n");
3383 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3387 * The provided reservation key much match the existing reservation key
3388 * provided during this initiator's I_T nexus registration.
3390 if (res_key != pr_reg->pr_res_key) {
3391 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3392 " res_key: 0x%016Lx does not match existing SA REGISTER"
3393 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3394 core_scsi3_put_pr_reg(pr_reg);
3395 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3399 * The service active reservation key needs to be non zero
3402 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3404 core_scsi3_put_pr_reg(pr_reg);
3405 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3410 * Determine the Relative Target Port Identifier where the reservation
3411 * will be moved to for the TransportID containing SCSI initiator WWN
3414 buf = transport_kmap_data_sg(cmd);
3415 rtpi = (buf[18] & 0xff) << 8;
3416 rtpi |= buf[19] & 0xff;
3417 tid_len = (buf[20] & 0xff) << 24;
3418 tid_len |= (buf[21] & 0xff) << 16;
3419 tid_len |= (buf[22] & 0xff) << 8;
3420 tid_len |= buf[23] & 0xff;
3421 transport_kunmap_data_sg(cmd);
3424 if ((tid_len + 24) != cmd->data_length) {
3425 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3426 " does not equal CDB data_length: %u\n", tid_len,
3428 core_scsi3_put_pr_reg(pr_reg);
3429 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3433 spin_lock(&dev->se_port_lock);
3434 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3435 if (se_port->sep_rtpi != rtpi)
3437 dest_se_tpg = se_port->sep_tpg;
3440 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3444 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3445 smp_mb__after_atomic_inc();
3446 spin_unlock(&dev->se_port_lock);
3448 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3450 pr_err("core_scsi3_tpg_depend_item() failed"
3451 " for dest_se_tpg\n");
3452 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3453 smp_mb__after_atomic_dec();
3454 core_scsi3_put_pr_reg(pr_reg);
3455 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3459 spin_lock(&dev->se_port_lock);
3462 spin_unlock(&dev->se_port_lock);
3464 if (!dest_se_tpg || !dest_tf_ops) {
3465 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3466 " fabric ops from Relative Target Port Identifier:"
3468 core_scsi3_put_pr_reg(pr_reg);
3469 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3473 buf = transport_kmap_data_sg(cmd);
3474 proto_ident = (buf[24] & 0x0f);
3476 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3477 " 0x%02x\n", proto_ident);
3479 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3480 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3481 " proto_ident: 0x%02x does not match ident: 0x%02x"
3482 " from fabric: %s\n", proto_ident,
3483 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3484 dest_tf_ops->get_fabric_name());
3485 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3489 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3490 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3491 " containg a valid tpg_parse_pr_out_transport_id"
3492 " function pointer\n");
3493 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3497 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3498 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3499 if (!initiator_str) {
3500 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3501 " initiator_str from Transport ID\n");
3502 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3507 transport_kunmap_data_sg(cmd);
3510 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3511 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3512 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3515 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3516 * action specifies a TransportID that is the same as the initiator port
3517 * of the I_T nexus for the command received, then the command shall
3518 * be terminated with CHECK CONDITION status, with the sense key set to
3519 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3520 * IN PARAMETER LIST.
3522 pr_reg_nacl = pr_reg->pr_reg_nacl;
3523 matching_iname = (!strcmp(initiator_str,
3524 pr_reg_nacl->initiatorname)) ? 1 : 0;
3525 if (!matching_iname)
3526 goto after_iport_check;
3528 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3529 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3530 " matches: %s on received I_T Nexus\n", initiator_str,
3531 pr_reg_nacl->initiatorname);
3532 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3536 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3537 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3538 " matches: %s %s on received I_T Nexus\n",
3539 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3540 pr_reg->pr_reg_isid);
3541 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3547 * Locate the destination struct se_node_acl from the received Transport ID
3549 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3550 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3552 if (dest_node_acl) {
3553 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3554 smp_mb__after_atomic_inc();
3556 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3558 if (!dest_node_acl) {
3559 pr_err("Unable to locate %s dest_node_acl for"
3560 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3562 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3566 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3568 pr_err("core_scsi3_nodeacl_depend_item() for"
3569 " dest_node_acl\n");
3570 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3571 smp_mb__after_atomic_dec();
3572 dest_node_acl = NULL;
3573 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3578 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3579 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3580 dest_node_acl->initiatorname);
3583 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3586 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3587 if (!dest_se_deve) {
3588 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3589 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3590 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3595 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3597 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3598 atomic_dec(&dest_se_deve->pr_ref_count);
3599 smp_mb__after_atomic_dec();
3600 dest_se_deve = NULL;
3601 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3606 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3607 " ACL for dest_se_deve->mapped_lun: %u\n",
3608 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3609 dest_se_deve->mapped_lun);
3612 * A persistent reservation needs to already existing in order to
3613 * successfully complete the REGISTER_AND_MOVE service action..
3615 spin_lock(&dev->dev_reservation_lock);
3616 pr_res_holder = dev->dev_pr_res_holder;
3617 if (!pr_res_holder) {
3618 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3619 " currently held\n");
3620 spin_unlock(&dev->dev_reservation_lock);
3621 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3626 * The received on I_T Nexus must be the reservation holder.
3628 * From spc4r17 section 5.7.8 Table 50 --
3629 * Register behaviors for a REGISTER AND MOVE service action
3631 if (pr_res_holder != pr_reg) {
3632 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3633 " Nexus is not reservation holder\n");
3634 spin_unlock(&dev->dev_reservation_lock);
3635 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3640 * From spc4r17 section 5.7.8: registering and moving reservation
3642 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3643 * action is received and the established persistent reservation is a
3644 * Write Exclusive - All Registrants type or Exclusive Access -
3645 * All Registrants type reservation, then the command shall be completed
3646 * with RESERVATION CONFLICT status.
3648 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3649 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3650 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3651 " reservation for type: %s\n",
3652 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3653 spin_unlock(&dev->dev_reservation_lock);
3654 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3658 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3660 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3662 type = pr_res_holder->pr_res_type;
3663 scope = pr_res_holder->pr_res_type;
3665 * c) Associate the reservation key specified in the SERVICE ACTION
3666 * RESERVATION KEY field with the I_T nexus specified as the
3667 * destination of the register and move, where:
3668 * A) The I_T nexus is specified by the TransportID and the
3669 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3670 * B) Regardless of the TransportID format used, the association for
3671 * the initiator port is based on either the initiator port name
3672 * (see 3.1.71) on SCSI transport protocols where port names are
3673 * required or the initiator port identifier (see 3.1.70) on SCSI
3674 * transport protocols where port names are not required;
3675 * d) Register the reservation key specified in the SERVICE ACTION
3676 * RESERVATION KEY field;
3677 * e) Retain the reservation key specified in the SERVICE ACTION
3678 * RESERVATION KEY field and associated information;
3680 * Also, It is not an error for a REGISTER AND MOVE service action to
3681 * register an I_T nexus that is already registered with the same
3682 * reservation key or a different reservation key.
3684 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3687 ret = core_scsi3_alloc_registration(cmd->se_dev,
3688 dest_node_acl, dest_se_deve, iport_ptr,
3689 sa_res_key, 0, aptpl, 2, 1);
3691 spin_unlock(&dev->dev_reservation_lock);
3692 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3696 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3701 * f) Release the persistent reservation for the persistent reservation
3702 * holder (i.e., the I_T nexus on which the
3704 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3705 dev->dev_pr_res_holder, 0);
3707 * g) Move the persistent reservation to the specified I_T nexus using
3708 * the same scope and type as the persistent reservation released in
3711 dev->dev_pr_res_holder = dest_pr_reg;
3712 dest_pr_reg->pr_res_holder = 1;
3713 dest_pr_reg->pr_res_type = type;
3714 pr_reg->pr_res_scope = scope;
3715 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3716 PR_REG_ISID_ID_LEN);
3718 * Increment PRGeneration for existing registrations..
3721 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3722 spin_unlock(&dev->dev_reservation_lock);
3724 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3725 " created new reservation holder TYPE: %s on object RTPI:"
3726 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3727 core_scsi3_pr_dump_type(type), rtpi,
3728 dest_pr_reg->pr_res_generation);
3729 pr_debug("SPC-3 PR Successfully moved reservation from"
3730 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3731 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3732 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3733 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3736 * It is now safe to release configfs group dependencies for destination
3737 * of Transport ID Initiator Device/Port Identifier
3739 core_scsi3_lunacl_undepend_item(dest_se_deve);
3740 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3741 core_scsi3_tpg_undepend_item(dest_se_tpg);
3743 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3744 * nexus on which PERSISTENT RESERVE OUT command was received.
3747 spin_lock(&pr_tmpl->registration_lock);
3748 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3749 spin_unlock(&pr_tmpl->registration_lock);
3751 core_scsi3_put_pr_reg(pr_reg);
3754 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3755 * write out the updated metadata to struct file for this SCSI device.
3758 pr_tmpl->pr_aptpl_active = 0;
3759 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3760 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3761 " REGISTER_AND_MOVE\n");
3763 pr_tmpl->pr_aptpl_active = 1;
3764 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3765 &dest_pr_reg->pr_aptpl_buf[0],
3766 pr_tmpl->pr_aptpl_buf_len);
3768 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3769 " REGISTER_AND_MOVE\n");
3772 transport_kunmap_data_sg(cmd);
3774 core_scsi3_put_pr_reg(dest_pr_reg);
3778 transport_kunmap_data_sg(cmd);
3780 core_scsi3_lunacl_undepend_item(dest_se_deve);
3782 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3783 core_scsi3_tpg_undepend_item(dest_se_tpg);
3784 core_scsi3_put_pr_reg(pr_reg);
3788 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3790 unsigned int __v1, __v2;
3792 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3793 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3795 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3799 * See spc4r17 section 6.14 Table 170
3801 int target_scsi3_emulate_pr_out(struct se_task *task)
3803 struct se_cmd *cmd = task->task_se_cmd;
3804 unsigned char *cdb = &cmd->t_task_cdb[0];
3806 u64 res_key, sa_res_key;
3807 int sa, scope, type, aptpl;
3808 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3812 * Following spc2r20 5.5.1 Reservations overview:
3814 * If a logical unit has been reserved by any RESERVE command and is
3815 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3816 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3817 * initiator or service action and shall terminate with a RESERVATION
3820 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3821 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3822 " SPC-2 reservation is held, returning"
3823 " RESERVATION_CONFLICT\n");
3824 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3830 * FIXME: A NULL struct se_session pointer means an this is not coming from
3831 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3833 if (!cmd->se_sess) {
3834 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3838 if (cmd->data_length < 24) {
3839 pr_warn("SPC-PR: Received PR OUT parameter list"
3840 " length too small: %u\n", cmd->data_length);
3841 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3846 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3848 sa = (cdb[1] & 0x1f);
3849 scope = (cdb[2] & 0xf0);
3850 type = (cdb[2] & 0x0f);
3852 buf = transport_kmap_data_sg(cmd);
3854 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3856 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3857 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3859 * REGISTER_AND_MOVE uses a different SA parameter list containing
3860 * SCSI TransportIDs.
3862 if (sa != PRO_REGISTER_AND_MOVE) {
3863 spec_i_pt = (buf[20] & 0x08);
3864 all_tg_pt = (buf[20] & 0x04);
3865 aptpl = (buf[20] & 0x01);
3867 aptpl = (buf[17] & 0x01);
3868 unreg = (buf[17] & 0x02);
3870 transport_kunmap_data_sg(cmd);
3874 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3876 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3877 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3883 * From spc4r17 section 6.14:
3885 * If the SPEC_I_PT bit is set to zero, the service action is not
3886 * REGISTER AND MOVE, and the parameter list length is not 24, then
3887 * the command shall be terminated with CHECK CONDITION status, with
3888 * the sense key set to ILLEGAL REQUEST, and the additional sense
3889 * code set to PARAMETER LIST LENGTH ERROR.
3891 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3892 (cmd->data_length != 24)) {
3893 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3894 " list length: %u\n", cmd->data_length);
3895 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3900 * (core_scsi3_emulate_pro_* function parameters
3901 * are defined by spc4r17 Table 174:
3902 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3906 ret = core_scsi3_emulate_pro_register(cmd,
3907 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3910 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3913 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3916 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3919 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3920 res_key, sa_res_key, 0);
3922 case PRO_PREEMPT_AND_ABORT:
3923 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3924 res_key, sa_res_key, 1);
3926 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3927 ret = core_scsi3_emulate_pro_register(cmd,
3928 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3930 case PRO_REGISTER_AND_MOVE:
3931 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3932 sa_res_key, aptpl, unreg);
3935 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3936 " action: 0x%02x\n", cdb[1] & 0x1f);
3937 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3944 task->task_scsi_status = GOOD;
3945 transport_complete_task(task, 1);
3951 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3953 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3955 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3957 struct se_device *se_dev = cmd->se_dev;
3958 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
3959 struct t10_pr_registration *pr_reg;
3961 u32 add_len = 0, off = 8;
3963 if (cmd->data_length < 8) {
3964 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3965 " too small\n", cmd->data_length);
3966 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3970 buf = transport_kmap_data_sg(cmd);
3971 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3972 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3973 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3974 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
3976 spin_lock(&su_dev->t10_pr.registration_lock);
3977 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
3980 * Check for overflow of 8byte PRI READ_KEYS payload and
3981 * next reservation key list descriptor.
3983 if ((add_len + 8) > (cmd->data_length - 8))
3986 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3987 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3988 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3989 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3990 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3991 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3992 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3993 buf[off++] = (pr_reg->pr_res_key & 0xff);
3997 spin_unlock(&su_dev->t10_pr.registration_lock);
3999 buf[4] = ((add_len >> 24) & 0xff);
4000 buf[5] = ((add_len >> 16) & 0xff);
4001 buf[6] = ((add_len >> 8) & 0xff);
4002 buf[7] = (add_len & 0xff);
4004 transport_kunmap_data_sg(cmd);
4010 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4012 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4014 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4016 struct se_device *se_dev = cmd->se_dev;
4017 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4018 struct t10_pr_registration *pr_reg;
4021 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4023 if (cmd->data_length < 8) {
4024 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4025 " too small\n", cmd->data_length);
4026 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4030 buf = transport_kmap_data_sg(cmd);
4031 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4032 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4033 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4034 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4036 spin_lock(&se_dev->dev_reservation_lock);
4037 pr_reg = se_dev->dev_pr_res_holder;
4040 * Set the hardcoded Additional Length
4042 buf[4] = ((add_len >> 24) & 0xff);
4043 buf[5] = ((add_len >> 16) & 0xff);
4044 buf[6] = ((add_len >> 8) & 0xff);
4045 buf[7] = (add_len & 0xff);
4047 if (cmd->data_length < 22)
4051 * Set the Reservation key.
4053 * From spc4r17, section 5.7.10:
4054 * A persistent reservation holder has its reservation key
4055 * returned in the parameter data from a PERSISTENT
4056 * RESERVE IN command with READ RESERVATION service action as
4058 * a) For a persistent reservation of the type Write Exclusive
4059 * - All Registrants or Exclusive Access  All Regitrants,
4060 * the reservation key shall be set to zero; or
4061 * b) For all other persistent reservation types, the
4062 * reservation key shall be set to the registered
4063 * reservation key for the I_T nexus that holds the
4064 * persistent reservation.
4066 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4067 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4070 pr_res_key = pr_reg->pr_res_key;
4072 buf[8] = ((pr_res_key >> 56) & 0xff);
4073 buf[9] = ((pr_res_key >> 48) & 0xff);
4074 buf[10] = ((pr_res_key >> 40) & 0xff);
4075 buf[11] = ((pr_res_key >> 32) & 0xff);
4076 buf[12] = ((pr_res_key >> 24) & 0xff);
4077 buf[13] = ((pr_res_key >> 16) & 0xff);
4078 buf[14] = ((pr_res_key >> 8) & 0xff);
4079 buf[15] = (pr_res_key & 0xff);
4081 * Set the SCOPE and TYPE
4083 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4084 (pr_reg->pr_res_type & 0x0f);
4088 spin_unlock(&se_dev->dev_reservation_lock);
4089 transport_kunmap_data_sg(cmd);
4095 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4097 * See spc4r17 section 6.13.4 Table 165
4099 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4101 struct se_device *dev = cmd->se_dev;
4102 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
4104 u16 add_len = 8; /* Hardcoded to 8. */
4106 if (cmd->data_length < 6) {
4107 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4108 " %u too small\n", cmd->data_length);
4109 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4113 buf = transport_kmap_data_sg(cmd);
4115 buf[0] = ((add_len << 8) & 0xff);
4116 buf[1] = (add_len & 0xff);
4117 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4118 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4119 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4120 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4122 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4123 * set the TMV: Task Mask Valid bit.
4127 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4129 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4131 * PTPL_A: Persistence across Target Power Loss Active bit
4133 if (pr_tmpl->pr_aptpl_active)
4136 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4138 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4139 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4140 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4141 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4142 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4143 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4145 transport_kunmap_data_sg(cmd);
4151 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4153 * See spc4r17 section 6.13.5 Table 168 and 169
4155 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4157 struct se_device *se_dev = cmd->se_dev;
4158 struct se_node_acl *se_nacl;
4159 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4160 struct se_portal_group *se_tpg;
4161 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4162 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
4164 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4165 u32 off = 8; /* off into first Full Status descriptor */
4166 int format_code = 0;
4168 if (cmd->data_length < 8) {
4169 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4170 " too small\n", cmd->data_length);
4171 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4175 buf = transport_kmap_data_sg(cmd);
4177 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4178 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4179 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4180 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4182 spin_lock(&pr_tmpl->registration_lock);
4183 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4184 &pr_tmpl->registration_list, pr_reg_list) {
4186 se_nacl = pr_reg->pr_reg_nacl;
4187 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4190 atomic_inc(&pr_reg->pr_res_holders);
4191 smp_mb__after_atomic_inc();
4192 spin_unlock(&pr_tmpl->registration_lock);
4194 * Determine expected length of $FABRIC_MOD specific
4195 * TransportID full status descriptor..
4197 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4198 se_tpg, se_nacl, pr_reg, &format_code);
4200 if ((exp_desc_len + add_len) > cmd->data_length) {
4201 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4202 " out of buffer: %d\n", cmd->data_length);
4203 spin_lock(&pr_tmpl->registration_lock);
4204 atomic_dec(&pr_reg->pr_res_holders);
4205 smp_mb__after_atomic_dec();
4209 * Set RESERVATION KEY
4211 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4212 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4213 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4214 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4215 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4216 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4217 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4218 buf[off++] = (pr_reg->pr_res_key & 0xff);
4219 off += 4; /* Skip Over Reserved area */
4222 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4224 if (pr_reg->pr_reg_all_tg_pt)
4227 * The struct se_lun pointer will be present for the
4228 * reservation holder for PR_HOLDER bit.
4230 * Also, if this registration is the reservation
4231 * holder, fill in SCOPE and TYPE in the next byte.
4233 if (pr_reg->pr_res_holder) {
4235 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4236 (pr_reg->pr_res_type & 0x0f);
4240 off += 4; /* Skip over reserved area */
4242 * From spc4r17 6.3.15:
4244 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4245 * IDENTIFIER field contains the relative port identifier (see
4246 * 3.1.120) of the target port that is part of the I_T nexus
4247 * described by this full status descriptor. If the ALL_TG_PT
4248 * bit is set to one, the contents of the RELATIVE TARGET PORT
4249 * IDENTIFIER field are not defined by this standard.
4251 if (!pr_reg->pr_reg_all_tg_pt) {
4252 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4254 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4255 buf[off++] = (port->sep_rtpi & 0xff);
4257 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4260 * Now, have the $FABRIC_MOD fill in the protocol identifier
4262 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4263 se_nacl, pr_reg, &format_code, &buf[off+4]);
4265 spin_lock(&pr_tmpl->registration_lock);
4266 atomic_dec(&pr_reg->pr_res_holders);
4267 smp_mb__after_atomic_dec();
4269 * Set the ADDITIONAL DESCRIPTOR LENGTH
4271 buf[off++] = ((desc_len >> 24) & 0xff);
4272 buf[off++] = ((desc_len >> 16) & 0xff);
4273 buf[off++] = ((desc_len >> 8) & 0xff);
4274 buf[off++] = (desc_len & 0xff);
4276 * Size of full desctipor header minus TransportID
4277 * containing $FABRIC_MOD specific) initiator device/port
4280 * See spc4r17 Section 6.13.5 Table 169
4282 add_desc_len = (24 + desc_len);
4285 add_len += add_desc_len;
4287 spin_unlock(&pr_tmpl->registration_lock);
4289 * Set ADDITIONAL_LENGTH
4291 buf[4] = ((add_len >> 24) & 0xff);
4292 buf[5] = ((add_len >> 16) & 0xff);
4293 buf[6] = ((add_len >> 8) & 0xff);
4294 buf[7] = (add_len & 0xff);
4296 transport_kunmap_data_sg(cmd);
4301 int target_scsi3_emulate_pr_in(struct se_task *task)
4303 struct se_cmd *cmd = task->task_se_cmd;
4307 * Following spc2r20 5.5.1 Reservations overview:
4309 * If a logical unit has been reserved by any RESERVE command and is
4310 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4311 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4312 * initiator or service action and shall terminate with a RESERVATION
4315 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4316 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4317 " SPC-2 reservation is held, returning"
4318 " RESERVATION_CONFLICT\n");
4319 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4323 switch (cmd->t_task_cdb[1] & 0x1f) {
4325 ret = core_scsi3_pri_read_keys(cmd);
4327 case PRI_READ_RESERVATION:
4328 ret = core_scsi3_pri_read_reservation(cmd);
4330 case PRI_REPORT_CAPABILITIES:
4331 ret = core_scsi3_pri_report_capabilities(cmd);
4333 case PRI_READ_FULL_STATUS:
4334 ret = core_scsi3_pri_read_full_status(cmd);
4337 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4338 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4339 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4345 task->task_scsi_status = GOOD;
4346 transport_complete_task(task, 1);
4351 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4356 static int core_pt_seq_non_holder(
4364 int core_setup_reservations(struct se_device *dev, int force_pt)
4366 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
4367 struct t10_reservation *rest = &su_dev->t10_pr;
4369 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4370 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4371 * cause a problem because libata and some SATA RAID HBAs appear
4372 * under Linux/SCSI, but to emulate reservations themselves.
4374 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4375 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
4376 rest->res_type = SPC_PASSTHROUGH;
4377 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4378 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4379 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4380 " emulation\n", dev->transport->name);
4384 * If SPC-3 or above is reported by real or emulated struct se_device,
4385 * use emulated Persistent Reservations.
4387 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4388 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4389 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4390 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4391 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4392 " emulation\n", dev->transport->name);
4394 rest->res_type = SPC2_RESERVATIONS;
4395 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4396 rest->pr_ops.t10_seq_non_holder =
4397 &core_scsi2_reservation_seq_non_holder;
4398 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4399 dev->transport->name);