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/file.h>
31 #include <scsi/scsi_proto.h>
32 #include <asm/unaligned.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_backend.h>
36 #include <target/target_core_fabric.h>
37 #include <target/target_core_configfs.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 {
48 struct t10_pr_registration *dest_pr_reg;
49 struct se_portal_group *dest_tpg;
50 struct se_node_acl *dest_node_acl;
51 struct se_dev_entry *dest_se_deve;
52 struct list_head dest_list;
55 void core_pr_dump_initiator_port(
56 struct t10_pr_registration *pr_reg,
60 if (!pr_reg->isid_present_at_reg)
63 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
68 REGISTER_AND_IGNORE_EXISTING_KEY,
77 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
78 struct t10_pr_registration *, int, int);
80 static int is_reservation_holder(
81 struct t10_pr_registration *pr_res_holder,
82 struct t10_pr_registration *pr_reg)
87 pr_res_type = pr_res_holder->pr_res_type;
89 return pr_res_holder == pr_reg ||
90 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
91 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
97 target_scsi2_reservation_check(struct se_cmd *cmd)
99 struct se_device *dev = cmd->se_dev;
100 struct se_session *sess = cmd->se_sess;
102 switch (cmd->t_task_cdb[0]) {
111 if (!dev->dev_reserved_node_acl || !sess)
114 if (dev->dev_reserved_node_acl != sess->se_node_acl)
115 return TCM_RESERVATION_CONFLICT;
117 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
118 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
119 return TCM_RESERVATION_CONFLICT;
125 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
126 struct se_node_acl *, struct se_session *);
127 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
129 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
131 struct se_session *se_sess = cmd->se_sess;
132 struct se_device *dev = cmd->se_dev;
133 struct t10_pr_registration *pr_reg;
134 struct t10_reservation *pr_tmpl = &dev->t10_pr;
137 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
141 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
144 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
145 * status, but no reservation shall be established and the
146 * persistent reservation shall not be changed, if the command
147 * is received from a) and b) below.
149 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
150 * status, but the persistent reservation shall not be released,
151 * if the command is received from a) and b)
153 * a) An I_T nexus that is a persistent reservation holder; or
154 * b) An I_T nexus that is registered if a registrants only or
155 * all registrants type persistent reservation is present.
157 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
158 * RELEASE(6) command, or RELEASE(10) command shall be processed
159 * as defined in SPC-2.
161 if (pr_reg->pr_res_holder) {
162 core_scsi3_put_pr_reg(pr_reg);
165 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
166 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
167 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
168 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
169 core_scsi3_put_pr_reg(pr_reg);
172 core_scsi3_put_pr_reg(pr_reg);
176 * Following spc2r20 5.5.1 Reservations overview:
178 * If a logical unit has executed a PERSISTENT RESERVE OUT
179 * command with the REGISTER or the REGISTER AND IGNORE
180 * EXISTING KEY service action and is still registered by any
181 * initiator, all RESERVE commands and all RELEASE commands
182 * regardless of initiator shall conflict and shall terminate
183 * with a RESERVATION CONFLICT status.
185 spin_lock(&pr_tmpl->registration_lock);
186 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
187 spin_unlock(&pr_tmpl->registration_lock);
191 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
192 " while active SPC-3 registrations exist,"
193 " returning RESERVATION_CONFLICT\n");
201 target_scsi2_reservation_release(struct se_cmd *cmd)
203 struct se_device *dev = cmd->se_dev;
204 struct se_session *sess = cmd->se_sess;
205 struct se_portal_group *tpg;
208 if (!sess || !sess->se_tpg)
210 rc = target_check_scsi2_reservation_conflict(cmd);
214 return TCM_RESERVATION_CONFLICT;
216 spin_lock(&dev->dev_reservation_lock);
217 if (!dev->dev_reserved_node_acl || !sess)
220 if (dev->dev_reserved_node_acl != sess->se_node_acl)
223 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
226 dev->dev_reserved_node_acl = NULL;
227 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
228 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
229 dev->dev_res_bin_isid = 0;
230 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
233 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
234 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
235 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
236 sess->se_node_acl->initiatorname);
239 spin_unlock(&dev->dev_reservation_lock);
241 target_complete_cmd(cmd, GOOD);
246 target_scsi2_reservation_reserve(struct se_cmd *cmd)
248 struct se_device *dev = cmd->se_dev;
249 struct se_session *sess = cmd->se_sess;
250 struct se_portal_group *tpg;
251 sense_reason_t ret = 0;
254 if ((cmd->t_task_cdb[1] & 0x01) &&
255 (cmd->t_task_cdb[1] & 0x02)) {
256 pr_err("LongIO and Obselete Bits set, returning"
257 " ILLEGAL_REQUEST\n");
258 return TCM_UNSUPPORTED_SCSI_OPCODE;
261 * This is currently the case for target_core_mod passthrough struct se_cmd
264 if (!sess || !sess->se_tpg)
266 rc = target_check_scsi2_reservation_conflict(cmd);
271 return TCM_RESERVATION_CONFLICT;
274 spin_lock(&dev->dev_reservation_lock);
275 if (dev->dev_reserved_node_acl &&
276 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
277 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
278 tpg->se_tpg_tfo->get_fabric_name());
279 pr_err("Original reserver LUN: %u %s\n",
280 cmd->se_lun->unpacked_lun,
281 dev->dev_reserved_node_acl->initiatorname);
282 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
283 " from %s \n", cmd->se_lun->unpacked_lun,
284 cmd->se_deve->mapped_lun,
285 sess->se_node_acl->initiatorname);
286 ret = TCM_RESERVATION_CONFLICT;
290 dev->dev_reserved_node_acl = sess->se_node_acl;
291 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
292 if (sess->sess_bin_isid != 0) {
293 dev->dev_res_bin_isid = sess->sess_bin_isid;
294 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
296 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
297 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
298 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
299 sess->se_node_acl->initiatorname);
302 spin_unlock(&dev->dev_reservation_lock);
305 target_complete_cmd(cmd, GOOD);
311 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
313 * This function is called by those initiator ports who are *NOT*
314 * the active PR reservation holder when a reservation is present.
316 static int core_scsi3_pr_seq_non_holder(
320 unsigned char *cdb = cmd->t_task_cdb;
321 struct se_dev_entry *se_deve;
322 struct se_session *se_sess = cmd->se_sess;
323 int other_cdb = 0, ignore_reg;
324 int registered_nexus = 0, ret = 1; /* Conflict by default */
325 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
326 int we = 0; /* Write Exclusive */
327 int legacy = 0; /* Act like a legacy device and return
328 * RESERVATION CONFLICT on some CDBs */
330 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
332 * Determine if the registration should be ignored due to
333 * non-matching ISIDs in target_scsi3_pr_reservation_check().
335 ignore_reg = (pr_reg_type & 0x80000000);
337 pr_reg_type &= ~0x80000000;
339 switch (pr_reg_type) {
340 case PR_TYPE_WRITE_EXCLUSIVE:
342 case PR_TYPE_EXCLUSIVE_ACCESS:
344 * Some commands are only allowed for the persistent reservation
347 if ((se_deve->def_pr_registered) && !(ignore_reg))
348 registered_nexus = 1;
350 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
352 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
354 * Some commands are only allowed for registered I_T Nexuses.
357 if ((se_deve->def_pr_registered) && !(ignore_reg))
358 registered_nexus = 1;
360 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
362 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
364 * Each registered I_T Nexus is a reservation holder.
367 if ((se_deve->def_pr_registered) && !(ignore_reg))
368 registered_nexus = 1;
374 * Referenced from spc4r17 table 45 for *NON* PR holder access
377 case SECURITY_PROTOCOL_IN:
378 if (registered_nexus)
386 case RECEIVE_DIAGNOSTIC:
391 if (registered_nexus) {
395 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
397 case PERSISTENT_RESERVE_OUT:
399 * This follows PERSISTENT_RESERVE_OUT service actions that
400 * are allowed in the presence of various reservations.
401 * See spc4r17, table 46
403 switch (cdb[1] & 0x1f) {
406 case PRO_PREEMPT_AND_ABORT:
407 ret = (registered_nexus) ? 0 : 1;
410 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
413 case PRO_REGISTER_AND_MOVE:
418 ret = (registered_nexus) ? 0 : 1;
421 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
422 " action: 0x%02x\n", cdb[1] & 0x1f);
428 /* Handled by CRH=1 in target_scsi2_reservation_release() */
433 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
436 case TEST_UNIT_READY:
437 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
440 switch (cdb[1] & 0x1f) {
441 case MI_MANAGEMENT_PROTOCOL_IN:
442 if (registered_nexus) {
446 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
448 case MI_REPORT_SUPPORTED_OPERATION_CODES:
449 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
454 if (registered_nexus) {
458 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
460 case MI_REPORT_ALIASES:
461 case MI_REPORT_IDENTIFYING_INFORMATION:
462 case MI_REPORT_PRIORITY:
463 case MI_REPORT_TARGET_PGS:
464 case MI_REPORT_TIMESTAMP:
465 ret = 0; /* Allowed */
468 pr_err("Unknown MI Service Action: 0x%02x\n",
473 case ACCESS_CONTROL_IN:
474 case ACCESS_CONTROL_OUT:
477 case SERVICE_ACTION_IN_12:
480 case PERSISTENT_RESERVE_IN:
481 ret = 0; /*/ Allowed CDBs */
488 * Case where the CDB is explicitly allowed in the above switch
491 if (!ret && !other_cdb) {
492 pr_debug("Allowing explicit CDB: 0x%02x for %s"
493 " reservation holder\n", cdb[0],
494 core_scsi3_pr_dump_type(pr_reg_type));
499 * Check if write exclusive initiator ports *NOT* holding the
500 * WRITE_EXCLUSIVE_* reservation.
502 if (we && !registered_nexus) {
503 if (cmd->data_direction == DMA_TO_DEVICE) {
505 * Conflict for write exclusive
507 pr_debug("%s Conflict for unregistered nexus"
508 " %s CDB: 0x%02x to %s reservation\n",
509 transport_dump_cmd_direction(cmd),
510 se_sess->se_node_acl->initiatorname, cdb[0],
511 core_scsi3_pr_dump_type(pr_reg_type));
515 * Allow non WRITE CDBs for all Write Exclusive
516 * PR TYPEs to pass for registered and
517 * non-registered_nexuxes NOT holding the reservation.
519 * We only make noise for the unregisterd nexuses,
520 * as we expect registered non-reservation holding
521 * nexuses to issue CDBs.
524 if (!registered_nexus) {
525 pr_debug("Allowing implicit CDB: 0x%02x"
526 " for %s reservation on unregistered"
528 core_scsi3_pr_dump_type(pr_reg_type));
533 } else if ((reg_only) || (all_reg)) {
534 if (registered_nexus) {
536 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
537 * allow commands from registered nexuses.
540 pr_debug("Allowing implicit CDB: 0x%02x for %s"
541 " reservation\n", cdb[0],
542 core_scsi3_pr_dump_type(pr_reg_type));
546 } else if (we && registered_nexus) {
548 * Reads are allowed for Write Exclusive locks
549 * from all registrants.
551 if (cmd->data_direction == DMA_FROM_DEVICE) {
552 pr_debug("Allowing READ CDB: 0x%02x for %s"
553 " reservation\n", cdb[0],
554 core_scsi3_pr_dump_type(pr_reg_type));
559 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
560 " for %s reservation\n", transport_dump_cmd_direction(cmd),
561 (registered_nexus) ? "" : "un",
562 se_sess->se_node_acl->initiatorname, cdb[0],
563 core_scsi3_pr_dump_type(pr_reg_type));
565 return 1; /* Conflict by default */
568 static sense_reason_t
569 target_scsi3_pr_reservation_check(struct se_cmd *cmd)
571 struct se_device *dev = cmd->se_dev;
572 struct se_session *sess = cmd->se_sess;
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 pr_reg_type |= 0x80000000;
587 goto check_nonholder;
594 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type))
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,
622 struct se_dev_entry *deve,
628 struct t10_pr_registration *pr_reg;
630 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
632 pr_err("Unable to allocate struct t10_pr_registration\n");
636 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
637 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
638 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
639 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
640 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
641 atomic_set(&pr_reg->pr_res_holders, 0);
642 pr_reg->pr_reg_nacl = nacl;
643 pr_reg->pr_reg_deve = deve;
644 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
645 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
646 pr_reg->pr_res_key = sa_res_key;
647 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
648 pr_reg->pr_reg_aptpl = aptpl;
649 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
651 * If an ISID value for this SCSI Initiator Port exists,
652 * save it to the registration now.
655 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
656 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
657 pr_reg->isid_present_at_reg = 1;
663 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
664 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
667 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
670 static struct t10_pr_registration *__core_scsi3_alloc_registration(
671 struct se_device *dev,
672 struct se_node_acl *nacl,
673 struct se_dev_entry *deve,
679 struct se_dev_entry *deve_tmp;
680 struct se_node_acl *nacl_tmp;
681 struct se_port *port, *port_tmp;
682 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
683 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
686 * Create a registration for the I_T Nexus upon which the
687 * PROUT REGISTER was received.
689 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
690 sa_res_key, all_tg_pt, aptpl);
694 * Return pointer to pr_reg for ALL_TG_PT=0
699 * Create list of matching SCSI Initiator Port registrations
702 spin_lock(&dev->se_port_lock);
703 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
704 atomic_inc_mb(&port->sep_tg_pt_ref_cnt);
705 spin_unlock(&dev->se_port_lock);
707 spin_lock_bh(&port->sep_alua_lock);
708 list_for_each_entry(deve_tmp, &port->sep_alua_list,
711 * This pointer will be NULL for demo mode MappedLUNs
712 * that have not been make explicit via a ConfigFS
713 * MappedLUN group for the SCSI Initiator Node ACL.
715 if (!deve_tmp->se_lun_acl)
718 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
720 * Skip the matching struct se_node_acl that is allocated
723 if (nacl == nacl_tmp)
726 * Only perform PR registrations for target ports on
727 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
730 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
733 * Look for a matching Initiator Node ACL in ASCII format
735 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
738 atomic_inc_mb(&deve_tmp->pr_ref_count);
739 spin_unlock_bh(&port->sep_alua_lock);
741 * Grab a configfs group dependency that is released
742 * for the exception path at label out: below, or upon
743 * completion of adding ALL_TG_PT=1 registrations in
744 * __core_scsi3_add_registration()
746 ret = core_scsi3_lunacl_depend_item(deve_tmp);
748 pr_err("core_scsi3_lunacl_depend"
750 atomic_dec_mb(&port->sep_tg_pt_ref_cnt);
751 atomic_dec_mb(&deve_tmp->pr_ref_count);
755 * Located a matching SCSI Initiator Port on a different
756 * port, allocate the pr_reg_atp and attach it to the
757 * pr_reg->pr_reg_atp_list that will be processed once
758 * the original *pr_reg is processed in
759 * __core_scsi3_add_registration()
761 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
762 nacl_tmp, deve_tmp, NULL,
763 sa_res_key, all_tg_pt, aptpl);
765 atomic_dec_mb(&port->sep_tg_pt_ref_cnt);
766 atomic_dec_mb(&deve_tmp->pr_ref_count);
767 core_scsi3_lunacl_undepend_item(deve_tmp);
771 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
772 &pr_reg->pr_reg_atp_list);
773 spin_lock_bh(&port->sep_alua_lock);
775 spin_unlock_bh(&port->sep_alua_lock);
777 spin_lock(&dev->se_port_lock);
778 atomic_dec_mb(&port->sep_tg_pt_ref_cnt);
780 spin_unlock(&dev->se_port_lock);
784 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
785 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
786 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
787 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
788 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
790 kmem_cache_free(t10_pr_reg_cache, pr_reg);
794 int core_scsi3_alloc_aptpl_registration(
795 struct t10_reservation *pr_tmpl,
797 unsigned char *i_port,
800 unsigned char *t_port,
807 struct t10_pr_registration *pr_reg;
809 if (!i_port || !t_port || !sa_res_key) {
810 pr_err("Illegal parameters for APTPL registration\n");
814 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
816 pr_err("Unable to allocate struct t10_pr_registration\n");
820 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
821 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
822 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
823 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
824 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
825 atomic_set(&pr_reg->pr_res_holders, 0);
826 pr_reg->pr_reg_nacl = NULL;
827 pr_reg->pr_reg_deve = NULL;
828 pr_reg->pr_res_mapped_lun = mapped_lun;
829 pr_reg->pr_aptpl_target_lun = target_lun;
830 pr_reg->pr_res_key = sa_res_key;
831 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
832 pr_reg->pr_reg_aptpl = 1;
833 pr_reg->pr_reg_tg_pt_lun = NULL;
834 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
835 pr_reg->pr_res_type = type;
837 * If an ISID value had been saved in APTPL metadata for this
838 * SCSI Initiator Port, restore it now.
841 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
842 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
843 pr_reg->isid_present_at_reg = 1;
846 * Copy the i_port and t_port information from caller.
848 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
849 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
850 pr_reg->pr_reg_tpgt = tpgt;
852 * Set pr_res_holder from caller, the pr_reg who is the reservation
853 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
854 * the Initiator Node LUN ACL from the fabric module is created for
857 pr_reg->pr_res_holder = res_holder;
859 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
860 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
861 " metadata\n", (res_holder) ? "+reservation" : "");
865 static void core_scsi3_aptpl_reserve(
866 struct se_device *dev,
867 struct se_portal_group *tpg,
868 struct se_node_acl *node_acl,
869 struct t10_pr_registration *pr_reg)
871 char i_buf[PR_REG_ISID_ID_LEN];
873 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
874 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
876 spin_lock(&dev->dev_reservation_lock);
877 dev->dev_pr_res_holder = pr_reg;
878 spin_unlock(&dev->dev_reservation_lock);
880 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
881 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
882 tpg->se_tpg_tfo->get_fabric_name(),
883 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
884 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
885 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
886 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
890 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
891 struct t10_pr_registration *, enum register_type, int);
893 static int __core_scsi3_check_aptpl_registration(
894 struct se_device *dev,
895 struct se_portal_group *tpg,
898 struct se_node_acl *nacl,
899 struct se_dev_entry *deve)
901 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
902 struct t10_reservation *pr_tmpl = &dev->t10_pr;
903 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
904 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
907 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
908 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
910 * Copy Initiator Port information from struct se_node_acl
912 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
913 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
914 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
915 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
917 * Look for the matching registrations+reservation from those
918 * created from APTPL metadata. Note that multiple registrations
919 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
922 spin_lock(&pr_tmpl->aptpl_reg_lock);
923 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
926 if (!strcmp(pr_reg->pr_iport, i_port) &&
927 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
928 !(strcmp(pr_reg->pr_tport, t_port)) &&
929 (pr_reg->pr_reg_tpgt == tpgt) &&
930 (pr_reg->pr_aptpl_target_lun == target_lun)) {
932 pr_reg->pr_reg_nacl = nacl;
933 pr_reg->pr_reg_deve = deve;
934 pr_reg->pr_reg_tg_pt_lun = lun;
936 list_del(&pr_reg->pr_reg_aptpl_list);
937 spin_unlock(&pr_tmpl->aptpl_reg_lock);
939 * At this point all of the pointers in *pr_reg will
940 * be setup, so go ahead and add the registration.
943 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
945 * If this registration is the reservation holder,
946 * make that happen now..
948 if (pr_reg->pr_res_holder)
949 core_scsi3_aptpl_reserve(dev, tpg,
952 * Reenable pr_aptpl_active to accept new metadata
953 * updates once the SCSI device is active again..
955 spin_lock(&pr_tmpl->aptpl_reg_lock);
956 pr_tmpl->pr_aptpl_active = 1;
959 spin_unlock(&pr_tmpl->aptpl_reg_lock);
964 int core_scsi3_check_aptpl_registration(
965 struct se_device *dev,
966 struct se_portal_group *tpg,
968 struct se_node_acl *nacl,
971 struct se_dev_entry *deve = nacl->device_list[mapped_lun];
973 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
976 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
977 lun->unpacked_lun, nacl, deve);
980 static void __core_scsi3_dump_registration(
981 const struct target_core_fabric_ops *tfo,
982 struct se_device *dev,
983 struct se_node_acl *nacl,
984 struct t10_pr_registration *pr_reg,
985 enum register_type register_type)
987 struct se_portal_group *se_tpg = nacl->se_tpg;
988 char i_buf[PR_REG_ISID_ID_LEN];
990 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
991 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
993 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
994 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
995 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
996 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
998 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
999 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1000 tfo->tpg_get_tag(se_tpg));
1001 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1002 " Port(s)\n", tfo->get_fabric_name(),
1003 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1004 dev->transport->name);
1005 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1006 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1007 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1008 pr_reg->pr_reg_aptpl);
1012 * this function can be called with struct se_device->dev_reservation_lock
1013 * when register_move = 1
1015 static void __core_scsi3_add_registration(
1016 struct se_device *dev,
1017 struct se_node_acl *nacl,
1018 struct t10_pr_registration *pr_reg,
1019 enum register_type register_type,
1022 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1023 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1024 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1027 * Increment PRgeneration counter for struct se_device upon a successful
1028 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1030 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1031 * action, the struct se_device->dev_reservation_lock will already be held,
1032 * so we do not call core_scsi3_pr_generation() which grabs the lock
1035 pr_reg->pr_res_generation = (register_move) ?
1036 dev->t10_pr.pr_generation++ :
1037 core_scsi3_pr_generation(dev);
1039 spin_lock(&pr_tmpl->registration_lock);
1040 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1041 pr_reg->pr_reg_deve->def_pr_registered = 1;
1043 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1044 spin_unlock(&pr_tmpl->registration_lock);
1046 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1048 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1051 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1052 * allocated in __core_scsi3_alloc_registration()
1054 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1055 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1056 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1058 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1060 spin_lock(&pr_tmpl->registration_lock);
1061 list_add_tail(&pr_reg_tmp->pr_reg_list,
1062 &pr_tmpl->registration_list);
1063 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1065 __core_scsi3_dump_registration(tfo, dev,
1066 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1068 spin_unlock(&pr_tmpl->registration_lock);
1070 * Drop configfs group dependency reference from
1071 * __core_scsi3_alloc_registration()
1073 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1077 static int core_scsi3_alloc_registration(
1078 struct se_device *dev,
1079 struct se_node_acl *nacl,
1080 struct se_dev_entry *deve,
1081 unsigned char *isid,
1085 enum register_type register_type,
1088 struct t10_pr_registration *pr_reg;
1090 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1091 sa_res_key, all_tg_pt, aptpl);
1095 __core_scsi3_add_registration(dev, nacl, pr_reg,
1096 register_type, register_move);
1100 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1101 struct se_device *dev,
1102 struct se_node_acl *nacl,
1103 unsigned char *isid)
1105 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1106 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1107 struct se_portal_group *tpg;
1109 spin_lock(&pr_tmpl->registration_lock);
1110 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1111 &pr_tmpl->registration_list, pr_reg_list) {
1113 * First look for a matching struct se_node_acl
1115 if (pr_reg->pr_reg_nacl != nacl)
1118 tpg = pr_reg->pr_reg_nacl->se_tpg;
1120 * If this registration does NOT contain a fabric provided
1121 * ISID, then we have found a match.
1123 if (!pr_reg->isid_present_at_reg) {
1125 * Determine if this SCSI device server requires that
1126 * SCSI Intiatior TransportID w/ ISIDs is enforced
1127 * for fabric modules (iSCSI) requiring them.
1129 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1130 if (dev->dev_attrib.enforce_pr_isids)
1133 atomic_inc_mb(&pr_reg->pr_res_holders);
1134 spin_unlock(&pr_tmpl->registration_lock);
1138 * If the *pr_reg contains a fabric defined ISID for multi-value
1139 * SCSI Initiator Port TransportIDs, then we expect a valid
1140 * matching ISID to be provided by the local SCSI Initiator Port.
1144 if (strcmp(isid, pr_reg->pr_reg_isid))
1147 atomic_inc_mb(&pr_reg->pr_res_holders);
1148 spin_unlock(&pr_tmpl->registration_lock);
1151 spin_unlock(&pr_tmpl->registration_lock);
1156 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1157 struct se_device *dev,
1158 struct se_node_acl *nacl,
1159 struct se_session *sess)
1161 struct se_portal_group *tpg = nacl->se_tpg;
1162 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1164 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1165 memset(&buf[0], 0, PR_REG_ISID_LEN);
1166 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1171 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1174 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1176 atomic_dec_mb(&pr_reg->pr_res_holders);
1179 static int core_scsi3_check_implicit_release(
1180 struct se_device *dev,
1181 struct t10_pr_registration *pr_reg)
1183 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1184 struct t10_pr_registration *pr_res_holder;
1187 spin_lock(&dev->dev_reservation_lock);
1188 pr_res_holder = dev->dev_pr_res_holder;
1189 if (!pr_res_holder) {
1190 spin_unlock(&dev->dev_reservation_lock);
1193 if (pr_res_holder == pr_reg) {
1195 * Perform an implicit RELEASE if the registration that
1196 * is being released is holding the reservation.
1198 * From spc4r17, section 5.7.11.1:
1200 * e) If the I_T nexus is the persistent reservation holder
1201 * and the persistent reservation is not an all registrants
1202 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1203 * service action or REGISTER AND IGNORE EXISTING KEY
1204 * service action with the SERVICE ACTION RESERVATION KEY
1205 * field set to zero (see 5.7.11.3).
1207 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1210 * For 'All Registrants' reservation types, all existing
1211 * registrations are still processed as reservation holders
1212 * in core_scsi3_pr_seq_non_holder() after the initial
1213 * reservation holder is implicitly released here.
1215 } else if (pr_reg->pr_reg_all_tg_pt &&
1216 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1217 pr_reg->pr_reg_nacl->initiatorname)) &&
1218 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1219 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1220 " UNREGISTER while existing reservation with matching"
1221 " key 0x%016Lx is present from another SCSI Initiator"
1222 " Port\n", pr_reg->pr_res_key);
1225 spin_unlock(&dev->dev_reservation_lock);
1231 * Called with struct t10_reservation->registration_lock held.
1233 static void __core_scsi3_free_registration(
1234 struct se_device *dev,
1235 struct t10_pr_registration *pr_reg,
1236 struct list_head *preempt_and_abort_list,
1238 __releases(&pr_tmpl->registration_lock)
1239 __acquires(&pr_tmpl->registration_lock)
1241 const struct target_core_fabric_ops *tfo =
1242 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1243 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1244 char i_buf[PR_REG_ISID_ID_LEN];
1246 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1247 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1249 pr_reg->pr_reg_deve->def_pr_registered = 0;
1250 pr_reg->pr_reg_deve->pr_res_key = 0;
1251 if (!list_empty(&pr_reg->pr_reg_list))
1252 list_del(&pr_reg->pr_reg_list);
1254 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1255 * so call core_scsi3_put_pr_reg() to decrement our reference.
1258 core_scsi3_put_pr_reg(pr_reg);
1260 * Wait until all reference from any other I_T nexuses for this
1261 * *pr_reg have been released. Because list_del() is called above,
1262 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1263 * count back to zero, and we release *pr_reg.
1265 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1266 spin_unlock(&pr_tmpl->registration_lock);
1267 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1268 tfo->get_fabric_name());
1270 spin_lock(&pr_tmpl->registration_lock);
1273 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1274 " Node: %s%s\n", tfo->get_fabric_name(),
1275 pr_reg->pr_reg_nacl->initiatorname,
1277 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1278 " Port(s)\n", tfo->get_fabric_name(),
1279 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1280 dev->transport->name);
1281 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1282 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1283 pr_reg->pr_res_generation);
1285 if (!preempt_and_abort_list) {
1286 pr_reg->pr_reg_deve = NULL;
1287 pr_reg->pr_reg_nacl = NULL;
1288 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1292 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1293 * are released once the ABORT_TASK_SET has completed..
1295 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1298 void core_scsi3_free_pr_reg_from_nacl(
1299 struct se_device *dev,
1300 struct se_node_acl *nacl)
1302 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1303 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1304 bool free_reg = false;
1306 * If the passed se_node_acl matches the reservation holder,
1307 * release the reservation.
1309 spin_lock(&dev->dev_reservation_lock);
1310 pr_res_holder = dev->dev_pr_res_holder;
1311 if ((pr_res_holder != NULL) &&
1312 (pr_res_holder->pr_reg_nacl == nacl)) {
1313 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1316 spin_unlock(&dev->dev_reservation_lock);
1318 * Release any registration associated with the struct se_node_acl.
1320 spin_lock(&pr_tmpl->registration_lock);
1321 if (pr_res_holder && free_reg)
1322 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1324 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1325 &pr_tmpl->registration_list, pr_reg_list) {
1327 if (pr_reg->pr_reg_nacl != nacl)
1330 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1332 spin_unlock(&pr_tmpl->registration_lock);
1335 void core_scsi3_free_all_registrations(
1336 struct se_device *dev)
1338 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1339 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1341 spin_lock(&dev->dev_reservation_lock);
1342 pr_res_holder = dev->dev_pr_res_holder;
1343 if (pr_res_holder != NULL) {
1344 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1345 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1346 pr_res_holder, 0, 0);
1348 spin_unlock(&dev->dev_reservation_lock);
1350 spin_lock(&pr_tmpl->registration_lock);
1351 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1352 &pr_tmpl->registration_list, pr_reg_list) {
1354 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1356 spin_unlock(&pr_tmpl->registration_lock);
1358 spin_lock(&pr_tmpl->aptpl_reg_lock);
1359 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1360 pr_reg_aptpl_list) {
1361 list_del(&pr_reg->pr_reg_aptpl_list);
1362 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1364 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1367 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1369 return target_depend_item(&tpg->tpg_group.cg_item);
1372 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1374 target_undepend_item(&tpg->tpg_group.cg_item);
1375 atomic_dec_mb(&tpg->tpg_pr_ref_count);
1378 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1380 if (nacl->dynamic_node_acl)
1382 return target_depend_item(&nacl->acl_group.cg_item);
1385 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1387 if (!nacl->dynamic_node_acl)
1388 target_undepend_item(&nacl->acl_group.cg_item);
1389 atomic_dec_mb(&nacl->acl_pr_ref_count);
1392 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1394 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1395 struct se_node_acl *nacl;
1396 struct se_portal_group *tpg;
1398 * For nacl->dynamic_node_acl=1
1403 nacl = lun_acl->se_lun_nacl;
1406 return target_depend_item(&lun_acl->se_lun_group.cg_item);
1409 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1411 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1412 struct se_node_acl *nacl;
1413 struct se_portal_group *tpg;
1415 * For nacl->dynamic_node_acl=1
1418 atomic_dec_mb(&se_deve->pr_ref_count);
1421 nacl = lun_acl->se_lun_nacl;
1424 target_undepend_item(&lun_acl->se_lun_group.cg_item);
1425 atomic_dec_mb(&se_deve->pr_ref_count);
1428 static sense_reason_t
1429 core_scsi3_decode_spec_i_port(
1431 struct se_portal_group *tpg,
1432 unsigned char *l_isid,
1437 struct se_device *dev = cmd->se_dev;
1438 struct se_port *tmp_port;
1439 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1440 struct se_session *se_sess = cmd->se_sess;
1441 struct se_node_acl *dest_node_acl = NULL;
1442 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1443 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1444 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1445 LIST_HEAD(tid_dest_list);
1446 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1447 const struct target_core_fabric_ops *tmp_tf_ops;
1449 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1450 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1452 u32 tpdl, tid_len = 0;
1453 int dest_local_nexus;
1456 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1458 * Allocate a struct pr_transport_id_holder and setup the
1459 * local_node_acl and local_se_deve pointers and add to
1460 * struct list_head tid_dest_list for add registration
1461 * processing in the loop of tid_dest_list below.
1463 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1465 pr_err("Unable to allocate tidh_new\n");
1466 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1468 INIT_LIST_HEAD(&tidh_new->dest_list);
1469 tidh_new->dest_tpg = tpg;
1470 tidh_new->dest_node_acl = se_sess->se_node_acl;
1471 tidh_new->dest_se_deve = local_se_deve;
1473 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1474 se_sess->se_node_acl, local_se_deve, l_isid,
1475 sa_res_key, all_tg_pt, aptpl);
1476 if (!local_pr_reg) {
1478 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1480 tidh_new->dest_pr_reg = local_pr_reg;
1482 * The local I_T nexus does not hold any configfs dependances,
1483 * so we set tid_h->dest_local_nexus=1 to prevent the
1484 * configfs_undepend_item() calls in the tid_dest_list loops below.
1486 tidh_new->dest_local_nexus = 1;
1487 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1489 if (cmd->data_length < 28) {
1490 pr_warn("SPC-PR: Received PR OUT parameter list"
1491 " length too small: %u\n", cmd->data_length);
1492 ret = TCM_INVALID_PARAMETER_LIST;
1496 buf = transport_kmap_data_sg(cmd);
1498 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1503 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1504 * first extract TransportID Parameter Data Length, and make sure
1505 * the value matches up to the SCSI expected data transfer length.
1507 tpdl = (buf[24] & 0xff) << 24;
1508 tpdl |= (buf[25] & 0xff) << 16;
1509 tpdl |= (buf[26] & 0xff) << 8;
1510 tpdl |= buf[27] & 0xff;
1512 if ((tpdl + 28) != cmd->data_length) {
1513 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1514 " does not equal CDB data_length: %u\n", tpdl,
1516 ret = TCM_INVALID_PARAMETER_LIST;
1520 * Start processing the received transport IDs using the
1521 * receiving I_T Nexus portal's fabric dependent methods to
1522 * obtain the SCSI Initiator Port/Device Identifiers.
1527 proto_ident = (ptr[0] & 0x0f);
1530 spin_lock(&dev->se_port_lock);
1531 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1532 tmp_tpg = tmp_port->sep_tpg;
1535 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1538 if (!tmp_tf_ops->get_fabric_proto_ident ||
1539 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1542 * Look for the matching proto_ident provided by
1543 * the received TransportID
1545 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1546 if (tmp_proto_ident != proto_ident)
1548 dest_rtpi = tmp_port->sep_rtpi;
1550 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1551 tmp_tpg, (const char *)ptr, &tid_len,
1556 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1557 spin_unlock(&dev->se_port_lock);
1559 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1560 pr_err(" core_scsi3_tpg_depend_item()"
1562 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1563 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1567 * Locate the destination initiator ACL to be registered
1568 * from the decoded fabric module specific TransportID
1571 spin_lock_irq(&tmp_tpg->acl_node_lock);
1572 dest_node_acl = __core_tpg_get_initiator_node_acl(
1575 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1576 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1578 if (!dest_node_acl) {
1579 core_scsi3_tpg_undepend_item(tmp_tpg);
1580 spin_lock(&dev->se_port_lock);
1584 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1585 pr_err("configfs_depend_item() failed"
1586 " for dest_node_acl->acl_group\n");
1587 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1588 core_scsi3_tpg_undepend_item(tmp_tpg);
1589 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1594 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1595 " %s Port RTPI: %hu\n",
1596 dest_tpg->se_tpg_tfo->get_fabric_name(),
1597 dest_node_acl->initiatorname, dest_rtpi);
1599 spin_lock(&dev->se_port_lock);
1602 spin_unlock(&dev->se_port_lock);
1605 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1607 ret = TCM_INVALID_PARAMETER_LIST;
1611 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1612 " tid_len: %d for %s + %s\n",
1613 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1614 tpdl, tid_len, i_str, iport_ptr);
1616 if (tid_len > tpdl) {
1617 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1618 " %u for Transport ID: %s\n", tid_len, ptr);
1619 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1620 core_scsi3_tpg_undepend_item(dest_tpg);
1621 ret = TCM_INVALID_PARAMETER_LIST;
1625 * Locate the desintation struct se_dev_entry pointer for matching
1626 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1629 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1631 if (!dest_se_deve) {
1632 pr_err("Unable to locate %s dest_se_deve"
1633 " from destination RTPI: %hu\n",
1634 dest_tpg->se_tpg_tfo->get_fabric_name(),
1637 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1638 core_scsi3_tpg_undepend_item(dest_tpg);
1639 ret = TCM_INVALID_PARAMETER_LIST;
1643 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1644 pr_err("core_scsi3_lunacl_depend_item()"
1646 atomic_dec_mb(&dest_se_deve->pr_ref_count);
1647 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1648 core_scsi3_tpg_undepend_item(dest_tpg);
1649 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1653 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1654 " dest_se_deve mapped_lun: %u\n",
1655 dest_tpg->se_tpg_tfo->get_fabric_name(),
1656 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1659 * Skip any TransportIDs that already have a registration for
1662 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1665 core_scsi3_put_pr_reg(pr_reg_e);
1666 core_scsi3_lunacl_undepend_item(dest_se_deve);
1667 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1668 core_scsi3_tpg_undepend_item(dest_tpg);
1675 * Allocate a struct pr_transport_id_holder and setup
1676 * the dest_node_acl and dest_se_deve pointers for the
1679 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1682 pr_err("Unable to allocate tidh_new\n");
1683 core_scsi3_lunacl_undepend_item(dest_se_deve);
1684 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1685 core_scsi3_tpg_undepend_item(dest_tpg);
1686 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1689 INIT_LIST_HEAD(&tidh_new->dest_list);
1690 tidh_new->dest_tpg = dest_tpg;
1691 tidh_new->dest_node_acl = dest_node_acl;
1692 tidh_new->dest_se_deve = dest_se_deve;
1695 * Allocate, but do NOT add the registration for the
1696 * TransportID referenced SCSI Initiator port. This
1697 * done because of the following from spc4r17 in section
1698 * 6.14.3 wrt SPEC_I_PT:
1700 * "If a registration fails for any initiator port (e.g., if th
1701 * logical unit does not have enough resources available to
1702 * hold the registration information), no registrations shall be
1703 * made, and the command shall be terminated with
1704 * CHECK CONDITION status."
1706 * That means we call __core_scsi3_alloc_registration() here,
1707 * and then call __core_scsi3_add_registration() in the
1708 * 2nd loop which will never fail.
1710 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1711 dest_node_acl, dest_se_deve, iport_ptr,
1712 sa_res_key, all_tg_pt, aptpl);
1714 core_scsi3_lunacl_undepend_item(dest_se_deve);
1715 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1716 core_scsi3_tpg_undepend_item(dest_tpg);
1718 ret = TCM_INVALID_PARAMETER_LIST;
1721 tidh_new->dest_pr_reg = dest_pr_reg;
1722 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1730 transport_kunmap_data_sg(cmd);
1733 * Go ahead and create a registrations from tid_dest_list for the
1734 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1737 * The SA Reservation Key from the PROUT is set for the
1738 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1739 * means that the TransportID Initiator port will be
1740 * registered on all of the target ports in the SCSI target device
1741 * ALL_TG_PT=0 means the registration will only be for the
1742 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1745 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1746 dest_tpg = tidh->dest_tpg;
1747 dest_node_acl = tidh->dest_node_acl;
1748 dest_se_deve = tidh->dest_se_deve;
1749 dest_pr_reg = tidh->dest_pr_reg;
1750 dest_local_nexus = tidh->dest_local_nexus;
1752 list_del(&tidh->dest_list);
1755 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1756 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1758 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1761 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1762 " registered Transport ID for Node: %s%s Mapped LUN:"
1763 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1764 dest_node_acl->initiatorname, i_buf, dest_se_deve->mapped_lun);
1766 if (dest_local_nexus)
1769 core_scsi3_lunacl_undepend_item(dest_se_deve);
1770 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1771 core_scsi3_tpg_undepend_item(dest_tpg);
1776 transport_kunmap_data_sg(cmd);
1779 * For the failure case, release everything from tid_dest_list
1780 * including *dest_pr_reg and the configfs dependances..
1782 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1783 dest_tpg = tidh->dest_tpg;
1784 dest_node_acl = tidh->dest_node_acl;
1785 dest_se_deve = tidh->dest_se_deve;
1786 dest_pr_reg = tidh->dest_pr_reg;
1787 dest_local_nexus = tidh->dest_local_nexus;
1789 list_del(&tidh->dest_list);
1792 * Release any extra ALL_TG_PT=1 registrations for
1793 * the SPEC_I_PT=1 case.
1795 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1796 &dest_pr_reg->pr_reg_atp_list,
1797 pr_reg_atp_mem_list) {
1798 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1799 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1800 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1803 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1805 if (dest_local_nexus)
1808 core_scsi3_lunacl_undepend_item(dest_se_deve);
1809 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1810 core_scsi3_tpg_undepend_item(dest_tpg);
1815 static int core_scsi3_update_aptpl_buf(
1816 struct se_device *dev,
1818 u32 pr_aptpl_buf_len)
1821 struct se_portal_group *tpg;
1822 struct t10_pr_registration *pr_reg;
1823 unsigned char tmp[512], isid_buf[32];
1828 spin_lock(&dev->dev_reservation_lock);
1829 spin_lock(&dev->t10_pr.registration_lock);
1831 * Walk the registration list..
1833 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1838 tpg = pr_reg->pr_reg_nacl->se_tpg;
1839 lun = pr_reg->pr_reg_tg_pt_lun;
1841 * Write out any ISID value to APTPL metadata that was included
1842 * in the original registration.
1844 if (pr_reg->isid_present_at_reg)
1845 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1846 pr_reg->pr_reg_isid);
1848 * Include special metadata if the pr_reg matches the
1849 * reservation holder.
1851 if (dev->dev_pr_res_holder == pr_reg) {
1852 snprintf(tmp, 512, "PR_REG_START: %d"
1853 "\ninitiator_fabric=%s\n"
1854 "initiator_node=%s\n%s"
1856 "res_holder=1\nres_type=%02x\n"
1857 "res_scope=%02x\nres_all_tg_pt=%d\n"
1858 "mapped_lun=%u\n", reg_count,
1859 tpg->se_tpg_tfo->get_fabric_name(),
1860 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1861 pr_reg->pr_res_key, pr_reg->pr_res_type,
1862 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1863 pr_reg->pr_res_mapped_lun);
1865 snprintf(tmp, 512, "PR_REG_START: %d\n"
1866 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1867 "sa_res_key=%llu\nres_holder=0\n"
1868 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1869 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1870 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1871 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1872 pr_reg->pr_res_mapped_lun);
1875 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1876 pr_err("Unable to update renaming APTPL metadata,"
1877 " reallocating larger buffer\n");
1881 len += sprintf(buf+len, "%s", tmp);
1884 * Include information about the associated SCSI target port.
1886 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1887 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1888 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1889 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1890 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1891 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1893 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1894 pr_err("Unable to update renaming APTPL metadata,"
1895 " reallocating larger buffer\n");
1899 len += sprintf(buf+len, "%s", tmp);
1904 len += sprintf(buf+len, "No Registrations or Reservations");
1907 spin_unlock(&dev->t10_pr.registration_lock);
1908 spin_unlock(&dev->dev_reservation_lock);
1913 static int __core_scsi3_write_aptpl_to_file(
1914 struct se_device *dev,
1917 struct t10_wwn *wwn = &dev->t10_wwn;
1919 int flags = O_RDWR | O_CREAT | O_TRUNC;
1921 u32 pr_aptpl_buf_len;
1924 memset(path, 0, 512);
1926 if (strlen(&wwn->unit_serial[0]) >= 512) {
1927 pr_err("WWN value for struct se_device does not fit"
1928 " into path buffer\n");
1932 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1933 file = filp_open(path, flags, 0600);
1935 pr_err("filp_open(%s) for APTPL metadata"
1937 return PTR_ERR(file);
1940 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
1942 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
1945 pr_debug("Error writing APTPL metadata file: %s\n", path);
1948 return (ret < 0) ? -EIO : 0;
1952 * Clear the APTPL metadata if APTPL has been disabled, otherwise
1953 * write out the updated metadata to struct file for this SCSI device.
1955 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
1958 int rc, len = PR_APTPL_BUF_LEN;
1961 char *null_buf = "No Registrations or Reservations\n";
1963 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
1964 dev->t10_pr.pr_aptpl_active = 0;
1965 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
1968 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1975 return TCM_OUT_OF_RESOURCES;
1977 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
1984 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
1986 pr_err("SPC-3 PR: Could not update APTPL\n");
1988 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1990 dev->t10_pr.pr_aptpl_active = 1;
1992 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
1996 static sense_reason_t
1997 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
1998 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2000 struct se_session *se_sess = cmd->se_sess;
2001 struct se_device *dev = cmd->se_dev;
2002 struct se_dev_entry *se_deve;
2003 struct se_lun *se_lun = cmd->se_lun;
2004 struct se_portal_group *se_tpg;
2005 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2006 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2007 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2008 sense_reason_t ret = TCM_NO_SENSE;
2009 int pr_holder = 0, type;
2011 if (!se_sess || !se_lun) {
2012 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2013 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2015 se_tpg = se_sess->se_tpg;
2016 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2018 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2019 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2020 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2022 isid_ptr = &isid_buf[0];
2025 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2027 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2030 pr_warn("SPC-3 PR: Reservation Key non-zero"
2031 " for SA REGISTER, returning CONFLICT\n");
2032 return TCM_RESERVATION_CONFLICT;
2035 * Do nothing but return GOOD status.
2042 * Perform the Service Action REGISTER on the Initiator
2043 * Port Endpoint that the PRO was received from on the
2044 * Logical Unit of the SCSI device server.
2046 if (core_scsi3_alloc_registration(cmd->se_dev,
2047 se_sess->se_node_acl, se_deve, isid_ptr,
2048 sa_res_key, all_tg_pt, aptpl,
2049 register_type, 0)) {
2050 pr_err("Unable to allocate"
2051 " struct t10_pr_registration\n");
2052 return TCM_INVALID_PARAMETER_LIST;
2056 * Register both the Initiator port that received
2057 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2058 * TransportID from Parameter list and loop through
2059 * fabric dependent parameter list while calling
2060 * logic from of core_scsi3_alloc_registration() for
2061 * each TransportID provided SCSI Initiator Port/Device
2063 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2064 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2069 return core_scsi3_update_and_write_aptpl(dev, aptpl);
2072 /* ok, existing registration */
2074 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2075 pr_err("SPC-3 PR REGISTER: Received"
2076 " res_key: 0x%016Lx does not match"
2077 " existing SA REGISTER res_key:"
2078 " 0x%016Lx\n", res_key,
2079 pr_reg->pr_res_key);
2080 ret = TCM_RESERVATION_CONFLICT;
2085 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2086 " set on a registered nexus\n");
2087 ret = TCM_INVALID_PARAMETER_LIST;
2092 * An existing ALL_TG_PT=1 registration being released
2093 * must also set ALL_TG_PT=1 in the incoming PROUT.
2095 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2096 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2097 " registration exists, but ALL_TG_PT=1 bit not"
2098 " present in received PROUT\n");
2099 ret = TCM_INVALID_CDB_FIELD;
2104 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2108 * Increment PRgeneration counter for struct se_device"
2109 * upon a successful REGISTER, see spc4r17 section 6.3.2
2110 * READ_KEYS service action.
2112 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2113 pr_reg->pr_res_key = sa_res_key;
2114 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2115 " Key for %s to: 0x%016Lx PRgeneration:"
2116 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2117 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2118 pr_reg->pr_reg_nacl->initiatorname,
2119 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2123 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2125 type = pr_reg->pr_res_type;
2126 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2128 if (pr_holder < 0) {
2129 ret = TCM_RESERVATION_CONFLICT;
2133 spin_lock(&pr_tmpl->registration_lock);
2135 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2136 * and matching pr_res_key.
2138 if (pr_reg->pr_reg_all_tg_pt) {
2139 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2140 &pr_tmpl->registration_list,
2143 if (!pr_reg_p->pr_reg_all_tg_pt)
2145 if (pr_reg_p->pr_res_key != res_key)
2147 if (pr_reg == pr_reg_p)
2149 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2150 pr_reg_p->pr_reg_nacl->initiatorname))
2153 __core_scsi3_free_registration(dev,
2159 * Release the calling I_T Nexus registration now..
2161 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2165 * From spc4r17, section 5.7.11.3 Unregistering
2167 * If the persistent reservation is a registrants only
2168 * type, the device server shall establish a unit
2169 * attention condition for the initiator port associated
2170 * with every registered I_T nexus except for the I_T
2171 * nexus on which the PERSISTENT RESERVE OUT command was
2172 * received, with the additional sense code set to
2173 * RESERVATIONS RELEASED.
2176 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2177 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2178 list_for_each_entry(pr_reg_p,
2179 &pr_tmpl->registration_list,
2182 core_scsi3_ua_allocate(
2183 pr_reg_p->pr_reg_nacl,
2184 pr_reg_p->pr_res_mapped_lun,
2186 ASCQ_2AH_RESERVATIONS_RELEASED);
2190 spin_unlock(&pr_tmpl->registration_lock);
2193 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2197 core_scsi3_put_pr_reg(pr_reg);
2201 unsigned char *core_scsi3_pr_dump_type(int type)
2204 case PR_TYPE_WRITE_EXCLUSIVE:
2205 return "Write Exclusive Access";
2206 case PR_TYPE_EXCLUSIVE_ACCESS:
2207 return "Exclusive Access";
2208 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2209 return "Write Exclusive Access, Registrants Only";
2210 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2211 return "Exclusive Access, Registrants Only";
2212 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2213 return "Write Exclusive Access, All Registrants";
2214 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2215 return "Exclusive Access, All Registrants";
2220 return "Unknown SPC-3 PR Type";
2223 static sense_reason_t
2224 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2226 struct se_device *dev = cmd->se_dev;
2227 struct se_session *se_sess = cmd->se_sess;
2228 struct se_lun *se_lun = cmd->se_lun;
2229 struct t10_pr_registration *pr_reg, *pr_res_holder;
2230 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2231 char i_buf[PR_REG_ISID_ID_LEN];
2234 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2236 if (!se_sess || !se_lun) {
2237 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2238 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2241 * Locate the existing *pr_reg via struct se_node_acl pointers
2243 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2246 pr_err("SPC-3 PR: Unable to locate"
2247 " PR_REGISTERED *pr_reg for RESERVE\n");
2248 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2251 * From spc4r17 Section 5.7.9: Reserving:
2253 * An application client creates a persistent reservation by issuing
2254 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2255 * a registered I_T nexus with the following parameters:
2256 * a) RESERVATION KEY set to the value of the reservation key that is
2257 * registered with the logical unit for the I_T nexus; and
2259 if (res_key != pr_reg->pr_res_key) {
2260 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2261 " does not match existing SA REGISTER res_key:"
2262 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2263 ret = TCM_RESERVATION_CONFLICT;
2264 goto out_put_pr_reg;
2267 * From spc4r17 Section 5.7.9: Reserving:
2270 * b) TYPE field and SCOPE field set to the persistent reservation
2273 * Only one persistent reservation is allowed at a time per logical unit
2274 * and that persistent reservation has a scope of LU_SCOPE.
2276 if (scope != PR_SCOPE_LU_SCOPE) {
2277 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2278 ret = TCM_INVALID_PARAMETER_LIST;
2279 goto out_put_pr_reg;
2282 * See if we have an existing PR reservation holder pointer at
2283 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2286 spin_lock(&dev->dev_reservation_lock);
2287 pr_res_holder = dev->dev_pr_res_holder;
2288 if (pr_res_holder) {
2290 * From spc4r17 Section 5.7.9: Reserving:
2292 * If the device server receives a PERSISTENT RESERVE OUT
2293 * command from an I_T nexus other than a persistent reservation
2294 * holder (see 5.7.10) that attempts to create a persistent
2295 * reservation when a persistent reservation already exists for
2296 * the logical unit, then the command shall be completed with
2297 * RESERVATION CONFLICT status.
2299 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2300 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2301 pr_err("SPC-3 PR: Attempted RESERVE from"
2302 " [%s]: %s while reservation already held by"
2303 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2304 cmd->se_tfo->get_fabric_name(),
2305 se_sess->se_node_acl->initiatorname,
2306 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2307 pr_res_holder->pr_reg_nacl->initiatorname);
2309 spin_unlock(&dev->dev_reservation_lock);
2310 ret = TCM_RESERVATION_CONFLICT;
2311 goto out_put_pr_reg;
2314 * From spc4r17 Section 5.7.9: Reserving:
2316 * If a persistent reservation holder attempts to modify the
2317 * type or scope of an existing persistent reservation, the
2318 * command shall be completed with RESERVATION CONFLICT status.
2320 if ((pr_res_holder->pr_res_type != type) ||
2321 (pr_res_holder->pr_res_scope != scope)) {
2322 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2323 pr_err("SPC-3 PR: Attempted RESERVE from"
2324 " [%s]: %s trying to change TYPE and/or SCOPE,"
2325 " while reservation already held by [%s]: %s,"
2326 " returning RESERVATION_CONFLICT\n",
2327 cmd->se_tfo->get_fabric_name(),
2328 se_sess->se_node_acl->initiatorname,
2329 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2330 pr_res_holder->pr_reg_nacl->initiatorname);
2332 spin_unlock(&dev->dev_reservation_lock);
2333 ret = TCM_RESERVATION_CONFLICT;
2334 goto out_put_pr_reg;
2337 * From spc4r17 Section 5.7.9: Reserving:
2339 * If the device server receives a PERSISTENT RESERVE OUT
2340 * command with RESERVE service action where the TYPE field and
2341 * the SCOPE field contain the same values as the existing type
2342 * and scope from a persistent reservation holder, it shall not
2343 * make any change to the existing persistent reservation and
2344 * shall completethe command with GOOD status.
2346 spin_unlock(&dev->dev_reservation_lock);
2348 goto out_put_pr_reg;
2351 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2352 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2354 pr_reg->pr_res_scope = scope;
2355 pr_reg->pr_res_type = type;
2356 pr_reg->pr_res_holder = 1;
2357 dev->dev_pr_res_holder = pr_reg;
2358 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2360 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2361 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2362 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2363 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2364 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2365 cmd->se_tfo->get_fabric_name(),
2366 se_sess->se_node_acl->initiatorname,
2368 spin_unlock(&dev->dev_reservation_lock);
2370 if (pr_tmpl->pr_aptpl_active)
2371 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2375 core_scsi3_put_pr_reg(pr_reg);
2379 static sense_reason_t
2380 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2384 case PR_TYPE_WRITE_EXCLUSIVE:
2385 case PR_TYPE_EXCLUSIVE_ACCESS:
2386 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2387 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2388 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2389 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2390 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2392 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2394 return TCM_INVALID_CDB_FIELD;
2399 * Called with struct se_device->dev_reservation_lock held.
2401 static void __core_scsi3_complete_pro_release(
2402 struct se_device *dev,
2403 struct se_node_acl *se_nacl,
2404 struct t10_pr_registration *pr_reg,
2408 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2409 char i_buf[PR_REG_ISID_ID_LEN];
2410 int pr_res_type = 0, pr_res_scope = 0;
2412 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2413 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2415 * Go ahead and release the current PR reservation holder.
2416 * If an All Registrants reservation is currently active and
2417 * a unregister operation is requested, replace the current
2418 * dev_pr_res_holder with another active registration.
2420 if (dev->dev_pr_res_holder) {
2421 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2422 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2423 dev->dev_pr_res_holder->pr_res_type = 0;
2424 dev->dev_pr_res_holder->pr_res_scope = 0;
2425 dev->dev_pr_res_holder->pr_res_holder = 0;
2426 dev->dev_pr_res_holder = NULL;
2431 spin_lock(&dev->t10_pr.registration_lock);
2432 list_del_init(&pr_reg->pr_reg_list);
2434 * If the I_T nexus is a reservation holder, the persistent reservation
2435 * is of an all registrants type, and the I_T nexus is the last remaining
2436 * registered I_T nexus, then the device server shall also release the
2437 * persistent reservation.
2439 if (!list_empty(&dev->t10_pr.registration_list) &&
2440 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2441 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2442 dev->dev_pr_res_holder =
2443 list_entry(dev->t10_pr.registration_list.next,
2444 struct t10_pr_registration, pr_reg_list);
2445 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2446 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2447 dev->dev_pr_res_holder->pr_res_holder = 1;
2449 spin_unlock(&dev->t10_pr.registration_lock);
2451 if (!dev->dev_pr_res_holder) {
2452 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2453 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2454 tfo->get_fabric_name(), (explicit) ? "explicit" :
2455 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2456 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2458 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2459 tfo->get_fabric_name(), se_nacl->initiatorname,
2462 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2464 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2467 static sense_reason_t
2468 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2471 struct se_device *dev = cmd->se_dev;
2472 struct se_session *se_sess = cmd->se_sess;
2473 struct se_lun *se_lun = cmd->se_lun;
2474 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2475 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2476 sense_reason_t ret = 0;
2478 if (!se_sess || !se_lun) {
2479 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2480 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2483 * Locate the existing *pr_reg via struct se_node_acl pointers
2485 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2487 pr_err("SPC-3 PR: Unable to locate"
2488 " PR_REGISTERED *pr_reg for RELEASE\n");
2489 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2492 * From spc4r17 Section 5.7.11.2 Releasing:
2494 * If there is no persistent reservation or in response to a persistent
2495 * reservation release request from a registered I_T nexus that is not a
2496 * persistent reservation holder (see 5.7.10), the device server shall
2499 * a) Not release the persistent reservation, if any;
2500 * b) Not remove any registrations; and
2501 * c) Complete the command with GOOD status.
2503 spin_lock(&dev->dev_reservation_lock);
2504 pr_res_holder = dev->dev_pr_res_holder;
2505 if (!pr_res_holder) {
2507 * No persistent reservation, return GOOD status.
2509 spin_unlock(&dev->dev_reservation_lock);
2510 goto out_put_pr_reg;
2513 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2515 * Release request from a registered I_T nexus that is not a
2516 * persistent reservation holder. return GOOD status.
2518 spin_unlock(&dev->dev_reservation_lock);
2519 goto out_put_pr_reg;
2523 * From spc4r17 Section 5.7.11.2 Releasing:
2525 * Only the persistent reservation holder (see 5.7.10) is allowed to
2526 * release a persistent reservation.
2528 * An application client releases the persistent reservation by issuing
2529 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2530 * an I_T nexus that is a persistent reservation holder with the
2531 * following parameters:
2533 * a) RESERVATION KEY field set to the value of the reservation key
2534 * that is registered with the logical unit for the I_T nexus;
2536 if (res_key != pr_reg->pr_res_key) {
2537 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2538 " does not match existing SA REGISTER res_key:"
2539 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2540 spin_unlock(&dev->dev_reservation_lock);
2541 ret = TCM_RESERVATION_CONFLICT;
2542 goto out_put_pr_reg;
2545 * From spc4r17 Section 5.7.11.2 Releasing and above:
2547 * b) TYPE field and SCOPE field set to match the persistent
2548 * reservation being released.
2550 if ((pr_res_holder->pr_res_type != type) ||
2551 (pr_res_holder->pr_res_scope != scope)) {
2552 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2553 pr_err("SPC-3 PR RELEASE: Attempted to release"
2554 " reservation from [%s]: %s with different TYPE "
2555 "and/or SCOPE while reservation already held by"
2556 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2557 cmd->se_tfo->get_fabric_name(),
2558 se_sess->se_node_acl->initiatorname,
2559 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2560 pr_res_holder->pr_reg_nacl->initiatorname);
2562 spin_unlock(&dev->dev_reservation_lock);
2563 ret = TCM_RESERVATION_CONFLICT;
2564 goto out_put_pr_reg;
2567 * In response to a persistent reservation release request from the
2568 * persistent reservation holder the device server shall perform a
2569 * release by doing the following as an uninterrupted series of actions:
2570 * a) Release the persistent reservation;
2571 * b) Not remove any registration(s);
2572 * c) If the released persistent reservation is a registrants only type
2573 * or all registrants type persistent reservation,
2574 * the device server shall establish a unit attention condition for
2575 * the initiator port associated with every regis-
2576 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2577 * RESERVE OUT command with RELEASE service action was received,
2578 * with the additional sense code set to RESERVATIONS RELEASED; and
2579 * d) If the persistent reservation is of any other type, the device
2580 * server shall not establish a unit attention condition.
2582 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2585 spin_unlock(&dev->dev_reservation_lock);
2587 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2588 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2589 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2590 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2592 * If no UNIT ATTENTION conditions will be established for
2593 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2594 * go ahead and check for APTPL=1 update+write below
2599 spin_lock(&pr_tmpl->registration_lock);
2600 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2603 * Do not establish a UNIT ATTENTION condition
2604 * for the calling I_T Nexus
2606 if (pr_reg_p == pr_reg)
2609 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2610 pr_reg_p->pr_res_mapped_lun,
2611 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2613 spin_unlock(&pr_tmpl->registration_lock);
2616 if (pr_tmpl->pr_aptpl_active)
2617 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2620 core_scsi3_put_pr_reg(pr_reg);
2624 static sense_reason_t
2625 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2627 struct se_device *dev = cmd->se_dev;
2628 struct se_node_acl *pr_reg_nacl;
2629 struct se_session *se_sess = cmd->se_sess;
2630 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2631 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2632 u32 pr_res_mapped_lun = 0;
2633 int calling_it_nexus = 0;
2635 * Locate the existing *pr_reg via struct se_node_acl pointers
2637 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2638 se_sess->se_node_acl, se_sess);
2640 pr_err("SPC-3 PR: Unable to locate"
2641 " PR_REGISTERED *pr_reg for CLEAR\n");
2642 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2645 * From spc4r17 section 5.7.11.6, Clearing:
2647 * Any application client may release the persistent reservation and
2648 * remove all registrations from a device server by issuing a
2649 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2650 * registered I_T nexus with the following parameter:
2652 * a) RESERVATION KEY field set to the value of the reservation key
2653 * that is registered with the logical unit for the I_T nexus.
2655 if (res_key != pr_reg_n->pr_res_key) {
2656 pr_err("SPC-3 PR REGISTER: Received"
2657 " res_key: 0x%016Lx does not match"
2658 " existing SA REGISTER res_key:"
2659 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2660 core_scsi3_put_pr_reg(pr_reg_n);
2661 return TCM_RESERVATION_CONFLICT;
2664 * a) Release the persistent reservation, if any;
2666 spin_lock(&dev->dev_reservation_lock);
2667 pr_res_holder = dev->dev_pr_res_holder;
2668 if (pr_res_holder) {
2669 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2670 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2671 pr_res_holder, 0, 0);
2673 spin_unlock(&dev->dev_reservation_lock);
2675 * b) Remove all registration(s) (see spc4r17 5.7.7);
2677 spin_lock(&pr_tmpl->registration_lock);
2678 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2679 &pr_tmpl->registration_list, pr_reg_list) {
2681 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2682 pr_reg_nacl = pr_reg->pr_reg_nacl;
2683 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2684 __core_scsi3_free_registration(dev, pr_reg, NULL,
2687 * e) Establish a unit attention condition for the initiator
2688 * port associated with every registered I_T nexus other
2689 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2690 * command with CLEAR service action was received, with the
2691 * additional sense code set to RESERVATIONS PREEMPTED.
2693 if (!calling_it_nexus)
2694 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2695 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2697 spin_unlock(&pr_tmpl->registration_lock);
2699 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2700 cmd->se_tfo->get_fabric_name());
2702 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2704 core_scsi3_pr_generation(dev);
2709 * Called with struct se_device->dev_reservation_lock held.
2711 static void __core_scsi3_complete_pro_preempt(
2712 struct se_device *dev,
2713 struct t10_pr_registration *pr_reg,
2714 struct list_head *preempt_and_abort_list,
2717 enum preempt_type preempt_type)
2719 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2720 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2721 char i_buf[PR_REG_ISID_ID_LEN];
2723 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2724 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2726 * Do an implicit RELEASE of the existing reservation.
2728 if (dev->dev_pr_res_holder)
2729 __core_scsi3_complete_pro_release(dev, nacl,
2730 dev->dev_pr_res_holder, 0, 0);
2732 dev->dev_pr_res_holder = pr_reg;
2733 pr_reg->pr_res_holder = 1;
2734 pr_reg->pr_res_type = type;
2735 pr_reg->pr_res_scope = scope;
2737 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2738 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2739 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2740 core_scsi3_pr_dump_type(type),
2741 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2742 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2743 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2744 nacl->initiatorname, i_buf);
2746 * For PREEMPT_AND_ABORT, add the preempting reservation's
2747 * struct t10_pr_registration to the list that will be compared
2748 * against received CDBs..
2750 if (preempt_and_abort_list)
2751 list_add_tail(&pr_reg->pr_reg_abort_list,
2752 preempt_and_abort_list);
2755 static void core_scsi3_release_preempt_and_abort(
2756 struct list_head *preempt_and_abort_list,
2757 struct t10_pr_registration *pr_reg_holder)
2759 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2761 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2762 pr_reg_abort_list) {
2764 list_del(&pr_reg->pr_reg_abort_list);
2765 if (pr_reg_holder == pr_reg)
2767 if (pr_reg->pr_res_holder) {
2768 pr_warn("pr_reg->pr_res_holder still set\n");
2772 pr_reg->pr_reg_deve = NULL;
2773 pr_reg->pr_reg_nacl = NULL;
2774 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2778 static sense_reason_t
2779 core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2780 u64 sa_res_key, enum preempt_type preempt_type)
2782 struct se_device *dev = cmd->se_dev;
2783 struct se_node_acl *pr_reg_nacl;
2784 struct se_session *se_sess = cmd->se_sess;
2785 LIST_HEAD(preempt_and_abort_list);
2786 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2787 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2788 u32 pr_res_mapped_lun = 0;
2789 int all_reg = 0, calling_it_nexus = 0;
2790 bool sa_res_key_unmatched = sa_res_key != 0;
2791 int prh_type = 0, prh_scope = 0;
2794 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2796 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2799 pr_err("SPC-3 PR: Unable to locate"
2800 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2801 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2802 return TCM_RESERVATION_CONFLICT;
2804 if (pr_reg_n->pr_res_key != res_key) {
2805 core_scsi3_put_pr_reg(pr_reg_n);
2806 return TCM_RESERVATION_CONFLICT;
2808 if (scope != PR_SCOPE_LU_SCOPE) {
2809 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2810 core_scsi3_put_pr_reg(pr_reg_n);
2811 return TCM_INVALID_PARAMETER_LIST;
2814 spin_lock(&dev->dev_reservation_lock);
2815 pr_res_holder = dev->dev_pr_res_holder;
2816 if (pr_res_holder &&
2817 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2818 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2821 if (!all_reg && !sa_res_key) {
2822 spin_unlock(&dev->dev_reservation_lock);
2823 core_scsi3_put_pr_reg(pr_reg_n);
2824 return TCM_INVALID_PARAMETER_LIST;
2827 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2829 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2830 * persistent reservation holder or there is no persistent reservation
2831 * holder (i.e., there is no persistent reservation), then the device
2832 * server shall perform a preempt by doing the following in an
2833 * uninterrupted series of actions. (See below..)
2835 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2837 * No existing or SA Reservation Key matching reservations..
2839 * PROUT SA PREEMPT with All Registrant type reservations are
2840 * allowed to be processed without a matching SA Reservation Key
2842 spin_lock(&pr_tmpl->registration_lock);
2843 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2844 &pr_tmpl->registration_list, pr_reg_list) {
2846 * Removing of registrations in non all registrants
2847 * type reservations without a matching SA reservation
2850 * a) Remove the registrations for all I_T nexuses
2851 * specified by the SERVICE ACTION RESERVATION KEY
2853 * b) Ignore the contents of the SCOPE and TYPE fields;
2854 * c) Process tasks as defined in 5.7.1; and
2855 * d) Establish a unit attention condition for the
2856 * initiator port associated with every I_T nexus
2857 * that lost its registration other than the I_T
2858 * nexus on which the PERSISTENT RESERVE OUT command
2859 * was received, with the additional sense code set
2860 * to REGISTRATIONS PREEMPTED.
2863 if (pr_reg->pr_res_key != sa_res_key)
2865 sa_res_key_unmatched = false;
2867 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2868 pr_reg_nacl = pr_reg->pr_reg_nacl;
2869 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2870 __core_scsi3_free_registration(dev, pr_reg,
2871 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2872 NULL, calling_it_nexus);
2875 * Case for any existing all registrants type
2876 * reservation, follow logic in spc4r17 section
2877 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2879 * For a ZERO SA Reservation key, release
2880 * all other registrations and do an implicit
2881 * release of active persistent reservation.
2883 * For a non-ZERO SA Reservation key, only
2884 * release the matching reservation key from
2888 (pr_reg->pr_res_key != sa_res_key))
2890 sa_res_key_unmatched = false;
2892 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2893 if (calling_it_nexus)
2896 pr_reg_nacl = pr_reg->pr_reg_nacl;
2897 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2898 __core_scsi3_free_registration(dev, pr_reg,
2899 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2902 if (!calling_it_nexus)
2903 core_scsi3_ua_allocate(pr_reg_nacl,
2904 pr_res_mapped_lun, 0x2A,
2905 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2907 spin_unlock(&pr_tmpl->registration_lock);
2909 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2910 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2911 * RESERVATION KEY field to a value that does not match any
2912 * registered reservation key, then the device server shall
2913 * complete the command with RESERVATION CONFLICT status.
2915 if (sa_res_key_unmatched) {
2916 spin_unlock(&dev->dev_reservation_lock);
2917 core_scsi3_put_pr_reg(pr_reg_n);
2918 return TCM_RESERVATION_CONFLICT;
2921 * For an existing all registrants type reservation
2922 * with a zero SA rservation key, preempt the existing
2923 * reservation with the new PR type and scope.
2925 if (pr_res_holder && all_reg && !(sa_res_key)) {
2926 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
2927 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2928 type, scope, preempt_type);
2930 if (preempt_type == PREEMPT_AND_ABORT)
2931 core_scsi3_release_preempt_and_abort(
2932 &preempt_and_abort_list, pr_reg_n);
2934 spin_unlock(&dev->dev_reservation_lock);
2936 if (pr_tmpl->pr_aptpl_active)
2937 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2939 core_scsi3_put_pr_reg(pr_reg_n);
2940 core_scsi3_pr_generation(cmd->se_dev);
2944 * The PREEMPTing SA reservation key matches that of the
2945 * existing persistent reservation, first, we check if
2946 * we are preempting our own reservation.
2947 * From spc4r17, section 5.7.11.4.3 Preempting
2948 * persistent reservations and registration handling
2950 * If an all registrants persistent reservation is not
2951 * present, it is not an error for the persistent
2952 * reservation holder to preempt itself (i.e., a
2953 * PERSISTENT RESERVE OUT with a PREEMPT service action
2954 * or a PREEMPT AND ABORT service action with the
2955 * SERVICE ACTION RESERVATION KEY value equal to the
2956 * persistent reservation holder's reservation key that
2957 * is received from the persistent reservation holder).
2958 * In that case, the device server shall establish the
2959 * new persistent reservation and maintain the
2962 prh_type = pr_res_holder->pr_res_type;
2963 prh_scope = pr_res_holder->pr_res_scope;
2965 * If the SERVICE ACTION RESERVATION KEY field identifies a
2966 * persistent reservation holder (see 5.7.10), the device
2967 * server shall perform a preempt by doing the following as
2968 * an uninterrupted series of actions:
2970 * a) Release the persistent reservation for the holder
2971 * identified by the SERVICE ACTION RESERVATION KEY field;
2973 if (pr_reg_n != pr_res_holder)
2974 __core_scsi3_complete_pro_release(dev,
2975 pr_res_holder->pr_reg_nacl,
2976 dev->dev_pr_res_holder, 0, 0);
2978 * b) Remove the registrations for all I_T nexuses identified
2979 * by the SERVICE ACTION RESERVATION KEY field, except the
2980 * I_T nexus that is being used for the PERSISTENT RESERVE
2981 * OUT command. If an all registrants persistent reservation
2982 * is present and the SERVICE ACTION RESERVATION KEY field
2983 * is set to zero, then all registrations shall be removed
2984 * except for that of the I_T nexus that is being used for
2985 * the PERSISTENT RESERVE OUT command;
2987 spin_lock(&pr_tmpl->registration_lock);
2988 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2989 &pr_tmpl->registration_list, pr_reg_list) {
2991 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2992 if (calling_it_nexus)
2995 if (pr_reg->pr_res_key != sa_res_key)
2998 pr_reg_nacl = pr_reg->pr_reg_nacl;
2999 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3000 __core_scsi3_free_registration(dev, pr_reg,
3001 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3004 * e) Establish a unit attention condition for the initiator
3005 * port associated with every I_T nexus that lost its
3006 * persistent reservation and/or registration, with the
3007 * additional sense code set to REGISTRATIONS PREEMPTED;
3009 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3010 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3012 spin_unlock(&pr_tmpl->registration_lock);
3014 * c) Establish a persistent reservation for the preempting
3015 * I_T nexus using the contents of the SCOPE and TYPE fields;
3017 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3018 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3019 type, scope, preempt_type);
3021 * d) Process tasks as defined in 5.7.1;
3023 * f) If the type or scope has changed, then for every I_T nexus
3024 * whose reservation key was not removed, except for the I_T
3025 * nexus on which the PERSISTENT RESERVE OUT command was
3026 * received, the device server shall establish a unit
3027 * attention condition for the initiator port associated with
3028 * that I_T nexus, with the additional sense code set to
3029 * RESERVATIONS RELEASED. If the type or scope have not
3030 * changed, then no unit attention condition(s) shall be
3031 * established for this reason.
3033 if ((prh_type != type) || (prh_scope != scope)) {
3034 spin_lock(&pr_tmpl->registration_lock);
3035 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3036 &pr_tmpl->registration_list, pr_reg_list) {
3038 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3039 if (calling_it_nexus)
3042 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3043 pr_reg->pr_res_mapped_lun, 0x2A,
3044 ASCQ_2AH_RESERVATIONS_RELEASED);
3046 spin_unlock(&pr_tmpl->registration_lock);
3048 spin_unlock(&dev->dev_reservation_lock);
3050 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3051 * All received CDBs for the matching existing reservation and
3052 * registrations undergo ABORT_TASK logic.
3054 * From there, core_scsi3_release_preempt_and_abort() will
3055 * release every registration in the list (which have already
3056 * been removed from the primary pr_reg list), except the
3057 * new persistent reservation holder, the calling Initiator Port.
3059 if (preempt_type == PREEMPT_AND_ABORT) {
3060 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3061 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3065 if (pr_tmpl->pr_aptpl_active)
3066 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3068 core_scsi3_put_pr_reg(pr_reg_n);
3069 core_scsi3_pr_generation(cmd->se_dev);
3073 static sense_reason_t
3074 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3075 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3078 case PR_TYPE_WRITE_EXCLUSIVE:
3079 case PR_TYPE_EXCLUSIVE_ACCESS:
3080 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3081 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3082 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3083 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3084 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3085 sa_res_key, preempt_type);
3087 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3088 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3089 return TCM_INVALID_CDB_FIELD;
3094 static sense_reason_t
3095 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3096 u64 sa_res_key, int aptpl, int unreg)
3098 struct se_session *se_sess = cmd->se_sess;
3099 struct se_device *dev = cmd->se_dev;
3100 struct se_dev_entry *dest_se_deve = NULL;
3101 struct se_lun *se_lun = cmd->se_lun;
3102 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3103 struct se_port *se_port;
3104 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3105 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3106 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3107 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3109 unsigned char *initiator_str;
3110 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
3111 u32 tid_len, tmp_tid_len;
3112 int new_reg = 0, type, scope, matching_iname;
3114 unsigned short rtpi;
3115 unsigned char proto_ident;
3117 if (!se_sess || !se_lun) {
3118 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3119 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3122 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3123 se_tpg = se_sess->se_tpg;
3124 tf_ops = se_tpg->se_tpg_tfo;
3126 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3127 * Register behaviors for a REGISTER AND MOVE service action
3129 * Locate the existing *pr_reg via struct se_node_acl pointers
3131 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3134 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3135 " *pr_reg for REGISTER_AND_MOVE\n");
3136 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3139 * The provided reservation key much match the existing reservation key
3140 * provided during this initiator's I_T nexus registration.
3142 if (res_key != pr_reg->pr_res_key) {
3143 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3144 " res_key: 0x%016Lx does not match existing SA REGISTER"
3145 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3146 ret = TCM_RESERVATION_CONFLICT;
3147 goto out_put_pr_reg;
3150 * The service active reservation key needs to be non zero
3153 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3155 ret = TCM_INVALID_PARAMETER_LIST;
3156 goto out_put_pr_reg;
3160 * Determine the Relative Target Port Identifier where the reservation
3161 * will be moved to for the TransportID containing SCSI initiator WWN
3164 buf = transport_kmap_data_sg(cmd);
3166 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3167 goto out_put_pr_reg;
3170 rtpi = (buf[18] & 0xff) << 8;
3171 rtpi |= buf[19] & 0xff;
3172 tid_len = (buf[20] & 0xff) << 24;
3173 tid_len |= (buf[21] & 0xff) << 16;
3174 tid_len |= (buf[22] & 0xff) << 8;
3175 tid_len |= buf[23] & 0xff;
3176 transport_kunmap_data_sg(cmd);
3179 if ((tid_len + 24) != cmd->data_length) {
3180 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3181 " does not equal CDB data_length: %u\n", tid_len,
3183 ret = TCM_INVALID_PARAMETER_LIST;
3184 goto out_put_pr_reg;
3187 spin_lock(&dev->se_port_lock);
3188 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3189 if (se_port->sep_rtpi != rtpi)
3191 dest_se_tpg = se_port->sep_tpg;
3194 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3198 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3199 spin_unlock(&dev->se_port_lock);
3201 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3202 pr_err("core_scsi3_tpg_depend_item() failed"
3203 " for dest_se_tpg\n");
3204 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3205 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3206 goto out_put_pr_reg;
3209 spin_lock(&dev->se_port_lock);
3212 spin_unlock(&dev->se_port_lock);
3214 if (!dest_se_tpg || !dest_tf_ops) {
3215 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3216 " fabric ops from Relative Target Port Identifier:"
3218 ret = TCM_INVALID_PARAMETER_LIST;
3219 goto out_put_pr_reg;
3222 buf = transport_kmap_data_sg(cmd);
3224 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3225 goto out_put_pr_reg;
3227 proto_ident = (buf[24] & 0x0f);
3229 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3230 " 0x%02x\n", proto_ident);
3232 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3233 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3234 " proto_ident: 0x%02x does not match ident: 0x%02x"
3235 " from fabric: %s\n", proto_ident,
3236 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3237 dest_tf_ops->get_fabric_name());
3238 ret = TCM_INVALID_PARAMETER_LIST;
3241 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3242 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3243 " containg a valid tpg_parse_pr_out_transport_id"
3244 " function pointer\n");
3245 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3248 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3249 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3250 if (!initiator_str) {
3251 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3252 " initiator_str from Transport ID\n");
3253 ret = TCM_INVALID_PARAMETER_LIST;
3257 transport_kunmap_data_sg(cmd);
3260 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3261 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3262 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3265 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3266 * action specifies a TransportID that is the same as the initiator port
3267 * of the I_T nexus for the command received, then the command shall
3268 * be terminated with CHECK CONDITION status, with the sense key set to
3269 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3270 * IN PARAMETER LIST.
3272 pr_reg_nacl = pr_reg->pr_reg_nacl;
3273 matching_iname = (!strcmp(initiator_str,
3274 pr_reg_nacl->initiatorname)) ? 1 : 0;
3275 if (!matching_iname)
3276 goto after_iport_check;
3278 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3279 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3280 " matches: %s on received I_T Nexus\n", initiator_str,
3281 pr_reg_nacl->initiatorname);
3282 ret = TCM_INVALID_PARAMETER_LIST;
3285 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3286 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3287 " matches: %s %s on received I_T Nexus\n",
3288 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3289 pr_reg->pr_reg_isid);
3290 ret = TCM_INVALID_PARAMETER_LIST;
3295 * Locate the destination struct se_node_acl from the received Transport ID
3297 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3298 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3301 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3302 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3304 if (!dest_node_acl) {
3305 pr_err("Unable to locate %s dest_node_acl for"
3306 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3308 ret = TCM_INVALID_PARAMETER_LIST;
3312 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3313 pr_err("core_scsi3_nodeacl_depend_item() for"
3314 " dest_node_acl\n");
3315 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3316 dest_node_acl = NULL;
3317 ret = TCM_INVALID_PARAMETER_LIST;
3321 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3322 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3323 dest_node_acl->initiatorname);
3326 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3329 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3330 if (!dest_se_deve) {
3331 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3332 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3333 ret = TCM_INVALID_PARAMETER_LIST;
3337 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3338 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3339 atomic_dec_mb(&dest_se_deve->pr_ref_count);
3340 dest_se_deve = NULL;
3341 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3345 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3346 " ACL for dest_se_deve->mapped_lun: %u\n",
3347 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3348 dest_se_deve->mapped_lun);
3351 * A persistent reservation needs to already existing in order to
3352 * successfully complete the REGISTER_AND_MOVE service action..
3354 spin_lock(&dev->dev_reservation_lock);
3355 pr_res_holder = dev->dev_pr_res_holder;
3356 if (!pr_res_holder) {
3357 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3358 " currently held\n");
3359 spin_unlock(&dev->dev_reservation_lock);
3360 ret = TCM_INVALID_CDB_FIELD;
3364 * The received on I_T Nexus must be the reservation holder.
3366 * From spc4r17 section 5.7.8 Table 50 --
3367 * Register behaviors for a REGISTER AND MOVE service action
3369 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3370 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3371 " Nexus is not reservation holder\n");
3372 spin_unlock(&dev->dev_reservation_lock);
3373 ret = TCM_RESERVATION_CONFLICT;
3377 * From spc4r17 section 5.7.8: registering and moving reservation
3379 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3380 * action is received and the established persistent reservation is a
3381 * Write Exclusive - All Registrants type or Exclusive Access -
3382 * All Registrants type reservation, then the command shall be completed
3383 * with RESERVATION CONFLICT status.
3385 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3386 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3387 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3388 " reservation for type: %s\n",
3389 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3390 spin_unlock(&dev->dev_reservation_lock);
3391 ret = TCM_RESERVATION_CONFLICT;
3394 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3396 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3398 type = pr_res_holder->pr_res_type;
3399 scope = pr_res_holder->pr_res_type;
3401 * c) Associate the reservation key specified in the SERVICE ACTION
3402 * RESERVATION KEY field with the I_T nexus specified as the
3403 * destination of the register and move, where:
3404 * A) The I_T nexus is specified by the TransportID and the
3405 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3406 * B) Regardless of the TransportID format used, the association for
3407 * the initiator port is based on either the initiator port name
3408 * (see 3.1.71) on SCSI transport protocols where port names are
3409 * required or the initiator port identifier (see 3.1.70) on SCSI
3410 * transport protocols where port names are not required;
3411 * d) Register the reservation key specified in the SERVICE ACTION
3412 * RESERVATION KEY field;
3413 * e) Retain the reservation key specified in the SERVICE ACTION
3414 * RESERVATION KEY field and associated information;
3416 * Also, It is not an error for a REGISTER AND MOVE service action to
3417 * register an I_T nexus that is already registered with the same
3418 * reservation key or a different reservation key.
3420 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3423 if (core_scsi3_alloc_registration(cmd->se_dev,
3424 dest_node_acl, dest_se_deve, iport_ptr,
3425 sa_res_key, 0, aptpl, 2, 1)) {
3426 spin_unlock(&dev->dev_reservation_lock);
3427 ret = TCM_INVALID_PARAMETER_LIST;
3430 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3435 * f) Release the persistent reservation for the persistent reservation
3436 * holder (i.e., the I_T nexus on which the
3438 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3439 dev->dev_pr_res_holder, 0, 0);
3441 * g) Move the persistent reservation to the specified I_T nexus using
3442 * the same scope and type as the persistent reservation released in
3445 dev->dev_pr_res_holder = dest_pr_reg;
3446 dest_pr_reg->pr_res_holder = 1;
3447 dest_pr_reg->pr_res_type = type;
3448 pr_reg->pr_res_scope = scope;
3449 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3451 * Increment PRGeneration for existing registrations..
3454 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3455 spin_unlock(&dev->dev_reservation_lock);
3457 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3458 " created new reservation holder TYPE: %s on object RTPI:"
3459 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3460 core_scsi3_pr_dump_type(type), rtpi,
3461 dest_pr_reg->pr_res_generation);
3462 pr_debug("SPC-3 PR Successfully moved reservation from"
3463 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3464 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3465 i_buf, dest_tf_ops->get_fabric_name(),
3466 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3469 * It is now safe to release configfs group dependencies for destination
3470 * of Transport ID Initiator Device/Port Identifier
3472 core_scsi3_lunacl_undepend_item(dest_se_deve);
3473 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3474 core_scsi3_tpg_undepend_item(dest_se_tpg);
3476 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3477 * nexus on which PERSISTENT RESERVE OUT command was received.
3480 spin_lock(&pr_tmpl->registration_lock);
3481 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3482 spin_unlock(&pr_tmpl->registration_lock);
3484 core_scsi3_put_pr_reg(pr_reg);
3486 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3488 transport_kunmap_data_sg(cmd);
3490 core_scsi3_put_pr_reg(dest_pr_reg);
3494 transport_kunmap_data_sg(cmd);
3496 core_scsi3_lunacl_undepend_item(dest_se_deve);
3498 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3499 core_scsi3_tpg_undepend_item(dest_se_tpg);
3502 core_scsi3_put_pr_reg(pr_reg);
3506 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3508 unsigned int __v1, __v2;
3510 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3511 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3513 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3517 * See spc4r17 section 6.14 Table 170
3520 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3522 struct se_device *dev = cmd->se_dev;
3523 unsigned char *cdb = &cmd->t_task_cdb[0];
3525 u64 res_key, sa_res_key;
3526 int sa, scope, type, aptpl;
3527 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3531 * Following spc2r20 5.5.1 Reservations overview:
3533 * If a logical unit has been reserved by any RESERVE command and is
3534 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3535 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3536 * initiator or service action and shall terminate with a RESERVATION
3539 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3540 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3541 " SPC-2 reservation is held, returning"
3542 " RESERVATION_CONFLICT\n");
3543 return TCM_RESERVATION_CONFLICT;
3547 * FIXME: A NULL struct se_session pointer means an this is not coming from
3548 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3551 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3553 if (cmd->data_length < 24) {
3554 pr_warn("SPC-PR: Received PR OUT parameter list"
3555 " length too small: %u\n", cmd->data_length);
3556 return TCM_INVALID_PARAMETER_LIST;
3560 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3562 sa = (cdb[1] & 0x1f);
3563 scope = (cdb[2] & 0xf0);
3564 type = (cdb[2] & 0x0f);
3566 buf = transport_kmap_data_sg(cmd);
3568 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3571 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3573 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3574 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3576 * REGISTER_AND_MOVE uses a different SA parameter list containing
3577 * SCSI TransportIDs.
3579 if (sa != PRO_REGISTER_AND_MOVE) {
3580 spec_i_pt = (buf[20] & 0x08);
3581 all_tg_pt = (buf[20] & 0x04);
3582 aptpl = (buf[20] & 0x01);
3584 aptpl = (buf[17] & 0x01);
3585 unreg = (buf[17] & 0x02);
3588 * If the backend device has been configured to force APTPL metadata
3589 * write-out, go ahead and propigate aptpl=1 down now.
3591 if (dev->dev_attrib.force_pr_aptpl)
3594 transport_kunmap_data_sg(cmd);
3598 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3600 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3601 return TCM_INVALID_PARAMETER_LIST;
3604 * From spc4r17 section 6.14:
3606 * If the SPEC_I_PT bit is set to zero, the service action is not
3607 * REGISTER AND MOVE, and the parameter list length is not 24, then
3608 * the command shall be terminated with CHECK CONDITION status, with
3609 * the sense key set to ILLEGAL REQUEST, and the additional sense
3610 * code set to PARAMETER LIST LENGTH ERROR.
3612 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3613 (cmd->data_length != 24)) {
3614 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3615 " list length: %u\n", cmd->data_length);
3616 return TCM_INVALID_PARAMETER_LIST;
3620 * (core_scsi3_emulate_pro_* function parameters
3621 * are defined by spc4r17 Table 174:
3622 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3626 ret = core_scsi3_emulate_pro_register(cmd,
3627 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3630 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3633 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3636 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3639 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3640 res_key, sa_res_key, PREEMPT);
3642 case PRO_PREEMPT_AND_ABORT:
3643 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3644 res_key, sa_res_key, PREEMPT_AND_ABORT);
3646 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3647 ret = core_scsi3_emulate_pro_register(cmd,
3648 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3650 case PRO_REGISTER_AND_MOVE:
3651 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3652 sa_res_key, aptpl, unreg);
3655 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3656 " action: 0x%02x\n", cdb[1] & 0x1f);
3657 return TCM_INVALID_CDB_FIELD;
3661 target_complete_cmd(cmd, GOOD);
3666 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3668 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3670 static sense_reason_t
3671 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3673 struct se_device *dev = cmd->se_dev;
3674 struct t10_pr_registration *pr_reg;
3676 u32 add_len = 0, off = 8;
3678 if (cmd->data_length < 8) {
3679 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3680 " too small\n", cmd->data_length);
3681 return TCM_INVALID_CDB_FIELD;
3684 buf = transport_kmap_data_sg(cmd);
3686 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3688 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3689 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3690 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3691 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3693 spin_lock(&dev->t10_pr.registration_lock);
3694 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3697 * Check for overflow of 8byte PRI READ_KEYS payload and
3698 * next reservation key list descriptor.
3700 if ((add_len + 8) > (cmd->data_length - 8))
3703 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3704 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3705 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3706 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3707 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3708 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3709 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3710 buf[off++] = (pr_reg->pr_res_key & 0xff);
3714 spin_unlock(&dev->t10_pr.registration_lock);
3716 buf[4] = ((add_len >> 24) & 0xff);
3717 buf[5] = ((add_len >> 16) & 0xff);
3718 buf[6] = ((add_len >> 8) & 0xff);
3719 buf[7] = (add_len & 0xff);
3721 transport_kunmap_data_sg(cmd);
3727 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3729 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3731 static sense_reason_t
3732 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3734 struct se_device *dev = cmd->se_dev;
3735 struct t10_pr_registration *pr_reg;
3738 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3740 if (cmd->data_length < 8) {
3741 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3742 " too small\n", cmd->data_length);
3743 return TCM_INVALID_CDB_FIELD;
3746 buf = transport_kmap_data_sg(cmd);
3748 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3750 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3751 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3752 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3753 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3755 spin_lock(&dev->dev_reservation_lock);
3756 pr_reg = dev->dev_pr_res_holder;
3759 * Set the hardcoded Additional Length
3761 buf[4] = ((add_len >> 24) & 0xff);
3762 buf[5] = ((add_len >> 16) & 0xff);
3763 buf[6] = ((add_len >> 8) & 0xff);
3764 buf[7] = (add_len & 0xff);
3766 if (cmd->data_length < 22)
3770 * Set the Reservation key.
3772 * From spc4r17, section 5.7.10:
3773 * A persistent reservation holder has its reservation key
3774 * returned in the parameter data from a PERSISTENT
3775 * RESERVE IN command with READ RESERVATION service action as
3777 * a) For a persistent reservation of the type Write Exclusive
3778 * - All Registrants or Exclusive Access  All Regitrants,
3779 * the reservation key shall be set to zero; or
3780 * b) For all other persistent reservation types, the
3781 * reservation key shall be set to the registered
3782 * reservation key for the I_T nexus that holds the
3783 * persistent reservation.
3785 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3786 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3789 pr_res_key = pr_reg->pr_res_key;
3791 buf[8] = ((pr_res_key >> 56) & 0xff);
3792 buf[9] = ((pr_res_key >> 48) & 0xff);
3793 buf[10] = ((pr_res_key >> 40) & 0xff);
3794 buf[11] = ((pr_res_key >> 32) & 0xff);
3795 buf[12] = ((pr_res_key >> 24) & 0xff);
3796 buf[13] = ((pr_res_key >> 16) & 0xff);
3797 buf[14] = ((pr_res_key >> 8) & 0xff);
3798 buf[15] = (pr_res_key & 0xff);
3800 * Set the SCOPE and TYPE
3802 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3803 (pr_reg->pr_res_type & 0x0f);
3807 spin_unlock(&dev->dev_reservation_lock);
3808 transport_kunmap_data_sg(cmd);
3814 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3816 * See spc4r17 section 6.13.4 Table 165
3818 static sense_reason_t
3819 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3821 struct se_device *dev = cmd->se_dev;
3822 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3824 u16 add_len = 8; /* Hardcoded to 8. */
3826 if (cmd->data_length < 6) {
3827 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3828 " %u too small\n", cmd->data_length);
3829 return TCM_INVALID_CDB_FIELD;
3832 buf = transport_kmap_data_sg(cmd);
3834 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3836 buf[0] = ((add_len >> 8) & 0xff);
3837 buf[1] = (add_len & 0xff);
3838 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3839 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3840 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3841 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3843 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3844 * set the TMV: Task Mask Valid bit.
3848 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3850 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3852 * PTPL_A: Persistence across Target Power Loss Active bit
3854 if (pr_tmpl->pr_aptpl_active)
3857 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3859 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3860 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3861 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3862 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3863 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3864 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3866 transport_kunmap_data_sg(cmd);
3872 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3874 * See spc4r17 section 6.13.5 Table 168 and 169
3876 static sense_reason_t
3877 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3879 struct se_device *dev = cmd->se_dev;
3880 struct se_node_acl *se_nacl;
3881 struct se_portal_group *se_tpg;
3882 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3883 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3885 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
3886 u32 off = 8; /* off into first Full Status descriptor */
3887 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3888 bool all_reg = false;
3890 if (cmd->data_length < 8) {
3891 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3892 " too small\n", cmd->data_length);
3893 return TCM_INVALID_CDB_FIELD;
3896 buf = transport_kmap_data_sg(cmd);
3898 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3900 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3901 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3902 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3903 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3905 spin_lock(&dev->dev_reservation_lock);
3906 if (dev->dev_pr_res_holder) {
3907 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3909 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3910 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3912 pr_res_type = pr_holder->pr_res_type;
3913 pr_res_scope = pr_holder->pr_res_scope;
3916 spin_unlock(&dev->dev_reservation_lock);
3918 spin_lock(&pr_tmpl->registration_lock);
3919 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3920 &pr_tmpl->registration_list, pr_reg_list) {
3922 se_nacl = pr_reg->pr_reg_nacl;
3923 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3926 atomic_inc_mb(&pr_reg->pr_res_holders);
3927 spin_unlock(&pr_tmpl->registration_lock);
3929 * Determine expected length of $FABRIC_MOD specific
3930 * TransportID full status descriptor..
3932 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
3933 se_tpg, se_nacl, pr_reg, &format_code);
3935 if ((exp_desc_len + add_len) > cmd->data_length) {
3936 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3937 " out of buffer: %d\n", cmd->data_length);
3938 spin_lock(&pr_tmpl->registration_lock);
3939 atomic_dec_mb(&pr_reg->pr_res_holders);
3943 * Set RESERVATION KEY
3945 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3946 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3947 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3948 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3949 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3950 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3951 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3952 buf[off++] = (pr_reg->pr_res_key & 0xff);
3953 off += 4; /* Skip Over Reserved area */
3956 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3958 if (pr_reg->pr_reg_all_tg_pt)
3961 * The struct se_lun pointer will be present for the
3962 * reservation holder for PR_HOLDER bit.
3964 * Also, if this registration is the reservation
3965 * holder or there is an All Registrants reservation
3966 * active, fill in SCOPE and TYPE in the next byte.
3968 if (pr_reg->pr_res_holder) {
3970 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3971 (pr_reg->pr_res_type & 0x0f);
3972 } else if (all_reg) {
3974 buf[off++] = (pr_res_scope & 0xf0) |
3975 (pr_res_type & 0x0f);
3980 off += 4; /* Skip over reserved area */
3982 * From spc4r17 6.3.15:
3984 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3985 * IDENTIFIER field contains the relative port identifier (see
3986 * 3.1.120) of the target port that is part of the I_T nexus
3987 * described by this full status descriptor. If the ALL_TG_PT
3988 * bit is set to one, the contents of the RELATIVE TARGET PORT
3989 * IDENTIFIER field are not defined by this standard.
3991 if (!pr_reg->pr_reg_all_tg_pt) {
3992 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
3994 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
3995 buf[off++] = (port->sep_rtpi & 0xff);
3997 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4000 * Now, have the $FABRIC_MOD fill in the protocol identifier
4002 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4003 se_nacl, pr_reg, &format_code, &buf[off+4]);
4005 spin_lock(&pr_tmpl->registration_lock);
4006 atomic_dec_mb(&pr_reg->pr_res_holders);
4008 * Set the ADDITIONAL DESCRIPTOR LENGTH
4010 buf[off++] = ((desc_len >> 24) & 0xff);
4011 buf[off++] = ((desc_len >> 16) & 0xff);
4012 buf[off++] = ((desc_len >> 8) & 0xff);
4013 buf[off++] = (desc_len & 0xff);
4015 * Size of full desctipor header minus TransportID
4016 * containing $FABRIC_MOD specific) initiator device/port
4019 * See spc4r17 Section 6.13.5 Table 169
4021 add_desc_len = (24 + desc_len);
4024 add_len += add_desc_len;
4026 spin_unlock(&pr_tmpl->registration_lock);
4028 * Set ADDITIONAL_LENGTH
4030 buf[4] = ((add_len >> 24) & 0xff);
4031 buf[5] = ((add_len >> 16) & 0xff);
4032 buf[6] = ((add_len >> 8) & 0xff);
4033 buf[7] = (add_len & 0xff);
4035 transport_kunmap_data_sg(cmd);
4041 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4046 * Following spc2r20 5.5.1 Reservations overview:
4048 * If a logical unit has been reserved by any RESERVE command and is
4049 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4050 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4051 * initiator or service action and shall terminate with a RESERVATION
4054 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4055 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4056 " SPC-2 reservation is held, returning"
4057 " RESERVATION_CONFLICT\n");
4058 return TCM_RESERVATION_CONFLICT;
4061 switch (cmd->t_task_cdb[1] & 0x1f) {
4063 ret = core_scsi3_pri_read_keys(cmd);
4065 case PRI_READ_RESERVATION:
4066 ret = core_scsi3_pri_read_reservation(cmd);
4068 case PRI_REPORT_CAPABILITIES:
4069 ret = core_scsi3_pri_report_capabilities(cmd);
4071 case PRI_READ_FULL_STATUS:
4072 ret = core_scsi3_pri_read_full_status(cmd);
4075 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4076 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4077 return TCM_INVALID_CDB_FIELD;
4081 target_complete_cmd(cmd, GOOD);
4086 target_check_reservation(struct se_cmd *cmd)
4088 struct se_device *dev = cmd->se_dev;
4093 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4095 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
4098 spin_lock(&dev->dev_reservation_lock);
4099 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4100 ret = target_scsi2_reservation_check(cmd);
4102 ret = target_scsi3_pr_reservation_check(cmd);
4103 spin_unlock(&dev->dev_reservation_lock);