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 * (c) Copyright 2009-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/vmalloc.h>
31 #include <linux/file.h>
32 #include <scsi/scsi_proto.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>
39 #include "target_core_internal.h"
40 #include "target_core_pr.h"
41 #include "target_core_ua.h"
44 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
46 struct pr_transport_id_holder {
47 struct t10_pr_registration *dest_pr_reg;
48 struct se_portal_group *dest_tpg;
49 struct se_node_acl *dest_node_acl;
50 struct se_dev_entry *dest_se_deve;
51 struct list_head dest_list;
54 void core_pr_dump_initiator_port(
55 struct t10_pr_registration *pr_reg,
59 if (!pr_reg->isid_present_at_reg) {
64 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
69 REGISTER_AND_IGNORE_EXISTING_KEY,
78 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
79 struct t10_pr_registration *, int, int);
81 static int is_reservation_holder(
82 struct t10_pr_registration *pr_res_holder,
83 struct t10_pr_registration *pr_reg)
88 pr_res_type = pr_res_holder->pr_res_type;
90 return pr_res_holder == pr_reg ||
91 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
92 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
98 target_scsi2_reservation_check(struct se_cmd *cmd)
100 struct se_device *dev = cmd->se_dev;
101 struct se_session *sess = cmd->se_sess;
103 switch (cmd->t_task_cdb[0]) {
112 if (!dev->dev_reserved_node_acl || !sess)
115 if (dev->dev_reserved_node_acl != sess->se_node_acl)
116 return TCM_RESERVATION_CONFLICT;
118 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
119 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
120 return TCM_RESERVATION_CONFLICT;
126 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
127 struct se_node_acl *, struct se_session *);
128 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
130 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
132 struct se_session *se_sess = cmd->se_sess;
133 struct se_device *dev = cmd->se_dev;
134 struct t10_pr_registration *pr_reg;
135 struct t10_reservation *pr_tmpl = &dev->t10_pr;
138 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
142 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
145 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
146 * status, but no reservation shall be established and the
147 * persistent reservation shall not be changed, if the command
148 * is received from a) and b) below.
150 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
151 * status, but the persistent reservation shall not be released,
152 * if the command is received from a) and b)
154 * a) An I_T nexus that is a persistent reservation holder; or
155 * b) An I_T nexus that is registered if a registrants only or
156 * all registrants type persistent reservation is present.
158 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
159 * RELEASE(6) command, or RELEASE(10) command shall be processed
160 * as defined in SPC-2.
162 if (pr_reg->pr_res_holder) {
163 core_scsi3_put_pr_reg(pr_reg);
166 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
167 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
168 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
169 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
170 core_scsi3_put_pr_reg(pr_reg);
173 core_scsi3_put_pr_reg(pr_reg);
177 * Following spc2r20 5.5.1 Reservations overview:
179 * If a logical unit has executed a PERSISTENT RESERVE OUT
180 * command with the REGISTER or the REGISTER AND IGNORE
181 * EXISTING KEY service action and is still registered by any
182 * initiator, all RESERVE commands and all RELEASE commands
183 * regardless of initiator shall conflict and shall terminate
184 * with a RESERVATION CONFLICT status.
186 spin_lock(&pr_tmpl->registration_lock);
187 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
188 spin_unlock(&pr_tmpl->registration_lock);
192 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
193 " while active SPC-3 registrations exist,"
194 " returning RESERVATION_CONFLICT\n");
202 target_scsi2_reservation_release(struct se_cmd *cmd)
204 struct se_device *dev = cmd->se_dev;
205 struct se_session *sess = cmd->se_sess;
206 struct se_portal_group *tpg;
209 if (!sess || !sess->se_tpg)
211 rc = target_check_scsi2_reservation_conflict(cmd);
215 return TCM_RESERVATION_CONFLICT;
217 spin_lock(&dev->dev_reservation_lock);
218 if (!dev->dev_reserved_node_acl || !sess)
221 if (dev->dev_reserved_node_acl != sess->se_node_acl)
224 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
227 dev->dev_reserved_node_acl = NULL;
228 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
229 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
230 dev->dev_res_bin_isid = 0;
231 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
234 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
235 " MAPPED LUN: %llu for %s\n",
236 tpg->se_tpg_tfo->get_fabric_name(),
237 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
238 sess->se_node_acl->initiatorname);
241 spin_unlock(&dev->dev_reservation_lock);
243 target_complete_cmd(cmd, GOOD);
248 target_scsi2_reservation_reserve(struct se_cmd *cmd)
250 struct se_device *dev = cmd->se_dev;
251 struct se_session *sess = cmd->se_sess;
252 struct se_portal_group *tpg;
253 sense_reason_t ret = 0;
256 if ((cmd->t_task_cdb[1] & 0x01) &&
257 (cmd->t_task_cdb[1] & 0x02)) {
258 pr_err("LongIO and Obselete Bits set, returning"
259 " ILLEGAL_REQUEST\n");
260 return TCM_UNSUPPORTED_SCSI_OPCODE;
263 * This is currently the case for target_core_mod passthrough struct se_cmd
266 if (!sess || !sess->se_tpg)
268 rc = target_check_scsi2_reservation_conflict(cmd);
273 return 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: %llu %s\n",
282 cmd->se_lun->unpacked_lun,
283 dev->dev_reserved_node_acl->initiatorname);
284 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
285 " from %s \n", cmd->se_lun->unpacked_lun,
287 sess->se_node_acl->initiatorname);
288 ret = TCM_RESERVATION_CONFLICT;
292 dev->dev_reserved_node_acl = sess->se_node_acl;
293 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
294 if (sess->sess_bin_isid != 0) {
295 dev->dev_res_bin_isid = sess->sess_bin_isid;
296 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
298 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
299 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
300 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
301 sess->se_node_acl->initiatorname);
304 spin_unlock(&dev->dev_reservation_lock);
307 target_complete_cmd(cmd, GOOD);
313 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
315 * This function is called by those initiator ports who are *NOT*
316 * the active PR reservation holder when a reservation is present.
318 static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
321 unsigned char *cdb = cmd->t_task_cdb;
322 struct se_session *se_sess = cmd->se_sess;
323 struct se_node_acl *nacl = se_sess->se_node_acl;
325 int registered_nexus = 0, ret = 1; /* Conflict by default */
326 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
327 int we = 0; /* Write Exclusive */
328 int legacy = 0; /* Act like a legacy device and return
329 * RESERVATION CONFLICT on some CDBs */
332 registered_nexus = 0;
334 struct se_dev_entry *se_deve;
337 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
339 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
340 &se_deve->deve_flags);
344 switch (pr_reg_type) {
345 case PR_TYPE_WRITE_EXCLUSIVE:
347 case PR_TYPE_EXCLUSIVE_ACCESS:
349 * Some commands are only allowed for the persistent reservation
353 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
355 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
357 * Some commands are only allowed for registered I_T Nexuses.
361 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
363 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
365 * Each registered I_T Nexus is a reservation holder.
373 * Referenced from spc4r17 table 45 for *NON* PR holder access
376 case SECURITY_PROTOCOL_IN:
377 if (registered_nexus)
385 case RECEIVE_DIAGNOSTIC:
390 if (registered_nexus) {
394 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
396 case PERSISTENT_RESERVE_OUT:
398 * This follows PERSISTENT_RESERVE_OUT service actions that
399 * are allowed in the presence of various reservations.
400 * See spc4r17, table 46
402 switch (cdb[1] & 0x1f) {
405 case PRO_PREEMPT_AND_ABORT:
406 ret = (registered_nexus) ? 0 : 1;
409 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
412 case PRO_REGISTER_AND_MOVE:
417 ret = (registered_nexus) ? 0 : 1;
420 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
421 " action: 0x%02x\n", cdb[1] & 0x1f);
427 /* Handled by CRH=1 in target_scsi2_reservation_release() */
432 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
435 case TEST_UNIT_READY:
436 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
439 switch (cdb[1] & 0x1f) {
440 case MI_MANAGEMENT_PROTOCOL_IN:
441 if (registered_nexus) {
445 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
447 case MI_REPORT_SUPPORTED_OPERATION_CODES:
448 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
453 if (registered_nexus) {
457 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
459 case MI_REPORT_ALIASES:
460 case MI_REPORT_IDENTIFYING_INFORMATION:
461 case MI_REPORT_PRIORITY:
462 case MI_REPORT_TARGET_PGS:
463 case MI_REPORT_TIMESTAMP:
464 ret = 0; /* Allowed */
467 pr_err("Unknown MI Service Action: 0x%02x\n",
472 case ACCESS_CONTROL_IN:
473 case ACCESS_CONTROL_OUT:
476 case SERVICE_ACTION_IN_12:
479 case PERSISTENT_RESERVE_IN:
480 ret = 0; /*/ Allowed CDBs */
487 * Case where the CDB is explicitly allowed in the above switch
490 if (!ret && !other_cdb) {
491 pr_debug("Allowing explicit CDB: 0x%02x for %s"
492 " reservation holder\n", cdb[0],
493 core_scsi3_pr_dump_type(pr_reg_type));
498 * Check if write exclusive initiator ports *NOT* holding the
499 * WRITE_EXCLUSIVE_* reservation.
501 if (we && !registered_nexus) {
502 if (cmd->data_direction == DMA_TO_DEVICE) {
504 * Conflict for write exclusive
506 pr_debug("%s Conflict for unregistered nexus"
507 " %s CDB: 0x%02x to %s reservation\n",
508 transport_dump_cmd_direction(cmd),
509 se_sess->se_node_acl->initiatorname, cdb[0],
510 core_scsi3_pr_dump_type(pr_reg_type));
514 * Allow non WRITE CDBs for all Write Exclusive
515 * PR TYPEs to pass for registered and
516 * non-registered_nexuxes NOT holding the reservation.
518 * We only make noise for the unregisterd nexuses,
519 * as we expect registered non-reservation holding
520 * nexuses to issue CDBs.
523 if (!registered_nexus) {
524 pr_debug("Allowing implicit CDB: 0x%02x"
525 " for %s reservation on unregistered"
527 core_scsi3_pr_dump_type(pr_reg_type));
532 } else if ((reg_only) || (all_reg)) {
533 if (registered_nexus) {
535 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
536 * allow commands from registered nexuses.
539 pr_debug("Allowing implicit CDB: 0x%02x for %s"
540 " reservation\n", cdb[0],
541 core_scsi3_pr_dump_type(pr_reg_type));
545 } else if (we && registered_nexus) {
547 * Reads are allowed for Write Exclusive locks
548 * from all registrants.
550 if (cmd->data_direction == DMA_FROM_DEVICE) {
551 pr_debug("Allowing READ 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 sense_reason_t
568 target_scsi3_pr_reservation_check(struct se_cmd *cmd)
570 struct se_device *dev = cmd->se_dev;
571 struct se_session *sess = cmd->se_sess;
573 bool isid_mismatch = false;
575 if (!dev->dev_pr_res_holder)
578 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
579 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
580 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
581 goto check_nonholder;
583 if (dev->dev_pr_res_holder->isid_present_at_reg) {
584 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
585 sess->sess_bin_isid) {
586 isid_mismatch = true;
587 goto check_nonholder;
594 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
595 return TCM_RESERVATION_CONFLICT;
599 static u32 core_scsi3_pr_generation(struct se_device *dev)
604 * PRGeneration field shall contain the value of a 32-bit wrapping
605 * counter mainted by the device server.
607 * Note that this is done regardless of Active Persist across
608 * Target PowerLoss (APTPL)
610 * See spc4r17 section 6.3.12 READ_KEYS service action
612 spin_lock(&dev->dev_reservation_lock);
613 prg = dev->t10_pr.pr_generation++;
614 spin_unlock(&dev->dev_reservation_lock);
619 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
620 struct se_device *dev,
621 struct se_node_acl *nacl,
623 struct se_dev_entry *dest_deve,
630 struct t10_pr_registration *pr_reg;
632 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
634 pr_err("Unable to allocate struct t10_pr_registration\n");
638 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
639 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
640 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
641 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
642 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
643 atomic_set(&pr_reg->pr_res_holders, 0);
644 pr_reg->pr_reg_nacl = nacl;
646 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
647 * the se_dev_entry->pr_ref will have been already obtained by
648 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
650 * Otherwise, locate se_dev_entry now and obtain a reference until
651 * registration completes in __core_scsi3_add_registration().
654 pr_reg->pr_reg_deve = dest_deve;
657 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
658 if (!pr_reg->pr_reg_deve) {
660 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
661 nacl->initiatorname, mapped_lun);
662 kmem_cache_free(t10_pr_reg_cache, pr_reg);
665 kref_get(&pr_reg->pr_reg_deve->pr_kref);
668 pr_reg->pr_res_mapped_lun = mapped_lun;
669 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
670 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
671 pr_reg->pr_res_key = sa_res_key;
672 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
673 pr_reg->pr_reg_aptpl = aptpl;
675 * If an ISID value for this SCSI Initiator Port exists,
676 * save it to the registration now.
679 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
680 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
681 pr_reg->isid_present_at_reg = 1;
687 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
688 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
691 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
694 static struct t10_pr_registration *__core_scsi3_alloc_registration(
695 struct se_device *dev,
696 struct se_node_acl *nacl,
698 struct se_dev_entry *deve,
705 struct se_dev_entry *deve_tmp;
706 struct se_node_acl *nacl_tmp;
707 struct se_lun_acl *lacl_tmp;
708 struct se_lun *lun_tmp, *next, *dest_lun;
709 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
710 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
713 * Create a registration for the I_T Nexus upon which the
714 * PROUT REGISTER was received.
716 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
717 isid, sa_res_key, all_tg_pt,
722 * Return pointer to pr_reg for ALL_TG_PT=0
727 * Create list of matching SCSI Initiator Port registrations
730 spin_lock(&dev->se_port_lock);
731 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
732 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
734 spin_unlock(&dev->se_port_lock);
736 spin_lock(&lun_tmp->lun_deve_lock);
737 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
739 * This pointer will be NULL for demo mode MappedLUNs
740 * that have not been make explicit via a ConfigFS
741 * MappedLUN group for the SCSI Initiator Node ACL.
743 if (!deve_tmp->se_lun_acl)
746 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
747 lockdep_is_held(&lun_tmp->lun_deve_lock));
748 nacl_tmp = lacl_tmp->se_lun_nacl;
750 * Skip the matching struct se_node_acl that is allocated
753 if (nacl == nacl_tmp)
756 * Only perform PR registrations for target ports on
757 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
760 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
763 * Look for a matching Initiator Node ACL in ASCII format
765 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
768 kref_get(&deve_tmp->pr_kref);
769 spin_unlock(&lun_tmp->lun_deve_lock);
771 * Grab a configfs group dependency that is released
772 * for the exception path at label out: below, or upon
773 * completion of adding ALL_TG_PT=1 registrations in
774 * __core_scsi3_add_registration()
776 ret = core_scsi3_lunacl_depend_item(deve_tmp);
778 pr_err("core_scsi3_lunacl_depend"
780 percpu_ref_put(&lun_tmp->lun_ref);
781 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
785 * Located a matching SCSI Initiator Port on a different
786 * port, allocate the pr_reg_atp and attach it to the
787 * pr_reg->pr_reg_atp_list that will be processed once
788 * the original *pr_reg is processed in
789 * __core_scsi3_add_registration()
791 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
792 atomic_read(&deve_tmp->pr_kref.refcount) != 0);
794 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
795 nacl_tmp, dest_lun, deve_tmp,
796 deve_tmp->mapped_lun, NULL,
797 sa_res_key, all_tg_pt, aptpl);
799 percpu_ref_put(&lun_tmp->lun_ref);
800 core_scsi3_lunacl_undepend_item(deve_tmp);
804 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
805 &pr_reg->pr_reg_atp_list);
806 spin_lock(&lun_tmp->lun_deve_lock);
808 spin_unlock(&lun_tmp->lun_deve_lock);
810 spin_lock(&dev->se_port_lock);
811 percpu_ref_put(&lun_tmp->lun_ref);
813 spin_unlock(&dev->se_port_lock);
817 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
818 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
819 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
820 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
821 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
823 kmem_cache_free(t10_pr_reg_cache, pr_reg);
827 int core_scsi3_alloc_aptpl_registration(
828 struct t10_reservation *pr_tmpl,
830 unsigned char *i_port,
833 unsigned char *t_port,
840 struct t10_pr_registration *pr_reg;
842 if (!i_port || !t_port || !sa_res_key) {
843 pr_err("Illegal parameters for APTPL registration\n");
847 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
849 pr_err("Unable to allocate struct t10_pr_registration\n");
853 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
854 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
855 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
856 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
857 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
858 atomic_set(&pr_reg->pr_res_holders, 0);
859 pr_reg->pr_reg_nacl = NULL;
860 pr_reg->pr_reg_deve = NULL;
861 pr_reg->pr_res_mapped_lun = mapped_lun;
862 pr_reg->pr_aptpl_target_lun = target_lun;
863 pr_reg->pr_res_key = sa_res_key;
864 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
865 pr_reg->pr_reg_aptpl = 1;
866 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
867 pr_reg->pr_res_type = type;
869 * If an ISID value had been saved in APTPL metadata for this
870 * SCSI Initiator Port, restore it now.
873 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
874 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
875 pr_reg->isid_present_at_reg = 1;
878 * Copy the i_port and t_port information from caller.
880 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
881 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
882 pr_reg->pr_reg_tpgt = tpgt;
884 * Set pr_res_holder from caller, the pr_reg who is the reservation
885 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
886 * the Initiator Node LUN ACL from the fabric module is created for
889 pr_reg->pr_res_holder = res_holder;
891 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
892 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
893 " metadata\n", (res_holder) ? "+reservation" : "");
897 static void core_scsi3_aptpl_reserve(
898 struct se_device *dev,
899 struct se_portal_group *tpg,
900 struct se_node_acl *node_acl,
901 struct t10_pr_registration *pr_reg)
903 char i_buf[PR_REG_ISID_ID_LEN];
905 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
906 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
908 spin_lock(&dev->dev_reservation_lock);
909 dev->dev_pr_res_holder = pr_reg;
910 spin_unlock(&dev->dev_reservation_lock);
912 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
913 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
914 tpg->se_tpg_tfo->get_fabric_name(),
915 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
916 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
917 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
918 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
922 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
923 struct t10_pr_registration *, enum register_type, int);
925 static int __core_scsi3_check_aptpl_registration(
926 struct se_device *dev,
927 struct se_portal_group *tpg,
930 struct se_node_acl *nacl,
933 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
934 struct t10_reservation *pr_tmpl = &dev->t10_pr;
935 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
936 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
939 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
940 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
942 * Copy Initiator Port information from struct se_node_acl
944 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
945 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
946 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
947 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
949 * Look for the matching registrations+reservation from those
950 * created from APTPL metadata. Note that multiple registrations
951 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
954 spin_lock(&pr_tmpl->aptpl_reg_lock);
955 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
958 if (!strcmp(pr_reg->pr_iport, i_port) &&
959 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
960 !(strcmp(pr_reg->pr_tport, t_port)) &&
961 (pr_reg->pr_reg_tpgt == tpgt) &&
962 (pr_reg->pr_aptpl_target_lun == target_lun)) {
964 * Obtain the ->pr_reg_deve pointer + reference, that
965 * is released by __core_scsi3_add_registration() below.
968 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
969 if (!pr_reg->pr_reg_deve) {
970 pr_err("Unable to locate PR APTPL %s mapped_lun:"
971 " %llu\n", nacl->initiatorname, mapped_lun);
975 kref_get(&pr_reg->pr_reg_deve->pr_kref);
978 pr_reg->pr_reg_nacl = nacl;
979 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
980 list_del(&pr_reg->pr_reg_aptpl_list);
981 spin_unlock(&pr_tmpl->aptpl_reg_lock);
983 * At this point all of the pointers in *pr_reg will
984 * be setup, so go ahead and add the registration.
986 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
988 * If this registration is the reservation holder,
989 * make that happen now..
991 if (pr_reg->pr_res_holder)
992 core_scsi3_aptpl_reserve(dev, tpg,
995 * Reenable pr_aptpl_active to accept new metadata
996 * updates once the SCSI device is active again..
998 spin_lock(&pr_tmpl->aptpl_reg_lock);
999 pr_tmpl->pr_aptpl_active = 1;
1002 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1007 int core_scsi3_check_aptpl_registration(
1008 struct se_device *dev,
1009 struct se_portal_group *tpg,
1011 struct se_node_acl *nacl,
1014 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1017 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1018 lun->unpacked_lun, nacl,
1022 static void __core_scsi3_dump_registration(
1023 const struct target_core_fabric_ops *tfo,
1024 struct se_device *dev,
1025 struct se_node_acl *nacl,
1026 struct t10_pr_registration *pr_reg,
1027 enum register_type register_type)
1029 struct se_portal_group *se_tpg = nacl->se_tpg;
1030 char i_buf[PR_REG_ISID_ID_LEN];
1032 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1033 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1035 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1036 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
1037 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1038 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1040 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1041 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1042 tfo->tpg_get_tag(se_tpg));
1043 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1044 " Port(s)\n", tfo->get_fabric_name(),
1045 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1046 dev->transport->name);
1047 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1048 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1049 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1050 pr_reg->pr_reg_aptpl);
1053 static void __core_scsi3_add_registration(
1054 struct se_device *dev,
1055 struct se_node_acl *nacl,
1056 struct t10_pr_registration *pr_reg,
1057 enum register_type register_type,
1060 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1061 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1062 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1063 struct se_dev_entry *deve;
1066 * Increment PRgeneration counter for struct se_device upon a successful
1067 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1069 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1070 * action, the struct se_device->dev_reservation_lock will already be held,
1071 * so we do not call core_scsi3_pr_generation() which grabs the lock
1074 pr_reg->pr_res_generation = (register_move) ?
1075 dev->t10_pr.pr_generation++ :
1076 core_scsi3_pr_generation(dev);
1078 spin_lock(&pr_tmpl->registration_lock);
1079 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1081 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1082 spin_unlock(&pr_tmpl->registration_lock);
1084 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1086 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1089 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1090 * allocated in __core_scsi3_alloc_registration()
1092 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1093 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1094 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1096 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1098 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1100 spin_lock(&pr_tmpl->registration_lock);
1101 list_add_tail(&pr_reg_tmp->pr_reg_list,
1102 &pr_tmpl->registration_list);
1104 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1106 spin_unlock(&pr_tmpl->registration_lock);
1108 * Drop configfs group dependency reference and deve->pr_kref
1109 * obtained from __core_scsi3_alloc_registration() code.
1112 deve = pr_reg_tmp->pr_reg_deve;
1114 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1115 core_scsi3_lunacl_undepend_item(deve);
1116 pr_reg_tmp->pr_reg_deve = NULL;
1122 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1125 deve = pr_reg->pr_reg_deve;
1127 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1128 kref_put(&deve->pr_kref, target_pr_kref_release);
1129 pr_reg->pr_reg_deve = NULL;
1134 static int core_scsi3_alloc_registration(
1135 struct se_device *dev,
1136 struct se_node_acl *nacl,
1138 struct se_dev_entry *deve,
1140 unsigned char *isid,
1144 enum register_type register_type,
1147 struct t10_pr_registration *pr_reg;
1149 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1150 isid, sa_res_key, all_tg_pt,
1155 __core_scsi3_add_registration(dev, nacl, pr_reg,
1156 register_type, register_move);
1160 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1161 struct se_device *dev,
1162 struct se_node_acl *nacl,
1163 unsigned char *isid)
1165 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1166 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1167 struct se_portal_group *tpg;
1169 spin_lock(&pr_tmpl->registration_lock);
1170 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1171 &pr_tmpl->registration_list, pr_reg_list) {
1173 * First look for a matching struct se_node_acl
1175 if (pr_reg->pr_reg_nacl != nacl)
1178 tpg = pr_reg->pr_reg_nacl->se_tpg;
1180 * If this registration does NOT contain a fabric provided
1181 * ISID, then we have found a match.
1183 if (!pr_reg->isid_present_at_reg) {
1185 * Determine if this SCSI device server requires that
1186 * SCSI Intiatior TransportID w/ ISIDs is enforced
1187 * for fabric modules (iSCSI) requiring them.
1189 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1190 if (dev->dev_attrib.enforce_pr_isids)
1193 atomic_inc_mb(&pr_reg->pr_res_holders);
1194 spin_unlock(&pr_tmpl->registration_lock);
1198 * If the *pr_reg contains a fabric defined ISID for multi-value
1199 * SCSI Initiator Port TransportIDs, then we expect a valid
1200 * matching ISID to be provided by the local SCSI Initiator Port.
1204 if (strcmp(isid, pr_reg->pr_reg_isid))
1207 atomic_inc_mb(&pr_reg->pr_res_holders);
1208 spin_unlock(&pr_tmpl->registration_lock);
1211 spin_unlock(&pr_tmpl->registration_lock);
1216 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1217 struct se_device *dev,
1218 struct se_node_acl *nacl,
1219 struct se_session *sess)
1221 struct se_portal_group *tpg = nacl->se_tpg;
1222 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1224 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1225 memset(&buf[0], 0, PR_REG_ISID_LEN);
1226 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1231 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1234 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1236 atomic_dec_mb(&pr_reg->pr_res_holders);
1239 static int core_scsi3_check_implicit_release(
1240 struct se_device *dev,
1241 struct t10_pr_registration *pr_reg)
1243 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1244 struct t10_pr_registration *pr_res_holder;
1247 spin_lock(&dev->dev_reservation_lock);
1248 pr_res_holder = dev->dev_pr_res_holder;
1249 if (!pr_res_holder) {
1250 spin_unlock(&dev->dev_reservation_lock);
1253 if (pr_res_holder == pr_reg) {
1255 * Perform an implicit RELEASE if the registration that
1256 * is being released is holding the reservation.
1258 * From spc4r17, section 5.7.11.1:
1260 * e) If the I_T nexus is the persistent reservation holder
1261 * and the persistent reservation is not an all registrants
1262 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1263 * service action or REGISTER AND IGNORE EXISTING KEY
1264 * service action with the SERVICE ACTION RESERVATION KEY
1265 * field set to zero (see 5.7.11.3).
1267 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1270 * For 'All Registrants' reservation types, all existing
1271 * registrations are still processed as reservation holders
1272 * in core_scsi3_pr_seq_non_holder() after the initial
1273 * reservation holder is implicitly released here.
1275 } else if (pr_reg->pr_reg_all_tg_pt &&
1276 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1277 pr_reg->pr_reg_nacl->initiatorname)) &&
1278 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1279 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1280 " UNREGISTER while existing reservation with matching"
1281 " key 0x%016Lx is present from another SCSI Initiator"
1282 " Port\n", pr_reg->pr_res_key);
1285 spin_unlock(&dev->dev_reservation_lock);
1291 * Called with struct t10_reservation->registration_lock held.
1293 static void __core_scsi3_free_registration(
1294 struct se_device *dev,
1295 struct t10_pr_registration *pr_reg,
1296 struct list_head *preempt_and_abort_list,
1298 __releases(&pr_tmpl->registration_lock)
1299 __acquires(&pr_tmpl->registration_lock)
1301 const struct target_core_fabric_ops *tfo =
1302 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1303 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1304 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1305 struct se_dev_entry *deve;
1306 char i_buf[PR_REG_ISID_ID_LEN];
1308 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1309 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1311 if (!list_empty(&pr_reg->pr_reg_list))
1312 list_del(&pr_reg->pr_reg_list);
1314 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1315 * so call core_scsi3_put_pr_reg() to decrement our reference.
1318 core_scsi3_put_pr_reg(pr_reg);
1320 spin_unlock(&pr_tmpl->registration_lock);
1322 * Wait until all reference from any other I_T nexuses for this
1323 * *pr_reg have been released. Because list_del() is called above,
1324 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1325 * count back to zero, and we release *pr_reg.
1327 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1328 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1329 tfo->get_fabric_name());
1334 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1336 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1339 spin_lock(&pr_tmpl->registration_lock);
1340 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1341 " Node: %s%s\n", tfo->get_fabric_name(),
1342 pr_reg->pr_reg_nacl->initiatorname,
1344 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1345 " Port(s)\n", tfo->get_fabric_name(),
1346 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1347 dev->transport->name);
1348 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1349 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1350 pr_reg->pr_res_generation);
1352 if (!preempt_and_abort_list) {
1353 pr_reg->pr_reg_deve = NULL;
1354 pr_reg->pr_reg_nacl = NULL;
1355 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1359 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1360 * are released once the ABORT_TASK_SET has completed..
1362 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1365 void core_scsi3_free_pr_reg_from_nacl(
1366 struct se_device *dev,
1367 struct se_node_acl *nacl)
1369 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1370 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1371 bool free_reg = false;
1373 * If the passed se_node_acl matches the reservation holder,
1374 * release the reservation.
1376 spin_lock(&dev->dev_reservation_lock);
1377 pr_res_holder = dev->dev_pr_res_holder;
1378 if ((pr_res_holder != NULL) &&
1379 (pr_res_holder->pr_reg_nacl == nacl)) {
1380 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1383 spin_unlock(&dev->dev_reservation_lock);
1385 * Release any registration associated with the struct se_node_acl.
1387 spin_lock(&pr_tmpl->registration_lock);
1388 if (pr_res_holder && free_reg)
1389 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1391 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1392 &pr_tmpl->registration_list, pr_reg_list) {
1394 if (pr_reg->pr_reg_nacl != nacl)
1397 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1399 spin_unlock(&pr_tmpl->registration_lock);
1402 void core_scsi3_free_all_registrations(
1403 struct se_device *dev)
1405 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1406 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1408 spin_lock(&dev->dev_reservation_lock);
1409 pr_res_holder = dev->dev_pr_res_holder;
1410 if (pr_res_holder != NULL) {
1411 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1412 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1413 pr_res_holder, 0, 0);
1415 spin_unlock(&dev->dev_reservation_lock);
1417 spin_lock(&pr_tmpl->registration_lock);
1418 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1419 &pr_tmpl->registration_list, pr_reg_list) {
1421 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1423 spin_unlock(&pr_tmpl->registration_lock);
1425 spin_lock(&pr_tmpl->aptpl_reg_lock);
1426 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1427 pr_reg_aptpl_list) {
1428 list_del(&pr_reg->pr_reg_aptpl_list);
1429 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1431 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1434 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1436 return target_depend_item(&tpg->tpg_group.cg_item);
1439 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1441 target_undepend_item(&tpg->tpg_group.cg_item);
1442 atomic_dec_mb(&tpg->tpg_pr_ref_count);
1445 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1447 if (nacl->dynamic_node_acl)
1449 return target_depend_item(&nacl->acl_group.cg_item);
1452 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1454 if (!nacl->dynamic_node_acl)
1455 target_undepend_item(&nacl->acl_group.cg_item);
1456 atomic_dec_mb(&nacl->acl_pr_ref_count);
1459 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1461 struct se_lun_acl *lun_acl;
1462 struct se_node_acl *nacl;
1463 struct se_portal_group *tpg;
1465 * For nacl->dynamic_node_acl=1
1467 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1468 atomic_read(&se_deve->pr_kref.refcount) != 0);
1472 nacl = lun_acl->se_lun_nacl;
1475 return target_depend_item(&lun_acl->se_lun_group.cg_item);
1478 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1480 struct se_lun_acl *lun_acl;
1481 struct se_node_acl *nacl;
1482 struct se_portal_group *tpg;
1484 * For nacl->dynamic_node_acl=1
1486 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1487 atomic_read(&se_deve->pr_kref.refcount) != 0);
1489 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1492 nacl = lun_acl->se_lun_nacl;
1495 target_undepend_item(&lun_acl->se_lun_group.cg_item);
1496 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1499 static sense_reason_t
1500 core_scsi3_decode_spec_i_port(
1502 struct se_portal_group *tpg,
1503 unsigned char *l_isid,
1508 struct se_device *dev = cmd->se_dev;
1509 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1510 struct se_session *se_sess = cmd->se_sess;
1511 struct se_node_acl *dest_node_acl = NULL;
1512 struct se_dev_entry *dest_se_deve = NULL;
1513 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1514 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1515 LIST_HEAD(tid_dest_list);
1516 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1517 unsigned char *buf, *ptr, proto_ident;
1518 const unsigned char *i_str = NULL;
1519 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1521 u32 tpdl, tid_len = 0;
1525 * Allocate a struct pr_transport_id_holder and setup the
1526 * local_node_acl pointer and add to struct list_head tid_dest_list
1527 * for add registration processing in the loop of tid_dest_list below.
1529 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1531 pr_err("Unable to allocate tidh_new\n");
1532 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1534 INIT_LIST_HEAD(&tidh_new->dest_list);
1535 tidh_new->dest_tpg = tpg;
1536 tidh_new->dest_node_acl = se_sess->se_node_acl;
1538 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1539 se_sess->se_node_acl, cmd->se_lun,
1540 NULL, cmd->orig_fe_lun, l_isid,
1541 sa_res_key, all_tg_pt, aptpl);
1542 if (!local_pr_reg) {
1544 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1546 tidh_new->dest_pr_reg = local_pr_reg;
1548 * The local I_T nexus does not hold any configfs dependances,
1549 * so we set tidh_new->dest_se_deve to NULL to prevent the
1550 * configfs_undepend_item() calls in the tid_dest_list loops below.
1552 tidh_new->dest_se_deve = NULL;
1553 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1555 if (cmd->data_length < 28) {
1556 pr_warn("SPC-PR: Received PR OUT parameter list"
1557 " length too small: %u\n", cmd->data_length);
1558 ret = TCM_INVALID_PARAMETER_LIST;
1562 buf = transport_kmap_data_sg(cmd);
1564 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1569 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1570 * first extract TransportID Parameter Data Length, and make sure
1571 * the value matches up to the SCSI expected data transfer length.
1573 tpdl = (buf[24] & 0xff) << 24;
1574 tpdl |= (buf[25] & 0xff) << 16;
1575 tpdl |= (buf[26] & 0xff) << 8;
1576 tpdl |= buf[27] & 0xff;
1578 if ((tpdl + 28) != cmd->data_length) {
1579 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1580 " does not equal CDB data_length: %u\n", tpdl,
1582 ret = TCM_INVALID_PARAMETER_LIST;
1586 * Start processing the received transport IDs using the
1587 * receiving I_T Nexus portal's fabric dependent methods to
1588 * obtain the SCSI Initiator Port/Device Identifiers.
1593 struct se_lun *dest_lun, *tmp_lun;
1595 proto_ident = (ptr[0] & 0x0f);
1598 spin_lock(&dev->se_port_lock);
1599 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1600 tmp_tpg = tmp_lun->lun_tpg;
1603 * Look for the matching proto_ident provided by
1604 * the received TransportID
1606 if (tmp_tpg->proto_id != proto_ident)
1608 dest_rtpi = tmp_lun->lun_rtpi;
1610 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1611 (const char *)ptr, &tid_len, &iport_ptr);
1615 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1616 spin_unlock(&dev->se_port_lock);
1618 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1619 pr_err(" core_scsi3_tpg_depend_item()"
1621 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1622 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1626 * Locate the destination initiator ACL to be registered
1627 * from the decoded fabric module specific TransportID
1630 mutex_lock(&tmp_tpg->acl_node_mutex);
1631 dest_node_acl = __core_tpg_get_initiator_node_acl(
1634 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1635 mutex_unlock(&tmp_tpg->acl_node_mutex);
1637 if (!dest_node_acl) {
1638 core_scsi3_tpg_undepend_item(tmp_tpg);
1639 spin_lock(&dev->se_port_lock);
1643 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1644 pr_err("configfs_depend_item() failed"
1645 " for dest_node_acl->acl_group\n");
1646 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1647 core_scsi3_tpg_undepend_item(tmp_tpg);
1648 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1653 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1654 " %s Port RTPI: %hu\n",
1655 dest_tpg->se_tpg_tfo->get_fabric_name(),
1656 dest_node_acl->initiatorname, dest_rtpi);
1658 spin_lock(&dev->se_port_lock);
1661 spin_unlock(&dev->se_port_lock);
1664 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1666 ret = TCM_INVALID_PARAMETER_LIST;
1670 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1671 " tid_len: %d for %s + %s\n",
1672 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1673 tpdl, tid_len, i_str, iport_ptr);
1675 if (tid_len > tpdl) {
1676 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1677 " %u for Transport ID: %s\n", tid_len, ptr);
1678 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1679 core_scsi3_tpg_undepend_item(dest_tpg);
1680 ret = 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 ret = TCM_INVALID_PARAMETER_LIST;
1702 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1703 pr_err("core_scsi3_lunacl_depend_item()"
1705 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1706 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1707 core_scsi3_tpg_undepend_item(dest_tpg);
1708 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1712 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1713 " dest_se_deve mapped_lun: %llu\n",
1714 dest_tpg->se_tpg_tfo->get_fabric_name(),
1715 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1718 * Skip any TransportIDs that already have a registration for
1721 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1724 core_scsi3_put_pr_reg(pr_reg_e);
1725 core_scsi3_lunacl_undepend_item(dest_se_deve);
1726 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1727 core_scsi3_tpg_undepend_item(dest_tpg);
1734 * Allocate a struct pr_transport_id_holder and setup
1735 * the dest_node_acl and dest_se_deve pointers for the
1738 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1741 pr_err("Unable to allocate tidh_new\n");
1742 core_scsi3_lunacl_undepend_item(dest_se_deve);
1743 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1744 core_scsi3_tpg_undepend_item(dest_tpg);
1745 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1748 INIT_LIST_HEAD(&tidh_new->dest_list);
1749 tidh_new->dest_tpg = dest_tpg;
1750 tidh_new->dest_node_acl = dest_node_acl;
1751 tidh_new->dest_se_deve = dest_se_deve;
1754 * Allocate, but do NOT add the registration for the
1755 * TransportID referenced SCSI Initiator port. This
1756 * done because of the following from spc4r17 in section
1757 * 6.14.3 wrt SPEC_I_PT:
1759 * "If a registration fails for any initiator port (e.g., if th
1760 * logical unit does not have enough resources available to
1761 * hold the registration information), no registrations shall be
1762 * made, and the command shall be terminated with
1763 * CHECK CONDITION status."
1765 * That means we call __core_scsi3_alloc_registration() here,
1766 * and then call __core_scsi3_add_registration() in the
1767 * 2nd loop which will never fail.
1769 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
1770 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
1772 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1773 dest_node_acl, dest_lun, dest_se_deve,
1774 dest_se_deve->mapped_lun, iport_ptr,
1775 sa_res_key, all_tg_pt, aptpl);
1777 core_scsi3_lunacl_undepend_item(dest_se_deve);
1778 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1779 core_scsi3_tpg_undepend_item(dest_tpg);
1781 ret = TCM_INVALID_PARAMETER_LIST;
1784 tidh_new->dest_pr_reg = dest_pr_reg;
1785 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1793 transport_kunmap_data_sg(cmd);
1796 * Go ahead and create a registrations from tid_dest_list for the
1797 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1800 * The SA Reservation Key from the PROUT is set for the
1801 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1802 * means that the TransportID Initiator port will be
1803 * registered on all of the target ports in the SCSI target device
1804 * ALL_TG_PT=0 means the registration will only be for the
1805 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1808 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1809 dest_tpg = tidh->dest_tpg;
1810 dest_node_acl = tidh->dest_node_acl;
1811 dest_se_deve = tidh->dest_se_deve;
1812 dest_pr_reg = tidh->dest_pr_reg;
1814 list_del(&tidh->dest_list);
1817 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1818 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1820 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1823 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1824 " registered Transport ID for Node: %s%s Mapped LUN:"
1825 " %llu\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1826 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1827 dest_se_deve->mapped_lun : 0);
1829 if (!dest_se_deve) {
1830 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1831 target_pr_kref_release);
1834 core_scsi3_lunacl_undepend_item(dest_se_deve);
1835 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1836 core_scsi3_tpg_undepend_item(dest_tpg);
1841 transport_kunmap_data_sg(cmd);
1844 * For the failure case, release everything from tid_dest_list
1845 * including *dest_pr_reg and the configfs dependances..
1847 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1848 dest_tpg = tidh->dest_tpg;
1849 dest_node_acl = tidh->dest_node_acl;
1850 dest_se_deve = tidh->dest_se_deve;
1851 dest_pr_reg = tidh->dest_pr_reg;
1853 list_del(&tidh->dest_list);
1856 * Release any extra ALL_TG_PT=1 registrations for
1857 * the SPEC_I_PT=1 case.
1859 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1860 &dest_pr_reg->pr_reg_atp_list,
1861 pr_reg_atp_mem_list) {
1862 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1863 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1864 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1867 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1869 if (!dest_se_deve) {
1870 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1871 target_pr_kref_release);
1874 core_scsi3_lunacl_undepend_item(dest_se_deve);
1875 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1876 core_scsi3_tpg_undepend_item(dest_tpg);
1881 static int core_scsi3_update_aptpl_buf(
1882 struct se_device *dev,
1884 u32 pr_aptpl_buf_len)
1886 struct se_portal_group *tpg;
1887 struct t10_pr_registration *pr_reg;
1888 unsigned char tmp[512], isid_buf[32];
1893 spin_lock(&dev->dev_reservation_lock);
1894 spin_lock(&dev->t10_pr.registration_lock);
1896 * Walk the registration list..
1898 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1903 tpg = pr_reg->pr_reg_nacl->se_tpg;
1905 * Write out any ISID value to APTPL metadata that was included
1906 * in the original registration.
1908 if (pr_reg->isid_present_at_reg)
1909 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1910 pr_reg->pr_reg_isid);
1912 * Include special metadata if the pr_reg matches the
1913 * reservation holder.
1915 if (dev->dev_pr_res_holder == pr_reg) {
1916 snprintf(tmp, 512, "PR_REG_START: %d"
1917 "\ninitiator_fabric=%s\n"
1918 "initiator_node=%s\n%s"
1920 "res_holder=1\nres_type=%02x\n"
1921 "res_scope=%02x\nres_all_tg_pt=%d\n"
1922 "mapped_lun=%llu\n", reg_count,
1923 tpg->se_tpg_tfo->get_fabric_name(),
1924 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1925 pr_reg->pr_res_key, pr_reg->pr_res_type,
1926 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1927 pr_reg->pr_res_mapped_lun);
1929 snprintf(tmp, 512, "PR_REG_START: %d\n"
1930 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1931 "sa_res_key=%llu\nres_holder=0\n"
1932 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
1933 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1934 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1935 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1936 pr_reg->pr_res_mapped_lun);
1939 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1940 pr_err("Unable to update renaming APTPL metadata,"
1941 " reallocating larger buffer\n");
1945 len += sprintf(buf+len, "%s", tmp);
1948 * Include information about the associated SCSI target port.
1950 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1951 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
1952 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1953 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1954 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1955 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1958 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1959 pr_err("Unable to update renaming APTPL metadata,"
1960 " reallocating larger buffer\n");
1964 len += sprintf(buf+len, "%s", tmp);
1969 len += sprintf(buf+len, "No Registrations or Reservations");
1972 spin_unlock(&dev->t10_pr.registration_lock);
1973 spin_unlock(&dev->dev_reservation_lock);
1978 static int __core_scsi3_write_aptpl_to_file(
1979 struct se_device *dev,
1982 struct t10_wwn *wwn = &dev->t10_wwn;
1984 int flags = O_RDWR | O_CREAT | O_TRUNC;
1986 u32 pr_aptpl_buf_len;
1989 memset(path, 0, 512);
1991 if (strlen(&wwn->unit_serial[0]) >= 512) {
1992 pr_err("WWN value for struct se_device does not fit"
1993 " into path buffer\n");
1997 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1998 file = filp_open(path, flags, 0600);
2000 pr_err("filp_open(%s) for APTPL metadata"
2002 return PTR_ERR(file);
2005 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
2007 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
2010 pr_debug("Error writing APTPL metadata file: %s\n", path);
2013 return (ret < 0) ? -EIO : 0;
2017 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2018 * write out the updated metadata to struct file for this SCSI device.
2020 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
2023 int rc, len = PR_APTPL_BUF_LEN;
2026 char *null_buf = "No Registrations or Reservations\n";
2028 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2029 dev->t10_pr.pr_aptpl_active = 0;
2030 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
2033 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2040 return TCM_OUT_OF_RESOURCES;
2042 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
2049 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2051 pr_err("SPC-3 PR: Could not update APTPL\n");
2053 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2055 dev->t10_pr.pr_aptpl_active = 1;
2057 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2061 static sense_reason_t
2062 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2063 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2065 struct se_session *se_sess = cmd->se_sess;
2066 struct se_device *dev = cmd->se_dev;
2067 struct se_lun *se_lun = cmd->se_lun;
2068 struct se_portal_group *se_tpg;
2069 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2070 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2071 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2072 sense_reason_t ret = TCM_NO_SENSE;
2073 int pr_holder = 0, type;
2075 if (!se_sess || !se_lun) {
2076 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2077 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2079 se_tpg = se_sess->se_tpg;
2081 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2082 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2083 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2085 isid_ptr = &isid_buf[0];
2088 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2090 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2093 pr_warn("SPC-3 PR: Reservation Key non-zero"
2094 " for SA REGISTER, returning CONFLICT\n");
2095 return TCM_RESERVATION_CONFLICT;
2098 * Do nothing but return GOOD status.
2105 * Perform the Service Action REGISTER on the Initiator
2106 * Port Endpoint that the PRO was received from on the
2107 * Logical Unit of the SCSI device server.
2109 if (core_scsi3_alloc_registration(cmd->se_dev,
2110 se_sess->se_node_acl, cmd->se_lun,
2111 NULL, cmd->orig_fe_lun, isid_ptr,
2112 sa_res_key, all_tg_pt, aptpl,
2113 register_type, 0)) {
2114 pr_err("Unable to allocate"
2115 " struct t10_pr_registration\n");
2116 return TCM_INVALID_PARAMETER_LIST;
2120 * Register both the Initiator port that received
2121 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2122 * TransportID from Parameter list and loop through
2123 * fabric dependent parameter list while calling
2124 * logic from of core_scsi3_alloc_registration() for
2125 * each TransportID provided SCSI Initiator Port/Device
2127 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2128 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2132 return core_scsi3_update_and_write_aptpl(dev, aptpl);
2135 /* ok, existing registration */
2137 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2138 pr_err("SPC-3 PR REGISTER: Received"
2139 " res_key: 0x%016Lx does not match"
2140 " existing SA REGISTER res_key:"
2141 " 0x%016Lx\n", res_key,
2142 pr_reg->pr_res_key);
2143 ret = TCM_RESERVATION_CONFLICT;
2148 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2149 " set on a registered nexus\n");
2150 ret = TCM_INVALID_PARAMETER_LIST;
2155 * An existing ALL_TG_PT=1 registration being released
2156 * must also set ALL_TG_PT=1 in the incoming PROUT.
2158 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2159 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2160 " registration exists, but ALL_TG_PT=1 bit not"
2161 " present in received PROUT\n");
2162 ret = TCM_INVALID_CDB_FIELD;
2167 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2171 * Increment PRgeneration counter for struct se_device"
2172 * upon a successful REGISTER, see spc4r17 section 6.3.2
2173 * READ_KEYS service action.
2175 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2176 pr_reg->pr_res_key = sa_res_key;
2177 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2178 " Key for %s to: 0x%016Lx PRgeneration:"
2179 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2180 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2181 pr_reg->pr_reg_nacl->initiatorname,
2182 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2186 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2188 type = pr_reg->pr_res_type;
2189 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2191 if (pr_holder < 0) {
2192 ret = TCM_RESERVATION_CONFLICT;
2196 spin_lock(&pr_tmpl->registration_lock);
2198 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2199 * and matching pr_res_key.
2201 if (pr_reg->pr_reg_all_tg_pt) {
2202 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2203 &pr_tmpl->registration_list,
2206 if (!pr_reg_p->pr_reg_all_tg_pt)
2208 if (pr_reg_p->pr_res_key != res_key)
2210 if (pr_reg == pr_reg_p)
2212 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2213 pr_reg_p->pr_reg_nacl->initiatorname))
2216 __core_scsi3_free_registration(dev,
2222 * Release the calling I_T Nexus registration now..
2224 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2228 * From spc4r17, section 5.7.11.3 Unregistering
2230 * If the persistent reservation is a registrants only
2231 * type, the device server shall establish a unit
2232 * attention condition for the initiator port associated
2233 * with every registered I_T nexus except for the I_T
2234 * nexus on which the PERSISTENT RESERVE OUT command was
2235 * received, with the additional sense code set to
2236 * RESERVATIONS RELEASED.
2239 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2240 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2241 list_for_each_entry(pr_reg_p,
2242 &pr_tmpl->registration_list,
2245 target_ua_allocate_lun(
2246 pr_reg_p->pr_reg_nacl,
2247 pr_reg_p->pr_res_mapped_lun,
2249 ASCQ_2AH_RESERVATIONS_RELEASED);
2253 spin_unlock(&pr_tmpl->registration_lock);
2256 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2260 core_scsi3_put_pr_reg(pr_reg);
2264 unsigned char *core_scsi3_pr_dump_type(int type)
2267 case PR_TYPE_WRITE_EXCLUSIVE:
2268 return "Write Exclusive Access";
2269 case PR_TYPE_EXCLUSIVE_ACCESS:
2270 return "Exclusive Access";
2271 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2272 return "Write Exclusive Access, Registrants Only";
2273 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2274 return "Exclusive Access, Registrants Only";
2275 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2276 return "Write Exclusive Access, All Registrants";
2277 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2278 return "Exclusive Access, All Registrants";
2283 return "Unknown SPC-3 PR Type";
2286 static sense_reason_t
2287 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2289 struct se_device *dev = cmd->se_dev;
2290 struct se_session *se_sess = cmd->se_sess;
2291 struct se_lun *se_lun = cmd->se_lun;
2292 struct t10_pr_registration *pr_reg, *pr_res_holder;
2293 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2294 char i_buf[PR_REG_ISID_ID_LEN];
2297 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2299 if (!se_sess || !se_lun) {
2300 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2301 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2304 * Locate the existing *pr_reg via struct se_node_acl pointers
2306 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2309 pr_err("SPC-3 PR: Unable to locate"
2310 " PR_REGISTERED *pr_reg for RESERVE\n");
2311 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2314 * From spc4r17 Section 5.7.9: Reserving:
2316 * An application client creates a persistent reservation by issuing
2317 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2318 * a registered I_T nexus with the following parameters:
2319 * a) RESERVATION KEY set to the value of the reservation key that is
2320 * registered with the logical unit for the I_T nexus; and
2322 if (res_key != pr_reg->pr_res_key) {
2323 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2324 " does not match existing SA REGISTER res_key:"
2325 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2326 ret = TCM_RESERVATION_CONFLICT;
2327 goto out_put_pr_reg;
2330 * From spc4r17 Section 5.7.9: Reserving:
2333 * b) TYPE field and SCOPE field set to the persistent reservation
2336 * Only one persistent reservation is allowed at a time per logical unit
2337 * and that persistent reservation has a scope of LU_SCOPE.
2339 if (scope != PR_SCOPE_LU_SCOPE) {
2340 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2341 ret = TCM_INVALID_PARAMETER_LIST;
2342 goto out_put_pr_reg;
2345 * See if we have an existing PR reservation holder pointer at
2346 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2349 spin_lock(&dev->dev_reservation_lock);
2350 pr_res_holder = dev->dev_pr_res_holder;
2351 if (pr_res_holder) {
2353 * From spc4r17 Section 5.7.9: Reserving:
2355 * If the device server receives a PERSISTENT RESERVE OUT
2356 * command from an I_T nexus other than a persistent reservation
2357 * holder (see 5.7.10) that attempts to create a persistent
2358 * reservation when a persistent reservation already exists for
2359 * the logical unit, then the command shall be completed with
2360 * RESERVATION CONFLICT status.
2362 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2363 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2364 pr_err("SPC-3 PR: Attempted RESERVE from"
2365 " [%s]: %s while reservation already held by"
2366 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2367 cmd->se_tfo->get_fabric_name(),
2368 se_sess->se_node_acl->initiatorname,
2369 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2370 pr_res_holder->pr_reg_nacl->initiatorname);
2372 spin_unlock(&dev->dev_reservation_lock);
2373 ret = TCM_RESERVATION_CONFLICT;
2374 goto out_put_pr_reg;
2377 * From spc4r17 Section 5.7.9: Reserving:
2379 * If a persistent reservation holder attempts to modify the
2380 * type or scope of an existing persistent reservation, the
2381 * command shall be completed with RESERVATION CONFLICT status.
2383 if ((pr_res_holder->pr_res_type != type) ||
2384 (pr_res_holder->pr_res_scope != scope)) {
2385 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2386 pr_err("SPC-3 PR: Attempted RESERVE from"
2387 " [%s]: %s trying to change TYPE and/or SCOPE,"
2388 " while reservation already held by [%s]: %s,"
2389 " returning RESERVATION_CONFLICT\n",
2390 cmd->se_tfo->get_fabric_name(),
2391 se_sess->se_node_acl->initiatorname,
2392 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2393 pr_res_holder->pr_reg_nacl->initiatorname);
2395 spin_unlock(&dev->dev_reservation_lock);
2396 ret = TCM_RESERVATION_CONFLICT;
2397 goto out_put_pr_reg;
2400 * From spc4r17 Section 5.7.9: Reserving:
2402 * If the device server receives a PERSISTENT RESERVE OUT
2403 * command with RESERVE service action where the TYPE field and
2404 * the SCOPE field contain the same values as the existing type
2405 * and scope from a persistent reservation holder, it shall not
2406 * make any change to the existing persistent reservation and
2407 * shall completethe command with GOOD status.
2409 spin_unlock(&dev->dev_reservation_lock);
2411 goto out_put_pr_reg;
2414 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2415 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2417 pr_reg->pr_res_scope = scope;
2418 pr_reg->pr_res_type = type;
2419 pr_reg->pr_res_holder = 1;
2420 dev->dev_pr_res_holder = pr_reg;
2421 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2423 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2424 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2425 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2426 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2427 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2428 cmd->se_tfo->get_fabric_name(),
2429 se_sess->se_node_acl->initiatorname,
2431 spin_unlock(&dev->dev_reservation_lock);
2433 if (pr_tmpl->pr_aptpl_active)
2434 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2438 core_scsi3_put_pr_reg(pr_reg);
2442 static sense_reason_t
2443 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2447 case PR_TYPE_WRITE_EXCLUSIVE:
2448 case PR_TYPE_EXCLUSIVE_ACCESS:
2449 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2450 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2451 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2452 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2453 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2455 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2457 return TCM_INVALID_CDB_FIELD;
2462 * Called with struct se_device->dev_reservation_lock held.
2464 static void __core_scsi3_complete_pro_release(
2465 struct se_device *dev,
2466 struct se_node_acl *se_nacl,
2467 struct t10_pr_registration *pr_reg,
2471 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2472 char i_buf[PR_REG_ISID_ID_LEN];
2473 int pr_res_type = 0, pr_res_scope = 0;
2475 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2476 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2478 * Go ahead and release the current PR reservation holder.
2479 * If an All Registrants reservation is currently active and
2480 * a unregister operation is requested, replace the current
2481 * dev_pr_res_holder with another active registration.
2483 if (dev->dev_pr_res_holder) {
2484 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2485 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2486 dev->dev_pr_res_holder->pr_res_type = 0;
2487 dev->dev_pr_res_holder->pr_res_scope = 0;
2488 dev->dev_pr_res_holder->pr_res_holder = 0;
2489 dev->dev_pr_res_holder = NULL;
2494 spin_lock(&dev->t10_pr.registration_lock);
2495 list_del_init(&pr_reg->pr_reg_list);
2497 * If the I_T nexus is a reservation holder, the persistent reservation
2498 * is of an all registrants type, and the I_T nexus is the last remaining
2499 * registered I_T nexus, then the device server shall also release the
2500 * persistent reservation.
2502 if (!list_empty(&dev->t10_pr.registration_list) &&
2503 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2504 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2505 dev->dev_pr_res_holder =
2506 list_entry(dev->t10_pr.registration_list.next,
2507 struct t10_pr_registration, pr_reg_list);
2508 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2509 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2510 dev->dev_pr_res_holder->pr_res_holder = 1;
2512 spin_unlock(&dev->t10_pr.registration_lock);
2514 if (!dev->dev_pr_res_holder) {
2515 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2516 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2517 tfo->get_fabric_name(), (explicit) ? "explicit" :
2518 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2519 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2521 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2522 tfo->get_fabric_name(), se_nacl->initiatorname,
2525 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2527 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2530 static sense_reason_t
2531 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2534 struct se_device *dev = cmd->se_dev;
2535 struct se_session *se_sess = cmd->se_sess;
2536 struct se_lun *se_lun = cmd->se_lun;
2537 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2538 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2539 sense_reason_t ret = 0;
2541 if (!se_sess || !se_lun) {
2542 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2543 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2546 * Locate the existing *pr_reg via struct se_node_acl pointers
2548 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2550 pr_err("SPC-3 PR: Unable to locate"
2551 " PR_REGISTERED *pr_reg for RELEASE\n");
2552 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2555 * From spc4r17 Section 5.7.11.2 Releasing:
2557 * If there is no persistent reservation or in response to a persistent
2558 * reservation release request from a registered I_T nexus that is not a
2559 * persistent reservation holder (see 5.7.10), the device server shall
2562 * a) Not release the persistent reservation, if any;
2563 * b) Not remove any registrations; and
2564 * c) Complete the command with GOOD status.
2566 spin_lock(&dev->dev_reservation_lock);
2567 pr_res_holder = dev->dev_pr_res_holder;
2568 if (!pr_res_holder) {
2570 * No persistent reservation, return GOOD status.
2572 spin_unlock(&dev->dev_reservation_lock);
2573 goto out_put_pr_reg;
2576 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2578 * Release request from a registered I_T nexus that is not a
2579 * persistent reservation holder. return GOOD status.
2581 spin_unlock(&dev->dev_reservation_lock);
2582 goto out_put_pr_reg;
2586 * From spc4r17 Section 5.7.11.2 Releasing:
2588 * Only the persistent reservation holder (see 5.7.10) is allowed to
2589 * release a persistent reservation.
2591 * An application client releases the persistent reservation by issuing
2592 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2593 * an I_T nexus that is a persistent reservation holder with the
2594 * following parameters:
2596 * a) RESERVATION KEY field set to the value of the reservation key
2597 * that is registered with the logical unit for the I_T nexus;
2599 if (res_key != pr_reg->pr_res_key) {
2600 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2601 " does not match existing SA REGISTER res_key:"
2602 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2603 spin_unlock(&dev->dev_reservation_lock);
2604 ret = TCM_RESERVATION_CONFLICT;
2605 goto out_put_pr_reg;
2608 * From spc4r17 Section 5.7.11.2 Releasing and above:
2610 * b) TYPE field and SCOPE field set to match the persistent
2611 * reservation being released.
2613 if ((pr_res_holder->pr_res_type != type) ||
2614 (pr_res_holder->pr_res_scope != scope)) {
2615 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2616 pr_err("SPC-3 PR RELEASE: Attempted to release"
2617 " reservation from [%s]: %s with different TYPE "
2618 "and/or SCOPE while reservation already held by"
2619 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2620 cmd->se_tfo->get_fabric_name(),
2621 se_sess->se_node_acl->initiatorname,
2622 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2623 pr_res_holder->pr_reg_nacl->initiatorname);
2625 spin_unlock(&dev->dev_reservation_lock);
2626 ret = TCM_RESERVATION_CONFLICT;
2627 goto out_put_pr_reg;
2630 * In response to a persistent reservation release request from the
2631 * persistent reservation holder the device server shall perform a
2632 * release by doing the following as an uninterrupted series of actions:
2633 * a) Release the persistent reservation;
2634 * b) Not remove any registration(s);
2635 * c) If the released persistent reservation is a registrants only type
2636 * or all registrants type persistent reservation,
2637 * the device server shall establish a unit attention condition for
2638 * the initiator port associated with every regis-
2639 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2640 * RESERVE OUT command with RELEASE service action was received,
2641 * with the additional sense code set to RESERVATIONS RELEASED; and
2642 * d) If the persistent reservation is of any other type, the device
2643 * server shall not establish a unit attention condition.
2645 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2648 spin_unlock(&dev->dev_reservation_lock);
2650 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2651 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2652 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2653 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2655 * If no UNIT ATTENTION conditions will be established for
2656 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2657 * go ahead and check for APTPL=1 update+write below
2662 spin_lock(&pr_tmpl->registration_lock);
2663 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2666 * Do not establish a UNIT ATTENTION condition
2667 * for the calling I_T Nexus
2669 if (pr_reg_p == pr_reg)
2672 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2673 pr_reg_p->pr_res_mapped_lun,
2674 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2676 spin_unlock(&pr_tmpl->registration_lock);
2679 if (pr_tmpl->pr_aptpl_active)
2680 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2683 core_scsi3_put_pr_reg(pr_reg);
2687 static sense_reason_t
2688 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2690 struct se_device *dev = cmd->se_dev;
2691 struct se_node_acl *pr_reg_nacl;
2692 struct se_session *se_sess = cmd->se_sess;
2693 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2694 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2695 u64 pr_res_mapped_lun = 0;
2696 int calling_it_nexus = 0;
2698 * Locate the existing *pr_reg via struct se_node_acl pointers
2700 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2701 se_sess->se_node_acl, se_sess);
2703 pr_err("SPC-3 PR: Unable to locate"
2704 " PR_REGISTERED *pr_reg for CLEAR\n");
2705 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2708 * From spc4r17 section 5.7.11.6, Clearing:
2710 * Any application client may release the persistent reservation and
2711 * remove all registrations from a device server by issuing a
2712 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2713 * registered I_T nexus with the following parameter:
2715 * a) RESERVATION KEY field set to the value of the reservation key
2716 * that is registered with the logical unit for the I_T nexus.
2718 if (res_key != pr_reg_n->pr_res_key) {
2719 pr_err("SPC-3 PR REGISTER: Received"
2720 " res_key: 0x%016Lx does not match"
2721 " existing SA REGISTER res_key:"
2722 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2723 core_scsi3_put_pr_reg(pr_reg_n);
2724 return TCM_RESERVATION_CONFLICT;
2727 * a) Release the persistent reservation, if any;
2729 spin_lock(&dev->dev_reservation_lock);
2730 pr_res_holder = dev->dev_pr_res_holder;
2731 if (pr_res_holder) {
2732 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2733 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2734 pr_res_holder, 0, 0);
2736 spin_unlock(&dev->dev_reservation_lock);
2738 * b) Remove all registration(s) (see spc4r17 5.7.7);
2740 spin_lock(&pr_tmpl->registration_lock);
2741 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2742 &pr_tmpl->registration_list, pr_reg_list) {
2744 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2745 pr_reg_nacl = pr_reg->pr_reg_nacl;
2746 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2747 __core_scsi3_free_registration(dev, pr_reg, NULL,
2750 * e) Establish a unit attention condition for the initiator
2751 * port associated with every registered I_T nexus other
2752 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2753 * command with CLEAR service action was received, with the
2754 * additional sense code set to RESERVATIONS PREEMPTED.
2756 if (!calling_it_nexus)
2757 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2758 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2760 spin_unlock(&pr_tmpl->registration_lock);
2762 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2763 cmd->se_tfo->get_fabric_name());
2765 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2767 core_scsi3_pr_generation(dev);
2772 * Called with struct se_device->dev_reservation_lock held.
2774 static void __core_scsi3_complete_pro_preempt(
2775 struct se_device *dev,
2776 struct t10_pr_registration *pr_reg,
2777 struct list_head *preempt_and_abort_list,
2780 enum preempt_type preempt_type)
2782 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2783 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2784 char i_buf[PR_REG_ISID_ID_LEN];
2786 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2787 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2789 * Do an implicit RELEASE of the existing reservation.
2791 if (dev->dev_pr_res_holder)
2792 __core_scsi3_complete_pro_release(dev, nacl,
2793 dev->dev_pr_res_holder, 0, 0);
2795 dev->dev_pr_res_holder = pr_reg;
2796 pr_reg->pr_res_holder = 1;
2797 pr_reg->pr_res_type = type;
2798 pr_reg->pr_res_scope = scope;
2800 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2801 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2802 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2803 core_scsi3_pr_dump_type(type),
2804 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2805 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2806 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2807 nacl->initiatorname, i_buf);
2809 * For PREEMPT_AND_ABORT, add the preempting reservation's
2810 * struct t10_pr_registration to the list that will be compared
2811 * against received CDBs..
2813 if (preempt_and_abort_list)
2814 list_add_tail(&pr_reg->pr_reg_abort_list,
2815 preempt_and_abort_list);
2818 static void core_scsi3_release_preempt_and_abort(
2819 struct list_head *preempt_and_abort_list,
2820 struct t10_pr_registration *pr_reg_holder)
2822 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2824 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2825 pr_reg_abort_list) {
2827 list_del(&pr_reg->pr_reg_abort_list);
2828 if (pr_reg_holder == pr_reg)
2830 if (pr_reg->pr_res_holder) {
2831 pr_warn("pr_reg->pr_res_holder still set\n");
2835 pr_reg->pr_reg_deve = NULL;
2836 pr_reg->pr_reg_nacl = NULL;
2837 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2841 static sense_reason_t
2842 core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2843 u64 sa_res_key, enum preempt_type preempt_type)
2845 struct se_device *dev = cmd->se_dev;
2846 struct se_node_acl *pr_reg_nacl;
2847 struct se_session *se_sess = cmd->se_sess;
2848 LIST_HEAD(preempt_and_abort_list);
2849 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2850 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2851 u64 pr_res_mapped_lun = 0;
2852 int all_reg = 0, calling_it_nexus = 0;
2853 bool sa_res_key_unmatched = sa_res_key != 0;
2854 int prh_type = 0, prh_scope = 0;
2857 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2859 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2862 pr_err("SPC-3 PR: Unable to locate"
2863 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2864 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2865 return TCM_RESERVATION_CONFLICT;
2867 if (pr_reg_n->pr_res_key != res_key) {
2868 core_scsi3_put_pr_reg(pr_reg_n);
2869 return TCM_RESERVATION_CONFLICT;
2871 if (scope != PR_SCOPE_LU_SCOPE) {
2872 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2873 core_scsi3_put_pr_reg(pr_reg_n);
2874 return TCM_INVALID_PARAMETER_LIST;
2877 spin_lock(&dev->dev_reservation_lock);
2878 pr_res_holder = dev->dev_pr_res_holder;
2879 if (pr_res_holder &&
2880 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2881 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2884 if (!all_reg && !sa_res_key) {
2885 spin_unlock(&dev->dev_reservation_lock);
2886 core_scsi3_put_pr_reg(pr_reg_n);
2887 return TCM_INVALID_PARAMETER_LIST;
2890 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2892 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2893 * persistent reservation holder or there is no persistent reservation
2894 * holder (i.e., there is no persistent reservation), then the device
2895 * server shall perform a preempt by doing the following in an
2896 * uninterrupted series of actions. (See below..)
2898 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2900 * No existing or SA Reservation Key matching reservations..
2902 * PROUT SA PREEMPT with All Registrant type reservations are
2903 * allowed to be processed without a matching SA Reservation Key
2905 spin_lock(&pr_tmpl->registration_lock);
2906 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2907 &pr_tmpl->registration_list, pr_reg_list) {
2909 * Removing of registrations in non all registrants
2910 * type reservations without a matching SA reservation
2913 * a) Remove the registrations for all I_T nexuses
2914 * specified by the SERVICE ACTION RESERVATION KEY
2916 * b) Ignore the contents of the SCOPE and TYPE fields;
2917 * c) Process tasks as defined in 5.7.1; and
2918 * d) Establish a unit attention condition for the
2919 * initiator port associated with every I_T nexus
2920 * that lost its registration other than the I_T
2921 * nexus on which the PERSISTENT RESERVE OUT command
2922 * was received, with the additional sense code set
2923 * to REGISTRATIONS PREEMPTED.
2926 if (pr_reg->pr_res_key != sa_res_key)
2928 sa_res_key_unmatched = false;
2930 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2931 pr_reg_nacl = pr_reg->pr_reg_nacl;
2932 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2933 __core_scsi3_free_registration(dev, pr_reg,
2934 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2935 NULL, calling_it_nexus);
2938 * Case for any existing all registrants type
2939 * reservation, follow logic in spc4r17 section
2940 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2942 * For a ZERO SA Reservation key, release
2943 * all other registrations and do an implicit
2944 * release of active persistent reservation.
2946 * For a non-ZERO SA Reservation key, only
2947 * release the matching reservation key from
2951 (pr_reg->pr_res_key != sa_res_key))
2953 sa_res_key_unmatched = false;
2955 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2956 if (calling_it_nexus)
2959 pr_reg_nacl = pr_reg->pr_reg_nacl;
2960 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2961 __core_scsi3_free_registration(dev, pr_reg,
2962 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2965 if (!calling_it_nexus)
2966 target_ua_allocate_lun(pr_reg_nacl,
2967 pr_res_mapped_lun, 0x2A,
2968 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2970 spin_unlock(&pr_tmpl->registration_lock);
2972 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2973 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2974 * RESERVATION KEY field to a value that does not match any
2975 * registered reservation key, then the device server shall
2976 * complete the command with RESERVATION CONFLICT status.
2978 if (sa_res_key_unmatched) {
2979 spin_unlock(&dev->dev_reservation_lock);
2980 core_scsi3_put_pr_reg(pr_reg_n);
2981 return TCM_RESERVATION_CONFLICT;
2984 * For an existing all registrants type reservation
2985 * with a zero SA rservation key, preempt the existing
2986 * reservation with the new PR type and scope.
2988 if (pr_res_holder && all_reg && !(sa_res_key)) {
2989 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
2990 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2991 type, scope, preempt_type);
2993 if (preempt_type == PREEMPT_AND_ABORT)
2994 core_scsi3_release_preempt_and_abort(
2995 &preempt_and_abort_list, pr_reg_n);
2997 spin_unlock(&dev->dev_reservation_lock);
2999 if (pr_tmpl->pr_aptpl_active)
3000 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3002 core_scsi3_put_pr_reg(pr_reg_n);
3003 core_scsi3_pr_generation(cmd->se_dev);
3007 * The PREEMPTing SA reservation key matches that of the
3008 * existing persistent reservation, first, we check if
3009 * we are preempting our own reservation.
3010 * From spc4r17, section 5.7.11.4.3 Preempting
3011 * persistent reservations and registration handling
3013 * If an all registrants persistent reservation is not
3014 * present, it is not an error for the persistent
3015 * reservation holder to preempt itself (i.e., a
3016 * PERSISTENT RESERVE OUT with a PREEMPT service action
3017 * or a PREEMPT AND ABORT service action with the
3018 * SERVICE ACTION RESERVATION KEY value equal to the
3019 * persistent reservation holder's reservation key that
3020 * is received from the persistent reservation holder).
3021 * In that case, the device server shall establish the
3022 * new persistent reservation and maintain the
3025 prh_type = pr_res_holder->pr_res_type;
3026 prh_scope = pr_res_holder->pr_res_scope;
3028 * If the SERVICE ACTION RESERVATION KEY field identifies a
3029 * persistent reservation holder (see 5.7.10), the device
3030 * server shall perform a preempt by doing the following as
3031 * an uninterrupted series of actions:
3033 * a) Release the persistent reservation for the holder
3034 * identified by the SERVICE ACTION RESERVATION KEY field;
3036 if (pr_reg_n != pr_res_holder)
3037 __core_scsi3_complete_pro_release(dev,
3038 pr_res_holder->pr_reg_nacl,
3039 dev->dev_pr_res_holder, 0, 0);
3041 * b) Remove the registrations for all I_T nexuses identified
3042 * by the SERVICE ACTION RESERVATION KEY field, except the
3043 * I_T nexus that is being used for the PERSISTENT RESERVE
3044 * OUT command. If an all registrants persistent reservation
3045 * is present and the SERVICE ACTION RESERVATION KEY field
3046 * is set to zero, then all registrations shall be removed
3047 * except for that of the I_T nexus that is being used for
3048 * the PERSISTENT RESERVE OUT command;
3050 spin_lock(&pr_tmpl->registration_lock);
3051 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3052 &pr_tmpl->registration_list, pr_reg_list) {
3054 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3055 if (calling_it_nexus)
3058 if (pr_reg->pr_res_key != sa_res_key)
3061 pr_reg_nacl = pr_reg->pr_reg_nacl;
3062 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3063 __core_scsi3_free_registration(dev, pr_reg,
3064 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3067 * e) Establish a unit attention condition for the initiator
3068 * port associated with every I_T nexus that lost its
3069 * persistent reservation and/or registration, with the
3070 * additional sense code set to REGISTRATIONS PREEMPTED;
3072 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3073 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3075 spin_unlock(&pr_tmpl->registration_lock);
3077 * c) Establish a persistent reservation for the preempting
3078 * I_T nexus using the contents of the SCOPE and TYPE fields;
3080 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3081 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3082 type, scope, preempt_type);
3084 * d) Process tasks as defined in 5.7.1;
3086 * f) If the type or scope has changed, then for every I_T nexus
3087 * whose reservation key was not removed, except for the I_T
3088 * nexus on which the PERSISTENT RESERVE OUT command was
3089 * received, the device server shall establish a unit
3090 * attention condition for the initiator port associated with
3091 * that I_T nexus, with the additional sense code set to
3092 * RESERVATIONS RELEASED. If the type or scope have not
3093 * changed, then no unit attention condition(s) shall be
3094 * established for this reason.
3096 if ((prh_type != type) || (prh_scope != scope)) {
3097 spin_lock(&pr_tmpl->registration_lock);
3098 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3099 &pr_tmpl->registration_list, pr_reg_list) {
3101 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3102 if (calling_it_nexus)
3105 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3106 pr_reg->pr_res_mapped_lun, 0x2A,
3107 ASCQ_2AH_RESERVATIONS_RELEASED);
3109 spin_unlock(&pr_tmpl->registration_lock);
3111 spin_unlock(&dev->dev_reservation_lock);
3113 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3114 * All received CDBs for the matching existing reservation and
3115 * registrations undergo ABORT_TASK logic.
3117 * From there, core_scsi3_release_preempt_and_abort() will
3118 * release every registration in the list (which have already
3119 * been removed from the primary pr_reg list), except the
3120 * new persistent reservation holder, the calling Initiator Port.
3122 if (preempt_type == PREEMPT_AND_ABORT) {
3123 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3124 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3128 if (pr_tmpl->pr_aptpl_active)
3129 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3131 core_scsi3_put_pr_reg(pr_reg_n);
3132 core_scsi3_pr_generation(cmd->se_dev);
3136 static sense_reason_t
3137 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3138 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3141 case PR_TYPE_WRITE_EXCLUSIVE:
3142 case PR_TYPE_EXCLUSIVE_ACCESS:
3143 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3144 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3145 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3146 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3147 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3148 sa_res_key, preempt_type);
3150 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3151 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3152 return TCM_INVALID_CDB_FIELD;
3157 static sense_reason_t
3158 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3159 u64 sa_res_key, int aptpl, int unreg)
3161 struct se_session *se_sess = cmd->se_sess;
3162 struct se_device *dev = cmd->se_dev;
3163 struct se_dev_entry *dest_se_deve = NULL;
3164 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3165 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3166 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3167 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3168 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3169 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3171 const unsigned char *initiator_str;
3172 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
3173 u32 tid_len, tmp_tid_len;
3174 int new_reg = 0, type, scope, matching_iname;
3176 unsigned short rtpi;
3177 unsigned char proto_ident;
3179 if (!se_sess || !se_lun) {
3180 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3181 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3184 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3185 se_tpg = se_sess->se_tpg;
3186 tf_ops = se_tpg->se_tpg_tfo;
3188 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3189 * Register behaviors for a REGISTER AND MOVE service action
3191 * Locate the existing *pr_reg via struct se_node_acl pointers
3193 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3196 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3197 " *pr_reg for REGISTER_AND_MOVE\n");
3198 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3201 * The provided reservation key much match the existing reservation key
3202 * provided during this initiator's I_T nexus registration.
3204 if (res_key != pr_reg->pr_res_key) {
3205 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3206 " res_key: 0x%016Lx does not match existing SA REGISTER"
3207 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3208 ret = TCM_RESERVATION_CONFLICT;
3209 goto out_put_pr_reg;
3212 * The service active reservation key needs to be non zero
3215 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3217 ret = TCM_INVALID_PARAMETER_LIST;
3218 goto out_put_pr_reg;
3222 * Determine the Relative Target Port Identifier where the reservation
3223 * will be moved to for the TransportID containing SCSI initiator WWN
3226 buf = transport_kmap_data_sg(cmd);
3228 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3229 goto out_put_pr_reg;
3232 rtpi = (buf[18] & 0xff) << 8;
3233 rtpi |= buf[19] & 0xff;
3234 tid_len = (buf[20] & 0xff) << 24;
3235 tid_len |= (buf[21] & 0xff) << 16;
3236 tid_len |= (buf[22] & 0xff) << 8;
3237 tid_len |= buf[23] & 0xff;
3238 transport_kunmap_data_sg(cmd);
3241 if ((tid_len + 24) != cmd->data_length) {
3242 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3243 " does not equal CDB data_length: %u\n", tid_len,
3245 ret = TCM_INVALID_PARAMETER_LIST;
3246 goto out_put_pr_reg;
3249 spin_lock(&dev->se_port_lock);
3250 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3251 if (tmp_lun->lun_rtpi != rtpi)
3253 dest_se_tpg = tmp_lun->lun_tpg;
3254 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3258 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3259 spin_unlock(&dev->se_port_lock);
3261 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3262 pr_err("core_scsi3_tpg_depend_item() failed"
3263 " for dest_se_tpg\n");
3264 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3265 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3266 goto out_put_pr_reg;
3269 spin_lock(&dev->se_port_lock);
3272 spin_unlock(&dev->se_port_lock);
3274 if (!dest_se_tpg || !dest_tf_ops) {
3275 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3276 " fabric ops from Relative Target Port Identifier:"
3278 ret = TCM_INVALID_PARAMETER_LIST;
3279 goto out_put_pr_reg;
3282 buf = transport_kmap_data_sg(cmd);
3284 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3285 goto out_put_pr_reg;
3287 proto_ident = (buf[24] & 0x0f);
3289 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3290 " 0x%02x\n", proto_ident);
3292 if (proto_ident != dest_se_tpg->proto_id) {
3293 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3294 " proto_ident: 0x%02x does not match ident: 0x%02x"
3295 " from fabric: %s\n", proto_ident,
3296 dest_se_tpg->proto_id,
3297 dest_tf_ops->get_fabric_name());
3298 ret = TCM_INVALID_PARAMETER_LIST;
3301 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
3302 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3303 if (!initiator_str) {
3304 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3305 " initiator_str from Transport ID\n");
3306 ret = TCM_INVALID_PARAMETER_LIST;
3310 transport_kunmap_data_sg(cmd);
3313 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3314 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3315 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3318 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3319 * action specifies a TransportID that is the same as the initiator port
3320 * of the I_T nexus for the command received, then the command shall
3321 * be terminated with CHECK CONDITION status, with the sense key set to
3322 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3323 * IN PARAMETER LIST.
3325 pr_reg_nacl = pr_reg->pr_reg_nacl;
3326 matching_iname = (!strcmp(initiator_str,
3327 pr_reg_nacl->initiatorname)) ? 1 : 0;
3328 if (!matching_iname)
3329 goto after_iport_check;
3331 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3332 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3333 " matches: %s on received I_T Nexus\n", initiator_str,
3334 pr_reg_nacl->initiatorname);
3335 ret = TCM_INVALID_PARAMETER_LIST;
3338 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3339 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3340 " matches: %s %s on received I_T Nexus\n",
3341 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3342 pr_reg->pr_reg_isid);
3343 ret = TCM_INVALID_PARAMETER_LIST;
3348 * Locate the destination struct se_node_acl from the received Transport ID
3350 mutex_lock(&dest_se_tpg->acl_node_mutex);
3351 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3354 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3355 mutex_unlock(&dest_se_tpg->acl_node_mutex);
3357 if (!dest_node_acl) {
3358 pr_err("Unable to locate %s dest_node_acl for"
3359 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3361 ret = TCM_INVALID_PARAMETER_LIST;
3365 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3366 pr_err("core_scsi3_nodeacl_depend_item() for"
3367 " dest_node_acl\n");
3368 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3369 dest_node_acl = NULL;
3370 ret = TCM_INVALID_PARAMETER_LIST;
3374 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3375 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3376 dest_node_acl->initiatorname);
3379 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3382 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3383 if (!dest_se_deve) {
3384 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3385 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3386 ret = TCM_INVALID_PARAMETER_LIST;
3390 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3391 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3392 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3393 dest_se_deve = NULL;
3394 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3398 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3399 " ACL for dest_se_deve->mapped_lun: %llu\n",
3400 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3401 dest_se_deve->mapped_lun);
3404 * A persistent reservation needs to already existing in order to
3405 * successfully complete the REGISTER_AND_MOVE service action..
3407 spin_lock(&dev->dev_reservation_lock);
3408 pr_res_holder = dev->dev_pr_res_holder;
3409 if (!pr_res_holder) {
3410 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3411 " currently held\n");
3412 spin_unlock(&dev->dev_reservation_lock);
3413 ret = TCM_INVALID_CDB_FIELD;
3417 * The received on I_T Nexus must be the reservation holder.
3419 * From spc4r17 section 5.7.8 Table 50 --
3420 * Register behaviors for a REGISTER AND MOVE service action
3422 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3423 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3424 " Nexus is not reservation holder\n");
3425 spin_unlock(&dev->dev_reservation_lock);
3426 ret = TCM_RESERVATION_CONFLICT;
3430 * From spc4r17 section 5.7.8: registering and moving reservation
3432 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3433 * action is received and the established persistent reservation is a
3434 * Write Exclusive - All Registrants type or Exclusive Access -
3435 * All Registrants type reservation, then the command shall be completed
3436 * with RESERVATION CONFLICT status.
3438 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3439 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3440 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3441 " reservation for type: %s\n",
3442 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3443 spin_unlock(&dev->dev_reservation_lock);
3444 ret = TCM_RESERVATION_CONFLICT;
3447 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3449 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3451 type = pr_res_holder->pr_res_type;
3452 scope = pr_res_holder->pr_res_type;
3454 * c) Associate the reservation key specified in the SERVICE ACTION
3455 * RESERVATION KEY field with the I_T nexus specified as the
3456 * destination of the register and move, where:
3457 * A) The I_T nexus is specified by the TransportID and the
3458 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3459 * B) Regardless of the TransportID format used, the association for
3460 * the initiator port is based on either the initiator port name
3461 * (see 3.1.71) on SCSI transport protocols where port names are
3462 * required or the initiator port identifier (see 3.1.70) on SCSI
3463 * transport protocols where port names are not required;
3464 * d) Register the reservation key specified in the SERVICE ACTION
3465 * RESERVATION KEY field;
3466 * e) Retain the reservation key specified in the SERVICE ACTION
3467 * RESERVATION KEY field and associated information;
3469 * Also, It is not an error for a REGISTER AND MOVE service action to
3470 * register an I_T nexus that is already registered with the same
3471 * reservation key or a different reservation key.
3473 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3476 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
3477 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
3479 spin_unlock(&dev->dev_reservation_lock);
3480 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3481 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3482 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
3483 ret = TCM_INVALID_PARAMETER_LIST;
3486 spin_lock(&dev->dev_reservation_lock);
3487 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3492 * f) Release the persistent reservation for the persistent reservation
3493 * holder (i.e., the I_T nexus on which the
3495 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3496 dev->dev_pr_res_holder, 0, 0);
3498 * g) Move the persistent reservation to the specified I_T nexus using
3499 * the same scope and type as the persistent reservation released in
3502 dev->dev_pr_res_holder = dest_pr_reg;
3503 dest_pr_reg->pr_res_holder = 1;
3504 dest_pr_reg->pr_res_type = type;
3505 pr_reg->pr_res_scope = scope;
3506 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3508 * Increment PRGeneration for existing registrations..
3511 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3512 spin_unlock(&dev->dev_reservation_lock);
3514 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3515 " created new reservation holder TYPE: %s on object RTPI:"
3516 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3517 core_scsi3_pr_dump_type(type), rtpi,
3518 dest_pr_reg->pr_res_generation);
3519 pr_debug("SPC-3 PR Successfully moved reservation from"
3520 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3521 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3522 i_buf, dest_tf_ops->get_fabric_name(),
3523 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3526 * It is now safe to release configfs group dependencies for destination
3527 * of Transport ID Initiator Device/Port Identifier
3529 core_scsi3_lunacl_undepend_item(dest_se_deve);
3530 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3531 core_scsi3_tpg_undepend_item(dest_se_tpg);
3533 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3534 * nexus on which PERSISTENT RESERVE OUT command was received.
3537 spin_lock(&pr_tmpl->registration_lock);
3538 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3539 spin_unlock(&pr_tmpl->registration_lock);
3541 core_scsi3_put_pr_reg(pr_reg);
3543 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3545 transport_kunmap_data_sg(cmd);
3547 core_scsi3_put_pr_reg(dest_pr_reg);
3551 transport_kunmap_data_sg(cmd);
3553 core_scsi3_lunacl_undepend_item(dest_se_deve);
3555 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3556 core_scsi3_tpg_undepend_item(dest_se_tpg);
3559 core_scsi3_put_pr_reg(pr_reg);
3563 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3565 unsigned int __v1, __v2;
3567 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3568 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3570 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3574 * See spc4r17 section 6.14 Table 170
3577 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3579 struct se_device *dev = cmd->se_dev;
3580 unsigned char *cdb = &cmd->t_task_cdb[0];
3582 u64 res_key, sa_res_key;
3583 int sa, scope, type, aptpl;
3584 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3588 * Following spc2r20 5.5.1 Reservations overview:
3590 * If a logical unit has been reserved by any RESERVE command and is
3591 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3592 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3593 * initiator or service action and shall terminate with a RESERVATION
3596 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3597 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3598 " SPC-2 reservation is held, returning"
3599 " RESERVATION_CONFLICT\n");
3600 return TCM_RESERVATION_CONFLICT;
3604 * FIXME: A NULL struct se_session pointer means an this is not coming from
3605 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3608 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3610 if (cmd->data_length < 24) {
3611 pr_warn("SPC-PR: Received PR OUT parameter list"
3612 " length too small: %u\n", cmd->data_length);
3613 return TCM_INVALID_PARAMETER_LIST;
3617 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3619 sa = (cdb[1] & 0x1f);
3620 scope = (cdb[2] & 0xf0);
3621 type = (cdb[2] & 0x0f);
3623 buf = transport_kmap_data_sg(cmd);
3625 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3628 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3630 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3631 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3633 * REGISTER_AND_MOVE uses a different SA parameter list containing
3634 * SCSI TransportIDs.
3636 if (sa != PRO_REGISTER_AND_MOVE) {
3637 spec_i_pt = (buf[20] & 0x08);
3638 all_tg_pt = (buf[20] & 0x04);
3639 aptpl = (buf[20] & 0x01);
3641 aptpl = (buf[17] & 0x01);
3642 unreg = (buf[17] & 0x02);
3645 * If the backend device has been configured to force APTPL metadata
3646 * write-out, go ahead and propigate aptpl=1 down now.
3648 if (dev->dev_attrib.force_pr_aptpl)
3651 transport_kunmap_data_sg(cmd);
3655 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3657 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3658 return TCM_INVALID_PARAMETER_LIST;
3661 * From spc4r17 section 6.14:
3663 * If the SPEC_I_PT bit is set to zero, the service action is not
3664 * REGISTER AND MOVE, and the parameter list length is not 24, then
3665 * the command shall be terminated with CHECK CONDITION status, with
3666 * the sense key set to ILLEGAL REQUEST, and the additional sense
3667 * code set to PARAMETER LIST LENGTH ERROR.
3669 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3670 (cmd->data_length != 24)) {
3671 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3672 " list length: %u\n", cmd->data_length);
3673 return TCM_INVALID_PARAMETER_LIST;
3677 * (core_scsi3_emulate_pro_* function parameters
3678 * are defined by spc4r17 Table 174:
3679 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3683 ret = core_scsi3_emulate_pro_register(cmd,
3684 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3687 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3690 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3693 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3696 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3697 res_key, sa_res_key, PREEMPT);
3699 case PRO_PREEMPT_AND_ABORT:
3700 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3701 res_key, sa_res_key, PREEMPT_AND_ABORT);
3703 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3704 ret = core_scsi3_emulate_pro_register(cmd,
3705 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3707 case PRO_REGISTER_AND_MOVE:
3708 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3709 sa_res_key, aptpl, unreg);
3712 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3713 " action: 0x%02x\n", cdb[1] & 0x1f);
3714 return TCM_INVALID_CDB_FIELD;
3718 target_complete_cmd(cmd, GOOD);
3723 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3725 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3727 static sense_reason_t
3728 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3730 struct se_device *dev = cmd->se_dev;
3731 struct t10_pr_registration *pr_reg;
3733 u32 add_len = 0, off = 8;
3735 if (cmd->data_length < 8) {
3736 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3737 " too small\n", cmd->data_length);
3738 return TCM_INVALID_CDB_FIELD;
3741 buf = transport_kmap_data_sg(cmd);
3743 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3745 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3746 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3747 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3748 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3750 spin_lock(&dev->t10_pr.registration_lock);
3751 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3754 * Check for overflow of 8byte PRI READ_KEYS payload and
3755 * next reservation key list descriptor.
3757 if ((add_len + 8) > (cmd->data_length - 8))
3760 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3761 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3762 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3763 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3764 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3765 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3766 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3767 buf[off++] = (pr_reg->pr_res_key & 0xff);
3771 spin_unlock(&dev->t10_pr.registration_lock);
3773 buf[4] = ((add_len >> 24) & 0xff);
3774 buf[5] = ((add_len >> 16) & 0xff);
3775 buf[6] = ((add_len >> 8) & 0xff);
3776 buf[7] = (add_len & 0xff);
3778 transport_kunmap_data_sg(cmd);
3784 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3786 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3788 static sense_reason_t
3789 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3791 struct se_device *dev = cmd->se_dev;
3792 struct t10_pr_registration *pr_reg;
3795 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3797 if (cmd->data_length < 8) {
3798 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3799 " too small\n", cmd->data_length);
3800 return TCM_INVALID_CDB_FIELD;
3803 buf = transport_kmap_data_sg(cmd);
3805 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3807 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3808 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3809 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3810 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3812 spin_lock(&dev->dev_reservation_lock);
3813 pr_reg = dev->dev_pr_res_holder;
3816 * Set the hardcoded Additional Length
3818 buf[4] = ((add_len >> 24) & 0xff);
3819 buf[5] = ((add_len >> 16) & 0xff);
3820 buf[6] = ((add_len >> 8) & 0xff);
3821 buf[7] = (add_len & 0xff);
3823 if (cmd->data_length < 22)
3827 * Set the Reservation key.
3829 * From spc4r17, section 5.7.10:
3830 * A persistent reservation holder has its reservation key
3831 * returned in the parameter data from a PERSISTENT
3832 * RESERVE IN command with READ RESERVATION service action as
3834 * a) For a persistent reservation of the type Write Exclusive
3835 * - All Registrants or Exclusive Access  All Regitrants,
3836 * the reservation key shall be set to zero; or
3837 * b) For all other persistent reservation types, the
3838 * reservation key shall be set to the registered
3839 * reservation key for the I_T nexus that holds the
3840 * persistent reservation.
3842 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3843 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3846 pr_res_key = pr_reg->pr_res_key;
3848 buf[8] = ((pr_res_key >> 56) & 0xff);
3849 buf[9] = ((pr_res_key >> 48) & 0xff);
3850 buf[10] = ((pr_res_key >> 40) & 0xff);
3851 buf[11] = ((pr_res_key >> 32) & 0xff);
3852 buf[12] = ((pr_res_key >> 24) & 0xff);
3853 buf[13] = ((pr_res_key >> 16) & 0xff);
3854 buf[14] = ((pr_res_key >> 8) & 0xff);
3855 buf[15] = (pr_res_key & 0xff);
3857 * Set the SCOPE and TYPE
3859 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3860 (pr_reg->pr_res_type & 0x0f);
3864 spin_unlock(&dev->dev_reservation_lock);
3865 transport_kunmap_data_sg(cmd);
3871 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3873 * See spc4r17 section 6.13.4 Table 165
3875 static sense_reason_t
3876 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3878 struct se_device *dev = cmd->se_dev;
3879 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3881 u16 add_len = 8; /* Hardcoded to 8. */
3883 if (cmd->data_length < 6) {
3884 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3885 " %u too small\n", cmd->data_length);
3886 return TCM_INVALID_CDB_FIELD;
3889 buf = transport_kmap_data_sg(cmd);
3891 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3893 buf[0] = ((add_len >> 8) & 0xff);
3894 buf[1] = (add_len & 0xff);
3895 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3896 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3897 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3898 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3900 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3901 * set the TMV: Task Mask Valid bit.
3905 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3907 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3909 * PTPL_A: Persistence across Target Power Loss Active bit
3911 if (pr_tmpl->pr_aptpl_active)
3914 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3916 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3917 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3918 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3919 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3920 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3921 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3923 transport_kunmap_data_sg(cmd);
3929 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3931 * See spc4r17 section 6.13.5 Table 168 and 169
3933 static sense_reason_t
3934 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3936 struct se_device *dev = cmd->se_dev;
3937 struct se_node_acl *se_nacl;
3938 struct se_portal_group *se_tpg;
3939 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3940 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3942 u32 add_desc_len = 0, add_len = 0;
3943 u32 off = 8; /* off into first Full Status descriptor */
3944 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3945 int exp_desc_len, desc_len;
3946 bool all_reg = false;
3948 if (cmd->data_length < 8) {
3949 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3950 " too small\n", cmd->data_length);
3951 return TCM_INVALID_CDB_FIELD;
3954 buf = transport_kmap_data_sg(cmd);
3956 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3958 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3959 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3960 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3961 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3963 spin_lock(&dev->dev_reservation_lock);
3964 if (dev->dev_pr_res_holder) {
3965 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3967 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3968 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3970 pr_res_type = pr_holder->pr_res_type;
3971 pr_res_scope = pr_holder->pr_res_scope;
3974 spin_unlock(&dev->dev_reservation_lock);
3976 spin_lock(&pr_tmpl->registration_lock);
3977 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3978 &pr_tmpl->registration_list, pr_reg_list) {
3980 se_nacl = pr_reg->pr_reg_nacl;
3981 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3984 atomic_inc_mb(&pr_reg->pr_res_holders);
3985 spin_unlock(&pr_tmpl->registration_lock);
3987 * Determine expected length of $FABRIC_MOD specific
3988 * TransportID full status descriptor..
3990 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3992 if (exp_desc_len < 0 ||
3993 exp_desc_len + add_len > cmd->data_length) {
3994 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3995 " out of buffer: %d\n", cmd->data_length);
3996 spin_lock(&pr_tmpl->registration_lock);
3997 atomic_dec_mb(&pr_reg->pr_res_holders);
4001 * Set RESERVATION KEY
4003 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4004 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4005 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4006 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4007 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4008 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4009 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4010 buf[off++] = (pr_reg->pr_res_key & 0xff);
4011 off += 4; /* Skip Over Reserved area */
4014 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4016 if (pr_reg->pr_reg_all_tg_pt)
4019 * The struct se_lun pointer will be present for the
4020 * reservation holder for PR_HOLDER bit.
4022 * Also, if this registration is the reservation
4023 * holder or there is an All Registrants reservation
4024 * active, fill in SCOPE and TYPE in the next byte.
4026 if (pr_reg->pr_res_holder) {
4028 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4029 (pr_reg->pr_res_type & 0x0f);
4030 } else if (all_reg) {
4032 buf[off++] = (pr_res_scope & 0xf0) |
4033 (pr_res_type & 0x0f);
4038 off += 4; /* Skip over reserved area */
4040 * From spc4r17 6.3.15:
4042 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4043 * IDENTIFIER field contains the relative port identifier (see
4044 * 3.1.120) of the target port that is part of the I_T nexus
4045 * described by this full status descriptor. If the ALL_TG_PT
4046 * bit is set to one, the contents of the RELATIVE TARGET PORT
4047 * IDENTIFIER field are not defined by this standard.
4049 if (!pr_reg->pr_reg_all_tg_pt) {
4050 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
4052 buf[off++] = ((sep_rtpi >> 8) & 0xff);
4053 buf[off++] = (sep_rtpi & 0xff);
4055 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4057 buf[off+4] = se_tpg->proto_id;
4060 * Now, have the $FABRIC_MOD fill in the transport ID.
4062 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4063 &format_code, &buf[off+4]);
4065 spin_lock(&pr_tmpl->registration_lock);
4066 atomic_dec_mb(&pr_reg->pr_res_holders);
4071 * Set the ADDITIONAL DESCRIPTOR LENGTH
4073 buf[off++] = ((desc_len >> 24) & 0xff);
4074 buf[off++] = ((desc_len >> 16) & 0xff);
4075 buf[off++] = ((desc_len >> 8) & 0xff);
4076 buf[off++] = (desc_len & 0xff);
4078 * Size of full desctipor header minus TransportID
4079 * containing $FABRIC_MOD specific) initiator device/port
4082 * See spc4r17 Section 6.13.5 Table 169
4084 add_desc_len = (24 + desc_len);
4087 add_len += add_desc_len;
4089 spin_unlock(&pr_tmpl->registration_lock);
4091 * Set ADDITIONAL_LENGTH
4093 buf[4] = ((add_len >> 24) & 0xff);
4094 buf[5] = ((add_len >> 16) & 0xff);
4095 buf[6] = ((add_len >> 8) & 0xff);
4096 buf[7] = (add_len & 0xff);
4098 transport_kunmap_data_sg(cmd);
4104 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4109 * Following spc2r20 5.5.1 Reservations overview:
4111 * If a logical unit has been reserved by any RESERVE command and is
4112 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4113 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4114 * initiator or service action and shall terminate with a RESERVATION
4117 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4118 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4119 " SPC-2 reservation is held, returning"
4120 " RESERVATION_CONFLICT\n");
4121 return TCM_RESERVATION_CONFLICT;
4124 switch (cmd->t_task_cdb[1] & 0x1f) {
4126 ret = core_scsi3_pri_read_keys(cmd);
4128 case PRI_READ_RESERVATION:
4129 ret = core_scsi3_pri_read_reservation(cmd);
4131 case PRI_REPORT_CAPABILITIES:
4132 ret = core_scsi3_pri_report_capabilities(cmd);
4134 case PRI_READ_FULL_STATUS:
4135 ret = core_scsi3_pri_read_full_status(cmd);
4138 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4139 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4140 return TCM_INVALID_CDB_FIELD;
4144 target_complete_cmd(cmd, GOOD);
4149 target_check_reservation(struct se_cmd *cmd)
4151 struct se_device *dev = cmd->se_dev;
4156 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4158 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
4161 spin_lock(&dev->dev_reservation_lock);
4162 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4163 ret = target_scsi2_reservation_check(cmd);
4165 ret = target_scsi3_pr_reservation_check(cmd);
4166 spin_unlock(&dev->dev_reservation_lock);