Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2 | /******************************************************************************* |
| 3 | * Filename: target_core_pr.c |
| 4 | * |
| 5 | * This file contains SPC-3 compliant persistent reservations and |
| 6 | * legacy SPC-2 reservations with compatible reservation handling (CRH=1) |
| 7 | * |
Nicholas Bellinger | 4c76251 | 2013-09-05 15:29:12 -0700 | [diff] [blame] | 8 | * (c) Copyright 2009-2013 Datera, Inc. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 9 | * |
| 10 | * Nicholas A. Bellinger <nab@kernel.org> |
| 11 | * |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 12 | ******************************************************************************/ |
| 13 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/list.h> |
David S. Miller | 5538d29 | 2015-05-28 11:35:41 -0700 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 18 | #include <linux/file.h> |
Bart Van Assche | 8dcf07b | 2016-11-14 15:47:14 -0800 | [diff] [blame] | 19 | #include <linux/fcntl.h> |
| 20 | #include <linux/fs.h> |
Bart Van Assche | ba92999 | 2015-05-08 10:11:12 +0200 | [diff] [blame] | 21 | #include <scsi/scsi_proto.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 22 | #include <asm/unaligned.h> |
| 23 | |
| 24 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 25 | #include <target/target_core_backend.h> |
| 26 | #include <target/target_core_fabric.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 27 | |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 28 | #include "target_core_internal.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 29 | #include "target_core_pr.h" |
| 30 | #include "target_core_ua.h" |
| 31 | |
| 32 | /* |
| 33 | * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT) |
| 34 | */ |
| 35 | struct pr_transport_id_holder { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 36 | struct t10_pr_registration *dest_pr_reg; |
| 37 | struct se_portal_group *dest_tpg; |
| 38 | struct se_node_acl *dest_node_acl; |
| 39 | struct se_dev_entry *dest_se_deve; |
| 40 | struct list_head dest_list; |
| 41 | }; |
| 42 | |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 43 | void core_pr_dump_initiator_port( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 44 | struct t10_pr_registration *pr_reg, |
| 45 | char *buf, |
| 46 | u32 size) |
| 47 | { |
tangwenji | 24528f0 | 2017-08-24 19:59:37 +0800 | [diff] [blame] | 48 | if (!pr_reg->isid_present_at_reg) { |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 49 | buf[0] = '\0'; |
tangwenji | 24528f0 | 2017-08-24 19:59:37 +0800 | [diff] [blame] | 50 | return; |
| 51 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 52 | |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 53 | snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 56 | enum register_type { |
| 57 | REGISTER, |
| 58 | REGISTER_AND_IGNORE_EXISTING_KEY, |
| 59 | REGISTER_AND_MOVE, |
| 60 | }; |
| 61 | |
| 62 | enum preempt_type { |
| 63 | PREEMPT, |
| 64 | PREEMPT_AND_ABORT, |
| 65 | }; |
| 66 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 67 | static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 68 | struct t10_pr_registration *, int, int); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 69 | |
Ilias Tsitsimpis | e673dc9 | 2015-03-26 17:12:42 +0200 | [diff] [blame] | 70 | static int is_reservation_holder( |
| 71 | struct t10_pr_registration *pr_res_holder, |
| 72 | struct t10_pr_registration *pr_reg) |
| 73 | { |
| 74 | int pr_res_type; |
| 75 | |
| 76 | if (pr_res_holder) { |
| 77 | pr_res_type = pr_res_holder->pr_res_type; |
| 78 | |
| 79 | return pr_res_holder == pr_reg || |
| 80 | pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG || |
| 81 | pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG; |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 86 | static sense_reason_t |
| 87 | target_scsi2_reservation_check(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 88 | { |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 89 | struct se_device *dev = cmd->se_dev; |
| 90 | struct se_session *sess = cmd->se_sess; |
| 91 | |
| 92 | switch (cmd->t_task_cdb[0]) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 93 | case INQUIRY: |
| 94 | case RELEASE: |
| 95 | case RELEASE_10: |
| 96 | return 0; |
| 97 | default: |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 98 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 101 | if (!dev->reservation_holder || !sess) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 102 | return 0; |
| 103 | |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 104 | if (dev->reservation_holder->se_node_acl != sess->se_node_acl) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 105 | return TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 106 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 107 | if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { |
| 108 | if (dev->dev_res_bin_isid != sess->sess_bin_isid) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 109 | return TCM_RESERVATION_CONFLICT; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 115 | static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *, |
| 116 | struct se_node_acl *, struct se_session *); |
| 117 | static void core_scsi3_put_pr_reg(struct t10_pr_registration *); |
| 118 | |
Nicholas Bellinger | 087a03b | 2012-03-13 21:29:06 -0700 | [diff] [blame] | 119 | static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd) |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 120 | { |
| 121 | struct se_session *se_sess = cmd->se_sess; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 122 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 123 | struct t10_pr_registration *pr_reg; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 124 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 125 | int conflict = 0; |
| 126 | |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 127 | pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, |
| 128 | se_sess); |
| 129 | if (pr_reg) { |
| 130 | /* |
| 131 | * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE |
| 132 | * behavior |
| 133 | * |
| 134 | * A RESERVE(6) or RESERVE(10) command shall complete with GOOD |
| 135 | * status, but no reservation shall be established and the |
| 136 | * persistent reservation shall not be changed, if the command |
| 137 | * is received from a) and b) below. |
| 138 | * |
| 139 | * A RELEASE(6) or RELEASE(10) command shall complete with GOOD |
| 140 | * status, but the persistent reservation shall not be released, |
| 141 | * if the command is received from a) and b) |
| 142 | * |
| 143 | * a) An I_T nexus that is a persistent reservation holder; or |
| 144 | * b) An I_T nexus that is registered if a registrants only or |
| 145 | * all registrants type persistent reservation is present. |
| 146 | * |
| 147 | * In all other cases, a RESERVE(6) command, RESERVE(10) command, |
| 148 | * RELEASE(6) command, or RELEASE(10) command shall be processed |
| 149 | * as defined in SPC-2. |
| 150 | */ |
| 151 | if (pr_reg->pr_res_holder) { |
| 152 | core_scsi3_put_pr_reg(pr_reg); |
Nicholas Bellinger | 087a03b | 2012-03-13 21:29:06 -0700 | [diff] [blame] | 153 | return 1; |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 154 | } |
| 155 | if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) || |
| 156 | (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) || |
| 157 | (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || |
| 158 | (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { |
| 159 | core_scsi3_put_pr_reg(pr_reg); |
Nicholas Bellinger | 087a03b | 2012-03-13 21:29:06 -0700 | [diff] [blame] | 160 | return 1; |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 161 | } |
| 162 | core_scsi3_put_pr_reg(pr_reg); |
| 163 | conflict = 1; |
| 164 | } else { |
| 165 | /* |
| 166 | * Following spc2r20 5.5.1 Reservations overview: |
| 167 | * |
| 168 | * If a logical unit has executed a PERSISTENT RESERVE OUT |
| 169 | * command with the REGISTER or the REGISTER AND IGNORE |
| 170 | * EXISTING KEY service action and is still registered by any |
| 171 | * initiator, all RESERVE commands and all RELEASE commands |
| 172 | * regardless of initiator shall conflict and shall terminate |
| 173 | * with a RESERVATION CONFLICT status. |
| 174 | */ |
| 175 | spin_lock(&pr_tmpl->registration_lock); |
| 176 | conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1; |
| 177 | spin_unlock(&pr_tmpl->registration_lock); |
| 178 | } |
| 179 | |
| 180 | if (conflict) { |
| 181 | pr_err("Received legacy SPC-2 RESERVE/RELEASE" |
| 182 | " while active SPC-3 registrations exist," |
| 183 | " returning RESERVATION_CONFLICT\n"); |
Nicholas Bellinger | 087a03b | 2012-03-13 21:29:06 -0700 | [diff] [blame] | 184 | return -EBUSY; |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Nicholas Bellinger | 087a03b | 2012-03-13 21:29:06 -0700 | [diff] [blame] | 187 | return 0; |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 190 | void target_release_reservation(struct se_device *dev) |
| 191 | { |
| 192 | dev->reservation_holder = NULL; |
| 193 | dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS; |
| 194 | if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { |
| 195 | dev->dev_res_bin_isid = 0; |
| 196 | dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID; |
| 197 | } |
| 198 | } |
| 199 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 200 | sense_reason_t |
| 201 | target_scsi2_reservation_release(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 202 | { |
| 203 | struct se_device *dev = cmd->se_dev; |
| 204 | struct se_session *sess = cmd->se_sess; |
Wei Yongjun | 609234e | 2012-09-07 14:56:28 +0800 | [diff] [blame] | 205 | struct se_portal_group *tpg; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 206 | int rc; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 207 | |
Wei Yongjun | 609234e | 2012-09-07 14:56:28 +0800 | [diff] [blame] | 208 | if (!sess || !sess->se_tpg) |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 209 | goto out; |
Nicholas Bellinger | 087a03b | 2012-03-13 21:29:06 -0700 | [diff] [blame] | 210 | rc = target_check_scsi2_reservation_conflict(cmd); |
| 211 | if (rc == 1) |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 212 | goto out; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 213 | if (rc < 0) |
| 214 | return TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 215 | |
| 216 | spin_lock(&dev->dev_reservation_lock); |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 217 | if (!dev->reservation_holder || !sess) |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 218 | goto out_unlock; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 219 | |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 220 | if (dev->reservation_holder->se_node_acl != sess->se_node_acl) |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 221 | goto out_unlock; |
| 222 | |
Bernhard Kohl | edc318d | 2012-05-13 23:39:37 +0200 | [diff] [blame] | 223 | if (dev->dev_res_bin_isid != sess->sess_bin_isid) |
| 224 | goto out_unlock; |
| 225 | |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 226 | target_release_reservation(dev); |
Wei Yongjun | 609234e | 2012-09-07 14:56:28 +0800 | [diff] [blame] | 227 | tpg = sess->se_tpg; |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 228 | pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->" |
| 229 | " MAPPED LUN: %llu for %s\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 230 | tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 231 | cmd->se_lun->unpacked_lun, cmd->orig_fe_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 232 | sess->se_node_acl->initiatorname); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 233 | |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 234 | out_unlock: |
| 235 | spin_unlock(&dev->dev_reservation_lock); |
| 236 | out: |
Hannes Reinecke | 14b40c1e | 2021-04-27 10:30:45 +0200 | [diff] [blame] | 237 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 238 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 241 | sense_reason_t |
| 242 | target_scsi2_reservation_reserve(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 243 | { |
| 244 | struct se_device *dev = cmd->se_dev; |
| 245 | struct se_session *sess = cmd->se_sess; |
Wei Yongjun | 609234e | 2012-09-07 14:56:28 +0800 | [diff] [blame] | 246 | struct se_portal_group *tpg; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 247 | sense_reason_t ret = 0; |
| 248 | int rc; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 249 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 250 | if ((cmd->t_task_cdb[1] & 0x01) && |
| 251 | (cmd->t_task_cdb[1] & 0x02)) { |
Colin Ian King | a3960ce | 2016-11-07 16:31:21 +0000 | [diff] [blame] | 252 | pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 253 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 254 | } |
| 255 | /* |
| 256 | * This is currently the case for target_core_mod passthrough struct se_cmd |
| 257 | * ops |
| 258 | */ |
Wei Yongjun | 609234e | 2012-09-07 14:56:28 +0800 | [diff] [blame] | 259 | if (!sess || !sess->se_tpg) |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 260 | goto out; |
Nicholas Bellinger | 087a03b | 2012-03-13 21:29:06 -0700 | [diff] [blame] | 261 | rc = target_check_scsi2_reservation_conflict(cmd); |
| 262 | if (rc == 1) |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 263 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 264 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 265 | if (rc < 0) |
| 266 | return TCM_RESERVATION_CONFLICT; |
| 267 | |
Wei Yongjun | 609234e | 2012-09-07 14:56:28 +0800 | [diff] [blame] | 268 | tpg = sess->se_tpg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 269 | spin_lock(&dev->dev_reservation_lock); |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 270 | if (dev->reservation_holder && |
| 271 | dev->reservation_holder->se_node_acl != sess->se_node_acl) { |
Colin Ian King | 9a8ef2c | 2021-09-20 19:32:06 +0100 | [diff] [blame] | 272 | pr_err("SCSI-2 RESERVATION CONFLICT for %s fabric\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 273 | tpg->se_tpg_tfo->fabric_name); |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 274 | pr_err("Original reserver LUN: %llu %s\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 275 | cmd->se_lun->unpacked_lun, |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 276 | dev->reservation_holder->se_node_acl->initiatorname); |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 277 | pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 278 | " from %s \n", cmd->se_lun->unpacked_lun, |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 279 | cmd->orig_fe_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 280 | sess->se_node_acl->initiatorname); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 281 | ret = TCM_RESERVATION_CONFLICT; |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 282 | goto out_unlock; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Bart Van Assche | fae4346 | 2019-04-02 12:58:06 -0700 | [diff] [blame] | 285 | dev->reservation_holder = sess; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 286 | dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 287 | if (sess->sess_bin_isid != 0) { |
| 288 | dev->dev_res_bin_isid = sess->sess_bin_isid; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 289 | dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 290 | } |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 291 | pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 292 | " for %s\n", tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 293 | cmd->se_lun->unpacked_lun, cmd->orig_fe_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 294 | sess->se_node_acl->initiatorname); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 295 | |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 296 | out_unlock: |
| 297 | spin_unlock(&dev->dev_reservation_lock); |
| 298 | out: |
Christoph Hellwig | 6bb35e0 | 2012-04-23 11:35:33 -0400 | [diff] [blame] | 299 | if (!ret) |
Hannes Reinecke | 14b40c1e | 2021-04-27 10:30:45 +0200 | [diff] [blame] | 300 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 301 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * Begin SPC-3/SPC-4 Persistent Reservations emulation support |
| 307 | * |
| 308 | * This function is called by those initiator ports who are *NOT* |
| 309 | * the active PR reservation holder when a reservation is present. |
| 310 | */ |
Christoph Hellwig | 9fcb57f | 2015-03-29 09:39:03 +0200 | [diff] [blame] | 311 | static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type, |
| 312 | bool isid_mismatch) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 313 | { |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 314 | unsigned char *cdb = cmd->t_task_cdb; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 315 | struct se_session *se_sess = cmd->se_sess; |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 316 | struct se_node_acl *nacl = se_sess->se_node_acl; |
Christoph Hellwig | 9fcb57f | 2015-03-29 09:39:03 +0200 | [diff] [blame] | 317 | int other_cdb = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 318 | int registered_nexus = 0, ret = 1; /* Conflict by default */ |
| 319 | int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */ |
| 320 | int we = 0; /* Write Exclusive */ |
| 321 | int legacy = 0; /* Act like a legacy device and return |
| 322 | * RESERVATION CONFLICT on some CDBs */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 323 | |
Christoph Hellwig | 9fcb57f | 2015-03-29 09:39:03 +0200 | [diff] [blame] | 324 | if (isid_mismatch) { |
| 325 | registered_nexus = 0; |
| 326 | } else { |
| 327 | struct se_dev_entry *se_deve; |
| 328 | |
| 329 | rcu_read_lock(); |
| 330 | se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun); |
| 331 | if (se_deve) |
| 332 | registered_nexus = test_bit(DEF_PR_REG_ACTIVE, |
| 333 | &se_deve->deve_flags); |
| 334 | rcu_read_unlock(); |
| 335 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 336 | |
| 337 | switch (pr_reg_type) { |
| 338 | case PR_TYPE_WRITE_EXCLUSIVE: |
| 339 | we = 1; |
Gustavo A. R. Silva | 492096e | 2020-11-20 12:32:32 -0600 | [diff] [blame] | 340 | fallthrough; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 341 | case PR_TYPE_EXCLUSIVE_ACCESS: |
| 342 | /* |
| 343 | * Some commands are only allowed for the persistent reservation |
| 344 | * holder. |
| 345 | */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 346 | break; |
| 347 | case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: |
| 348 | we = 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 349 | fallthrough; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 350 | case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: |
| 351 | /* |
| 352 | * Some commands are only allowed for registered I_T Nexuses. |
| 353 | */ |
| 354 | reg_only = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 355 | break; |
| 356 | case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: |
| 357 | we = 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 358 | fallthrough; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 359 | case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: |
| 360 | /* |
| 361 | * Each registered I_T Nexus is a reservation holder. |
| 362 | */ |
| 363 | all_reg = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 364 | break; |
| 365 | default: |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 366 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 367 | } |
| 368 | /* |
| 369 | * Referenced from spc4r17 table 45 for *NON* PR holder access |
| 370 | */ |
| 371 | switch (cdb[0]) { |
| 372 | case SECURITY_PROTOCOL_IN: |
| 373 | if (registered_nexus) |
| 374 | return 0; |
| 375 | ret = (we) ? 0 : 1; |
| 376 | break; |
| 377 | case MODE_SENSE: |
| 378 | case MODE_SENSE_10: |
| 379 | case READ_ATTRIBUTE: |
| 380 | case READ_BUFFER: |
| 381 | case RECEIVE_DIAGNOSTIC: |
| 382 | if (legacy) { |
| 383 | ret = 1; |
| 384 | break; |
| 385 | } |
| 386 | if (registered_nexus) { |
| 387 | ret = 0; |
| 388 | break; |
| 389 | } |
| 390 | ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ |
| 391 | break; |
| 392 | case PERSISTENT_RESERVE_OUT: |
| 393 | /* |
| 394 | * This follows PERSISTENT_RESERVE_OUT service actions that |
| 395 | * are allowed in the presence of various reservations. |
| 396 | * See spc4r17, table 46 |
| 397 | */ |
| 398 | switch (cdb[1] & 0x1f) { |
| 399 | case PRO_CLEAR: |
| 400 | case PRO_PREEMPT: |
| 401 | case PRO_PREEMPT_AND_ABORT: |
| 402 | ret = (registered_nexus) ? 0 : 1; |
| 403 | break; |
| 404 | case PRO_REGISTER: |
| 405 | case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: |
| 406 | ret = 0; |
| 407 | break; |
| 408 | case PRO_REGISTER_AND_MOVE: |
| 409 | case PRO_RESERVE: |
| 410 | ret = 1; |
| 411 | break; |
| 412 | case PRO_RELEASE: |
| 413 | ret = (registered_nexus) ? 0 : 1; |
| 414 | break; |
| 415 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 416 | pr_err("Unknown PERSISTENT_RESERVE_OUT service" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 417 | " action: 0x%02x\n", cdb[1] & 0x1f); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 418 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 419 | } |
| 420 | break; |
| 421 | case RELEASE: |
| 422 | case RELEASE_10: |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 423 | /* Handled by CRH=1 in target_scsi2_reservation_release() */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 424 | ret = 0; |
| 425 | break; |
| 426 | case RESERVE: |
| 427 | case RESERVE_10: |
Christoph Hellwig | eacac00 | 2011-11-03 17:50:40 -0400 | [diff] [blame] | 428 | /* Handled by CRH=1 in target_scsi2_reservation_reserve() */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 429 | ret = 0; |
| 430 | break; |
| 431 | case TEST_UNIT_READY: |
| 432 | ret = (legacy) ? 1 : 0; /* Conflict for legacy */ |
| 433 | break; |
| 434 | case MAINTENANCE_IN: |
| 435 | switch (cdb[1] & 0x1f) { |
| 436 | case MI_MANAGEMENT_PROTOCOL_IN: |
| 437 | if (registered_nexus) { |
| 438 | ret = 0; |
| 439 | break; |
| 440 | } |
| 441 | ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ |
| 442 | break; |
| 443 | case MI_REPORT_SUPPORTED_OPERATION_CODES: |
| 444 | case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS: |
| 445 | if (legacy) { |
| 446 | ret = 1; |
| 447 | break; |
| 448 | } |
| 449 | if (registered_nexus) { |
| 450 | ret = 0; |
| 451 | break; |
| 452 | } |
| 453 | ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ |
| 454 | break; |
| 455 | case MI_REPORT_ALIASES: |
| 456 | case MI_REPORT_IDENTIFYING_INFORMATION: |
| 457 | case MI_REPORT_PRIORITY: |
| 458 | case MI_REPORT_TARGET_PGS: |
| 459 | case MI_REPORT_TIMESTAMP: |
| 460 | ret = 0; /* Allowed */ |
| 461 | break; |
| 462 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 463 | pr_err("Unknown MI Service Action: 0x%02x\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 464 | (cdb[1] & 0x1f)); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 465 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 466 | } |
| 467 | break; |
| 468 | case ACCESS_CONTROL_IN: |
| 469 | case ACCESS_CONTROL_OUT: |
| 470 | case INQUIRY: |
| 471 | case LOG_SENSE: |
Hannes Reinecke | 85686f6 | 2014-11-17 14:25:20 +0100 | [diff] [blame] | 472 | case SERVICE_ACTION_IN_12: |
Benjamin Coddington | 28c58f8 | 2023-11-29 11:13:54 -0500 | [diff] [blame] | 473 | case READ_CAPACITY: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 474 | case REPORT_LUNS: |
| 475 | case REQUEST_SENSE: |
Marco Sanvido | 6816966 | 2012-01-03 17:12:58 -0800 | [diff] [blame] | 476 | case PERSISTENT_RESERVE_IN: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 477 | ret = 0; /*/ Allowed CDBs */ |
| 478 | break; |
| 479 | default: |
| 480 | other_cdb = 1; |
| 481 | break; |
| 482 | } |
| 483 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 484 | * Case where the CDB is explicitly allowed in the above switch |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 485 | * statement. |
| 486 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 487 | if (!ret && !other_cdb) { |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 488 | pr_debug("Allowing explicit CDB: 0x%02x for %s" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 489 | " reservation holder\n", cdb[0], |
| 490 | core_scsi3_pr_dump_type(pr_reg_type)); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 491 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 492 | return ret; |
| 493 | } |
| 494 | /* |
| 495 | * Check if write exclusive initiator ports *NOT* holding the |
| 496 | * WRITE_EXCLUSIVE_* reservation. |
| 497 | */ |
Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 498 | if (we && !registered_nexus) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 499 | if (cmd->data_direction == DMA_TO_DEVICE) { |
| 500 | /* |
| 501 | * Conflict for write exclusive |
| 502 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 503 | pr_debug("%s Conflict for unregistered nexus" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 504 | " %s CDB: 0x%02x to %s reservation\n", |
| 505 | transport_dump_cmd_direction(cmd), |
| 506 | se_sess->se_node_acl->initiatorname, cdb[0], |
| 507 | core_scsi3_pr_dump_type(pr_reg_type)); |
| 508 | return 1; |
| 509 | } else { |
| 510 | /* |
| 511 | * Allow non WRITE CDBs for all Write Exclusive |
| 512 | * PR TYPEs to pass for registered and |
| 513 | * non-registered_nexuxes NOT holding the reservation. |
| 514 | * |
| 515 | * We only make noise for the unregisterd nexuses, |
| 516 | * as we expect registered non-reservation holding |
| 517 | * nexuses to issue CDBs. |
| 518 | */ |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 519 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 520 | if (!registered_nexus) { |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 521 | pr_debug("Allowing implicit CDB: 0x%02x" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 522 | " for %s reservation on unregistered" |
| 523 | " nexus\n", cdb[0], |
| 524 | core_scsi3_pr_dump_type(pr_reg_type)); |
| 525 | } |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 526 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | } else if ((reg_only) || (all_reg)) { |
| 530 | if (registered_nexus) { |
| 531 | /* |
| 532 | * For PR_*_REG_ONLY and PR_*_ALL_REG reservations, |
| 533 | * allow commands from registered nexuses. |
| 534 | */ |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 535 | |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 536 | pr_debug("Allowing implicit CDB: 0x%02x for %s" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 537 | " reservation\n", cdb[0], |
| 538 | core_scsi3_pr_dump_type(pr_reg_type)); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 539 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 540 | return 0; |
| 541 | } |
Lee Duncan | 1ecc758 | 2015-01-05 10:49:44 -0800 | [diff] [blame] | 542 | } else if (we && registered_nexus) { |
| 543 | /* |
| 544 | * Reads are allowed for Write Exclusive locks |
| 545 | * from all registrants. |
| 546 | */ |
| 547 | if (cmd->data_direction == DMA_FROM_DEVICE) { |
| 548 | pr_debug("Allowing READ CDB: 0x%02x for %s" |
| 549 | " reservation\n", cdb[0], |
| 550 | core_scsi3_pr_dump_type(pr_reg_type)); |
| 551 | |
| 552 | return 0; |
| 553 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 554 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 555 | pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 556 | " for %s reservation\n", transport_dump_cmd_direction(cmd), |
| 557 | (registered_nexus) ? "" : "un", |
| 558 | se_sess->se_node_acl->initiatorname, cdb[0], |
| 559 | core_scsi3_pr_dump_type(pr_reg_type)); |
| 560 | |
| 561 | return 1; /* Conflict by default */ |
| 562 | } |
| 563 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 564 | static sense_reason_t |
| 565 | target_scsi3_pr_reservation_check(struct se_cmd *cmd) |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 566 | { |
| 567 | struct se_device *dev = cmd->se_dev; |
| 568 | struct se_session *sess = cmd->se_sess; |
| 569 | u32 pr_reg_type; |
Christoph Hellwig | 9fcb57f | 2015-03-29 09:39:03 +0200 | [diff] [blame] | 570 | bool isid_mismatch = false; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 571 | |
| 572 | if (!dev->dev_pr_res_holder) |
| 573 | return 0; |
| 574 | |
| 575 | pr_reg_type = dev->dev_pr_res_holder->pr_res_type; |
| 576 | cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key; |
| 577 | if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) |
| 578 | goto check_nonholder; |
| 579 | |
| 580 | if (dev->dev_pr_res_holder->isid_present_at_reg) { |
| 581 | if (dev->dev_pr_res_holder->pr_reg_bin_isid != |
| 582 | sess->sess_bin_isid) { |
Christoph Hellwig | 9fcb57f | 2015-03-29 09:39:03 +0200 | [diff] [blame] | 583 | isid_mismatch = true; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 584 | goto check_nonholder; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | return 0; |
| 589 | |
| 590 | check_nonholder: |
Christoph Hellwig | 9fcb57f | 2015-03-29 09:39:03 +0200 | [diff] [blame] | 591 | if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch)) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 592 | return TCM_RESERVATION_CONFLICT; |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 596 | static u32 core_scsi3_pr_generation(struct se_device *dev) |
| 597 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 598 | u32 prg; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 599 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 600 | /* |
| 601 | * PRGeneration field shall contain the value of a 32-bit wrapping |
| 602 | * counter mainted by the device server. |
| 603 | * |
| 604 | * Note that this is done regardless of Active Persist across |
| 605 | * Target PowerLoss (APTPL) |
| 606 | * |
| 607 | * See spc4r17 section 6.3.12 READ_KEYS service action |
| 608 | */ |
| 609 | spin_lock(&dev->dev_reservation_lock); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 610 | prg = dev->t10_pr.pr_generation++; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 611 | spin_unlock(&dev->dev_reservation_lock); |
| 612 | |
| 613 | return prg; |
| 614 | } |
| 615 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 616 | static struct t10_pr_registration *__core_scsi3_do_alloc_registration( |
| 617 | struct se_device *dev, |
| 618 | struct se_node_acl *nacl, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 619 | struct se_lun *lun, |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 620 | struct se_dev_entry *dest_deve, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 621 | u64 mapped_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 622 | unsigned char *isid, |
| 623 | u64 sa_res_key, |
| 624 | int all_tg_pt, |
| 625 | int aptpl) |
| 626 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 627 | struct t10_pr_registration *pr_reg; |
| 628 | |
| 629 | pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 630 | if (!pr_reg) { |
| 631 | pr_err("Unable to allocate struct t10_pr_registration\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 632 | return NULL; |
| 633 | } |
| 634 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 635 | INIT_LIST_HEAD(&pr_reg->pr_reg_list); |
| 636 | INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); |
| 637 | INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); |
| 638 | INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); |
| 639 | INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); |
| 640 | atomic_set(&pr_reg->pr_res_holders, 0); |
| 641 | pr_reg->pr_reg_nacl = nacl; |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 642 | /* |
| 643 | * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1, |
| 644 | * the se_dev_entry->pr_ref will have been already obtained by |
| 645 | * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration(). |
| 646 | * |
| 647 | * Otherwise, locate se_dev_entry now and obtain a reference until |
| 648 | * registration completes in __core_scsi3_add_registration(). |
| 649 | */ |
| 650 | if (dest_deve) { |
| 651 | pr_reg->pr_reg_deve = dest_deve; |
| 652 | } else { |
| 653 | rcu_read_lock(); |
| 654 | pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun); |
| 655 | if (!pr_reg->pr_reg_deve) { |
| 656 | rcu_read_unlock(); |
| 657 | pr_err("Unable to locate PR deve %s mapped_lun: %llu\n", |
| 658 | nacl->initiatorname, mapped_lun); |
| 659 | kmem_cache_free(t10_pr_reg_cache, pr_reg); |
| 660 | return NULL; |
| 661 | } |
| 662 | kref_get(&pr_reg->pr_reg_deve->pr_kref); |
| 663 | rcu_read_unlock(); |
| 664 | } |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 665 | pr_reg->pr_res_mapped_lun = mapped_lun; |
| 666 | pr_reg->pr_aptpl_target_lun = lun->unpacked_lun; |
Roman Bolshakov | b9e063a | 2023-03-01 11:45:10 +0300 | [diff] [blame] | 667 | pr_reg->tg_pt_sep_rtpi = lun->lun_tpg->tpg_rtpi; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 668 | pr_reg->pr_res_key = sa_res_key; |
| 669 | pr_reg->pr_reg_all_tg_pt = all_tg_pt; |
| 670 | pr_reg->pr_reg_aptpl = aptpl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 671 | /* |
| 672 | * If an ISID value for this SCSI Initiator Port exists, |
| 673 | * save it to the registration now. |
| 674 | */ |
| 675 | if (isid != NULL) { |
| 676 | pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); |
| 677 | snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); |
| 678 | pr_reg->isid_present_at_reg = 1; |
| 679 | } |
| 680 | |
| 681 | return pr_reg; |
| 682 | } |
| 683 | |
| 684 | static int core_scsi3_lunacl_depend_item(struct se_dev_entry *); |
| 685 | static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *); |
| 686 | |
| 687 | /* |
| 688 | * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0 |
| 689 | * modes. |
| 690 | */ |
| 691 | static struct t10_pr_registration *__core_scsi3_alloc_registration( |
| 692 | struct se_device *dev, |
| 693 | struct se_node_acl *nacl, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 694 | struct se_lun *lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 695 | struct se_dev_entry *deve, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 696 | u64 mapped_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 697 | unsigned char *isid, |
| 698 | u64 sa_res_key, |
| 699 | int all_tg_pt, |
| 700 | int aptpl) |
| 701 | { |
| 702 | struct se_dev_entry *deve_tmp; |
| 703 | struct se_node_acl *nacl_tmp; |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 704 | struct se_lun_acl *lacl_tmp; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 705 | struct se_lun *lun_tmp, *next, *dest_lun; |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 706 | const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 707 | struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe; |
| 708 | int ret; |
| 709 | /* |
| 710 | * Create a registration for the I_T Nexus upon which the |
| 711 | * PROUT REGISTER was received. |
| 712 | */ |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 713 | pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun, |
| 714 | isid, sa_res_key, all_tg_pt, |
| 715 | aptpl); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 716 | if (!pr_reg) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 717 | return NULL; |
| 718 | /* |
| 719 | * Return pointer to pr_reg for ALL_TG_PT=0 |
| 720 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 721 | if (!all_tg_pt) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 722 | return pr_reg; |
| 723 | /* |
| 724 | * Create list of matching SCSI Initiator Port registrations |
| 725 | * for ALL_TG_PT=1 |
| 726 | */ |
| 727 | spin_lock(&dev->se_port_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 728 | list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) { |
Nicholas Bellinger | 9e37d04 | 2015-05-20 21:21:08 -0700 | [diff] [blame] | 729 | if (!percpu_ref_tryget_live(&lun_tmp->lun_ref)) |
| 730 | continue; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 731 | spin_unlock(&dev->se_port_lock); |
| 732 | |
Nicholas Bellinger | 1adff1b | 2015-06-22 23:44:05 -0700 | [diff] [blame] | 733 | spin_lock(&lun_tmp->lun_deve_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 734 | list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 735 | /* |
| 736 | * This pointer will be NULL for demo mode MappedLUNs |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 737 | * that have not been make explicit via a ConfigFS |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 738 | * MappedLUN group for the SCSI Initiator Node ACL. |
| 739 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 740 | if (!deve_tmp->se_lun_acl) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 741 | continue; |
| 742 | |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 743 | lacl_tmp = deve_tmp->se_lun_acl; |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 744 | nacl_tmp = lacl_tmp->se_lun_nacl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 745 | /* |
| 746 | * Skip the matching struct se_node_acl that is allocated |
| 747 | * above.. |
| 748 | */ |
| 749 | if (nacl == nacl_tmp) |
| 750 | continue; |
| 751 | /* |
| 752 | * Only perform PR registrations for target ports on |
| 753 | * the same fabric module as the REGISTER w/ ALL_TG_PT=1 |
| 754 | * arrived. |
| 755 | */ |
| 756 | if (tfo != nacl_tmp->se_tpg->se_tpg_tfo) |
| 757 | continue; |
| 758 | /* |
| 759 | * Look for a matching Initiator Node ACL in ASCII format |
| 760 | */ |
| 761 | if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname)) |
| 762 | continue; |
| 763 | |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 764 | kref_get(&deve_tmp->pr_kref); |
Nicholas Bellinger | 1adff1b | 2015-06-22 23:44:05 -0700 | [diff] [blame] | 765 | spin_unlock(&lun_tmp->lun_deve_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 766 | /* |
| 767 | * Grab a configfs group dependency that is released |
| 768 | * for the exception path at label out: below, or upon |
| 769 | * completion of adding ALL_TG_PT=1 registrations in |
| 770 | * __core_scsi3_add_registration() |
| 771 | */ |
| 772 | ret = core_scsi3_lunacl_depend_item(deve_tmp); |
| 773 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 774 | pr_err("core_scsi3_lunacl_depend" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 775 | "_item() failed\n"); |
Nicholas Bellinger | 9e37d04 | 2015-05-20 21:21:08 -0700 | [diff] [blame] | 776 | percpu_ref_put(&lun_tmp->lun_ref); |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 777 | kref_put(&deve_tmp->pr_kref, target_pr_kref_release); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 778 | goto out; |
| 779 | } |
| 780 | /* |
| 781 | * Located a matching SCSI Initiator Port on a different |
| 782 | * port, allocate the pr_reg_atp and attach it to the |
| 783 | * pr_reg->pr_reg_atp_list that will be processed once |
| 784 | * the original *pr_reg is processed in |
| 785 | * __core_scsi3_add_registration() |
| 786 | */ |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 787 | dest_lun = deve_tmp->se_lun; |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 788 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 789 | pr_reg_atp = __core_scsi3_do_alloc_registration(dev, |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 790 | nacl_tmp, dest_lun, deve_tmp, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 791 | deve_tmp->mapped_lun, NULL, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 792 | sa_res_key, all_tg_pt, aptpl); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 793 | if (!pr_reg_atp) { |
Nicholas Bellinger | 9e37d04 | 2015-05-20 21:21:08 -0700 | [diff] [blame] | 794 | percpu_ref_put(&lun_tmp->lun_ref); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 795 | core_scsi3_lunacl_undepend_item(deve_tmp); |
| 796 | goto out; |
| 797 | } |
| 798 | |
| 799 | list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list, |
| 800 | &pr_reg->pr_reg_atp_list); |
Nicholas Bellinger | 1adff1b | 2015-06-22 23:44:05 -0700 | [diff] [blame] | 801 | spin_lock(&lun_tmp->lun_deve_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 802 | } |
Nicholas Bellinger | 1adff1b | 2015-06-22 23:44:05 -0700 | [diff] [blame] | 803 | spin_unlock(&lun_tmp->lun_deve_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 804 | |
| 805 | spin_lock(&dev->se_port_lock); |
Nicholas Bellinger | 9e37d04 | 2015-05-20 21:21:08 -0700 | [diff] [blame] | 806 | percpu_ref_put(&lun_tmp->lun_ref); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 807 | } |
| 808 | spin_unlock(&dev->se_port_lock); |
| 809 | |
| 810 | return pr_reg; |
| 811 | out: |
| 812 | list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, |
| 813 | &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { |
| 814 | list_del(&pr_reg_tmp->pr_reg_atp_mem_list); |
| 815 | core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); |
| 816 | kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); |
| 817 | } |
| 818 | kmem_cache_free(t10_pr_reg_cache, pr_reg); |
| 819 | return NULL; |
| 820 | } |
| 821 | |
| 822 | int core_scsi3_alloc_aptpl_registration( |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 823 | struct t10_reservation *pr_tmpl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 824 | u64 sa_res_key, |
| 825 | unsigned char *i_port, |
| 826 | unsigned char *isid, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 827 | u64 mapped_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 828 | unsigned char *t_port, |
| 829 | u16 tpgt, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 830 | u64 target_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 831 | int res_holder, |
| 832 | int all_tg_pt, |
| 833 | u8 type) |
| 834 | { |
| 835 | struct t10_pr_registration *pr_reg; |
| 836 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 837 | if (!i_port || !t_port || !sa_res_key) { |
| 838 | pr_err("Illegal parameters for APTPL registration\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 839 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 843 | if (!pr_reg) { |
| 844 | pr_err("Unable to allocate struct t10_pr_registration\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 845 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 846 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 847 | |
| 848 | INIT_LIST_HEAD(&pr_reg->pr_reg_list); |
| 849 | INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); |
| 850 | INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); |
| 851 | INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); |
| 852 | INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); |
| 853 | atomic_set(&pr_reg->pr_res_holders, 0); |
| 854 | pr_reg->pr_reg_nacl = NULL; |
| 855 | pr_reg->pr_reg_deve = NULL; |
| 856 | pr_reg->pr_res_mapped_lun = mapped_lun; |
| 857 | pr_reg->pr_aptpl_target_lun = target_lun; |
| 858 | pr_reg->pr_res_key = sa_res_key; |
| 859 | pr_reg->pr_reg_all_tg_pt = all_tg_pt; |
| 860 | pr_reg->pr_reg_aptpl = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 861 | pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */ |
| 862 | pr_reg->pr_res_type = type; |
| 863 | /* |
| 864 | * If an ISID value had been saved in APTPL metadata for this |
| 865 | * SCSI Initiator Port, restore it now. |
| 866 | */ |
| 867 | if (isid != NULL) { |
| 868 | pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); |
| 869 | snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); |
| 870 | pr_reg->isid_present_at_reg = 1; |
| 871 | } |
| 872 | /* |
| 873 | * Copy the i_port and t_port information from caller. |
| 874 | */ |
| 875 | snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port); |
| 876 | snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port); |
| 877 | pr_reg->pr_reg_tpgt = tpgt; |
| 878 | /* |
| 879 | * Set pr_res_holder from caller, the pr_reg who is the reservation |
| 880 | * holder will get it's pointer set in core_scsi3_aptpl_reserve() once |
| 881 | * the Initiator Node LUN ACL from the fabric module is created for |
| 882 | * this registration. |
| 883 | */ |
| 884 | pr_reg->pr_res_holder = res_holder; |
| 885 | |
| 886 | list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 887 | pr_debug("SPC-3 PR APTPL Successfully added registration%s from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 888 | " metadata\n", (res_holder) ? "+reservation" : ""); |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | static void core_scsi3_aptpl_reserve( |
| 893 | struct se_device *dev, |
| 894 | struct se_portal_group *tpg, |
| 895 | struct se_node_acl *node_acl, |
| 896 | struct t10_pr_registration *pr_reg) |
| 897 | { |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 898 | char i_buf[PR_REG_ISID_ID_LEN] = { }; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 899 | |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 900 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 901 | |
| 902 | spin_lock(&dev->dev_reservation_lock); |
| 903 | dev->dev_pr_res_holder = pr_reg; |
| 904 | spin_unlock(&dev->dev_reservation_lock); |
| 905 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 906 | pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 907 | " new reservation holder TYPE: %s ALL_TG_PT: %d\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 908 | tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 909 | core_scsi3_pr_dump_type(pr_reg->pr_res_type), |
| 910 | (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 911 | pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 912 | tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname, |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 913 | i_buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 917 | struct t10_pr_registration *, enum register_type, int); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 918 | |
| 919 | static int __core_scsi3_check_aptpl_registration( |
| 920 | struct se_device *dev, |
| 921 | struct se_portal_group *tpg, |
| 922 | struct se_lun *lun, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 923 | u64 target_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 924 | struct se_node_acl *nacl, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 925 | u64 mapped_lun) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 926 | { |
| 927 | struct t10_pr_registration *pr_reg, *pr_reg_tmp; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 928 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 929 | unsigned char i_port[PR_APTPL_MAX_IPORT_LEN] = { }; |
| 930 | unsigned char t_port[PR_APTPL_MAX_TPORT_LEN] = { }; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 931 | u16 tpgt; |
| 932 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 933 | /* |
| 934 | * Copy Initiator Port information from struct se_node_acl |
| 935 | */ |
| 936 | snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname); |
| 937 | snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 938 | tpg->se_tpg_tfo->tpg_get_wwn(tpg)); |
| 939 | tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 940 | /* |
| 941 | * Look for the matching registrations+reservation from those |
| 942 | * created from APTPL metadata. Note that multiple registrations |
| 943 | * may exist for fabrics that use ISIDs in their SCSI Initiator Port |
| 944 | * TransportIDs. |
| 945 | */ |
| 946 | spin_lock(&pr_tmpl->aptpl_reg_lock); |
| 947 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, |
| 948 | pr_reg_aptpl_list) { |
Nicholas Bellinger | 92404e6 | 2014-10-04 01:06:08 +0000 | [diff] [blame] | 949 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 950 | if (!strcmp(pr_reg->pr_iport, i_port) && |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 951 | (pr_reg->pr_res_mapped_lun == mapped_lun) && |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 952 | !(strcmp(pr_reg->pr_tport, t_port)) && |
| 953 | (pr_reg->pr_reg_tpgt == tpgt) && |
| 954 | (pr_reg->pr_aptpl_target_lun == target_lun)) { |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 955 | /* |
| 956 | * Obtain the ->pr_reg_deve pointer + reference, that |
| 957 | * is released by __core_scsi3_add_registration() below. |
| 958 | */ |
| 959 | rcu_read_lock(); |
| 960 | pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun); |
| 961 | if (!pr_reg->pr_reg_deve) { |
| 962 | pr_err("Unable to locate PR APTPL %s mapped_lun:" |
| 963 | " %llu\n", nacl->initiatorname, mapped_lun); |
| 964 | rcu_read_unlock(); |
| 965 | continue; |
| 966 | } |
| 967 | kref_get(&pr_reg->pr_reg_deve->pr_kref); |
| 968 | rcu_read_unlock(); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 969 | |
| 970 | pr_reg->pr_reg_nacl = nacl; |
Roman Bolshakov | b9e063a | 2023-03-01 11:45:10 +0300 | [diff] [blame] | 971 | pr_reg->tg_pt_sep_rtpi = lun->lun_tpg->tpg_rtpi; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 972 | list_del(&pr_reg->pr_reg_aptpl_list); |
| 973 | spin_unlock(&pr_tmpl->aptpl_reg_lock); |
| 974 | /* |
| 975 | * At this point all of the pointers in *pr_reg will |
| 976 | * be setup, so go ahead and add the registration. |
| 977 | */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 978 | __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0); |
| 979 | /* |
| 980 | * If this registration is the reservation holder, |
| 981 | * make that happen now.. |
| 982 | */ |
| 983 | if (pr_reg->pr_res_holder) |
| 984 | core_scsi3_aptpl_reserve(dev, tpg, |
| 985 | nacl, pr_reg); |
| 986 | /* |
| 987 | * Reenable pr_aptpl_active to accept new metadata |
| 988 | * updates once the SCSI device is active again.. |
| 989 | */ |
| 990 | spin_lock(&pr_tmpl->aptpl_reg_lock); |
| 991 | pr_tmpl->pr_aptpl_active = 1; |
| 992 | } |
| 993 | } |
| 994 | spin_unlock(&pr_tmpl->aptpl_reg_lock); |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | int core_scsi3_check_aptpl_registration( |
| 1000 | struct se_device *dev, |
| 1001 | struct se_portal_group *tpg, |
| 1002 | struct se_lun *lun, |
Nicholas Bellinger | e248056 | 2014-10-04 04:23:15 +0000 | [diff] [blame] | 1003 | struct se_node_acl *nacl, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1004 | u64 mapped_lun) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1005 | { |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 1006 | if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1007 | return 0; |
| 1008 | |
| 1009 | return __core_scsi3_check_aptpl_registration(dev, tpg, lun, |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 1010 | lun->unpacked_lun, nacl, |
| 1011 | mapped_lun); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static void __core_scsi3_dump_registration( |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 1015 | const struct target_core_fabric_ops *tfo, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1016 | struct se_device *dev, |
| 1017 | struct se_node_acl *nacl, |
| 1018 | struct t10_pr_registration *pr_reg, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 1019 | enum register_type register_type) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1020 | { |
| 1021 | struct se_portal_group *se_tpg = nacl->se_tpg; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 1022 | char i_buf[PR_REG_ISID_ID_LEN] = { }; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1023 | |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 1024 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1025 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1026 | pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1027 | " Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ? |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 1028 | "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1029 | "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname, |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 1030 | i_buf); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1031 | pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1032 | tfo->fabric_name, tfo->tpg_get_wwn(se_tpg), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1033 | tfo->tpg_get_tag(se_tpg)); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1034 | pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1035 | " Port(s)\n", tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1036 | (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1037 | dev->transport->name); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1038 | pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1039 | " 0x%08x APTPL: %d\n", tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1040 | pr_reg->pr_res_key, pr_reg->pr_res_generation, |
| 1041 | pr_reg->pr_reg_aptpl); |
| 1042 | } |
| 1043 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1044 | static void __core_scsi3_add_registration( |
| 1045 | struct se_device *dev, |
| 1046 | struct se_node_acl *nacl, |
| 1047 | struct t10_pr_registration *pr_reg, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 1048 | enum register_type register_type, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1049 | int register_move) |
| 1050 | { |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 1051 | const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1052 | struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1053 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1054 | struct se_dev_entry *deve; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * Increment PRgeneration counter for struct se_device upon a successful |
| 1058 | * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action |
| 1059 | * |
| 1060 | * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service |
| 1061 | * action, the struct se_device->dev_reservation_lock will already be held, |
| 1062 | * so we do not call core_scsi3_pr_generation() which grabs the lock |
| 1063 | * for the REGISTER. |
| 1064 | */ |
| 1065 | pr_reg->pr_res_generation = (register_move) ? |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1066 | dev->t10_pr.pr_generation++ : |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1067 | core_scsi3_pr_generation(dev); |
| 1068 | |
| 1069 | spin_lock(&pr_tmpl->registration_lock); |
| 1070 | list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1071 | |
| 1072 | __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type); |
| 1073 | spin_unlock(&pr_tmpl->registration_lock); |
| 1074 | /* |
| 1075 | * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE. |
| 1076 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1077 | if (!pr_reg->pr_reg_all_tg_pt || register_move) |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 1078 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1079 | /* |
| 1080 | * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1 |
| 1081 | * allocated in __core_scsi3_alloc_registration() |
| 1082 | */ |
| 1083 | list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, |
| 1084 | &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1085 | struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl; |
| 1086 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1087 | list_del(&pr_reg_tmp->pr_reg_atp_mem_list); |
| 1088 | |
| 1089 | pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev); |
| 1090 | |
| 1091 | spin_lock(&pr_tmpl->registration_lock); |
| 1092 | list_add_tail(&pr_reg_tmp->pr_reg_list, |
| 1093 | &pr_tmpl->registration_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1094 | |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1095 | __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp, |
| 1096 | register_type); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1097 | spin_unlock(&pr_tmpl->registration_lock); |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 1098 | /* |
| 1099 | * Drop configfs group dependency reference and deve->pr_kref |
| 1100 | * obtained from __core_scsi3_alloc_registration() code. |
| 1101 | */ |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1102 | rcu_read_lock(); |
| 1103 | deve = pr_reg_tmp->pr_reg_deve; |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 1104 | if (deve) { |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1105 | set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags); |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 1106 | core_scsi3_lunacl_undepend_item(deve); |
| 1107 | pr_reg_tmp->pr_reg_deve = NULL; |
| 1108 | } |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1109 | rcu_read_unlock(); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1110 | } |
Nicholas Bellinger | 3ccd6e8 | 2015-09-13 02:30:46 -0700 | [diff] [blame] | 1111 | out: |
| 1112 | /* |
| 1113 | * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration() |
| 1114 | */ |
| 1115 | rcu_read_lock(); |
| 1116 | deve = pr_reg->pr_reg_deve; |
| 1117 | if (deve) { |
| 1118 | set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags); |
| 1119 | kref_put(&deve->pr_kref, target_pr_kref_release); |
| 1120 | pr_reg->pr_reg_deve = NULL; |
| 1121 | } |
| 1122 | rcu_read_unlock(); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | static int core_scsi3_alloc_registration( |
| 1126 | struct se_device *dev, |
| 1127 | struct se_node_acl *nacl, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1128 | struct se_lun *lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1129 | struct se_dev_entry *deve, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1130 | u64 mapped_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1131 | unsigned char *isid, |
| 1132 | u64 sa_res_key, |
| 1133 | int all_tg_pt, |
| 1134 | int aptpl, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 1135 | enum register_type register_type, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1136 | int register_move) |
| 1137 | { |
| 1138 | struct t10_pr_registration *pr_reg; |
| 1139 | |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1140 | pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun, |
| 1141 | isid, sa_res_key, all_tg_pt, |
| 1142 | aptpl); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1143 | if (!pr_reg) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1144 | return -EPERM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1145 | |
| 1146 | __core_scsi3_add_registration(dev, nacl, pr_reg, |
| 1147 | register_type, register_move); |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | static struct t10_pr_registration *__core_scsi3_locate_pr_reg( |
| 1152 | struct se_device *dev, |
| 1153 | struct se_node_acl *nacl, |
| 1154 | unsigned char *isid) |
| 1155 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1156 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1157 | struct t10_pr_registration *pr_reg, *pr_reg_tmp; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1158 | |
| 1159 | spin_lock(&pr_tmpl->registration_lock); |
| 1160 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 1161 | &pr_tmpl->registration_list, pr_reg_list) { |
| 1162 | /* |
| 1163 | * First look for a matching struct se_node_acl |
| 1164 | */ |
| 1165 | if (pr_reg->pr_reg_nacl != nacl) |
| 1166 | continue; |
| 1167 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1168 | /* |
| 1169 | * If this registration does NOT contain a fabric provided |
| 1170 | * ISID, then we have found a match. |
| 1171 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1172 | if (!pr_reg->isid_present_at_reg) { |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1173 | atomic_inc_mb(&pr_reg->pr_res_holders); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1174 | spin_unlock(&pr_tmpl->registration_lock); |
| 1175 | return pr_reg; |
| 1176 | } |
| 1177 | /* |
| 1178 | * If the *pr_reg contains a fabric defined ISID for multi-value |
| 1179 | * SCSI Initiator Port TransportIDs, then we expect a valid |
| 1180 | * matching ISID to be provided by the local SCSI Initiator Port. |
| 1181 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1182 | if (!isid) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1183 | continue; |
| 1184 | if (strcmp(isid, pr_reg->pr_reg_isid)) |
| 1185 | continue; |
| 1186 | |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1187 | atomic_inc_mb(&pr_reg->pr_res_holders); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1188 | spin_unlock(&pr_tmpl->registration_lock); |
| 1189 | return pr_reg; |
| 1190 | } |
| 1191 | spin_unlock(&pr_tmpl->registration_lock); |
| 1192 | |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | |
| 1196 | static struct t10_pr_registration *core_scsi3_locate_pr_reg( |
| 1197 | struct se_device *dev, |
| 1198 | struct se_node_acl *nacl, |
| 1199 | struct se_session *sess) |
| 1200 | { |
| 1201 | struct se_portal_group *tpg = nacl->se_tpg; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 1202 | unsigned char buf[PR_REG_ISID_LEN] = { }; |
| 1203 | unsigned char *isid_ptr = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1204 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1205 | if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1206 | tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1207 | PR_REG_ISID_LEN); |
| 1208 | isid_ptr = &buf[0]; |
| 1209 | } |
| 1210 | |
| 1211 | return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr); |
| 1212 | } |
| 1213 | |
| 1214 | static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg) |
| 1215 | { |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1216 | atomic_dec_mb(&pr_reg->pr_res_holders); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1219 | static int core_scsi3_check_implicit_release( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1220 | struct se_device *dev, |
| 1221 | struct t10_pr_registration *pr_reg) |
| 1222 | { |
| 1223 | struct se_node_acl *nacl = pr_reg->pr_reg_nacl; |
| 1224 | struct t10_pr_registration *pr_res_holder; |
| 1225 | int ret = 0; |
| 1226 | |
| 1227 | spin_lock(&dev->dev_reservation_lock); |
| 1228 | pr_res_holder = dev->dev_pr_res_holder; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1229 | if (!pr_res_holder) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1230 | spin_unlock(&dev->dev_reservation_lock); |
| 1231 | return ret; |
| 1232 | } |
| 1233 | if (pr_res_holder == pr_reg) { |
| 1234 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1235 | * Perform an implicit RELEASE if the registration that |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1236 | * is being released is holding the reservation. |
| 1237 | * |
| 1238 | * From spc4r17, section 5.7.11.1: |
| 1239 | * |
| 1240 | * e) If the I_T nexus is the persistent reservation holder |
| 1241 | * and the persistent reservation is not an all registrants |
| 1242 | * type, then a PERSISTENT RESERVE OUT command with REGISTER |
| 1243 | * service action or REGISTER AND IGNORE EXISTING KEY |
| 1244 | * service action with the SERVICE ACTION RESERVATION KEY |
| 1245 | * field set to zero (see 5.7.11.3). |
| 1246 | */ |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 1247 | __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1248 | ret = 1; |
| 1249 | /* |
| 1250 | * For 'All Registrants' reservation types, all existing |
| 1251 | * registrations are still processed as reservation holders |
| 1252 | * in core_scsi3_pr_seq_non_holder() after the initial |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1253 | * reservation holder is implicitly released here. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1254 | */ |
| 1255 | } else if (pr_reg->pr_reg_all_tg_pt && |
| 1256 | (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname, |
| 1257 | pr_reg->pr_reg_nacl->initiatorname)) && |
| 1258 | (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1259 | pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1260 | " UNREGISTER while existing reservation with matching" |
| 1261 | " key 0x%016Lx is present from another SCSI Initiator" |
| 1262 | " Port\n", pr_reg->pr_res_key); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1263 | ret = -EPERM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1264 | } |
| 1265 | spin_unlock(&dev->dev_reservation_lock); |
| 1266 | |
| 1267 | return ret; |
| 1268 | } |
| 1269 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1270 | static void __core_scsi3_free_registration( |
| 1271 | struct se_device *dev, |
| 1272 | struct t10_pr_registration *pr_reg, |
| 1273 | struct list_head *preempt_and_abort_list, |
| 1274 | int dec_holders) |
Bart Van Assche | cb0df4d | 2015-04-10 14:49:02 +0200 | [diff] [blame] | 1275 | __releases(&pr_tmpl->registration_lock) |
| 1276 | __acquires(&pr_tmpl->registration_lock) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1277 | { |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 1278 | const struct target_core_fabric_ops *tfo = |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1279 | pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1280 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1281 | struct se_node_acl *nacl = pr_reg->pr_reg_nacl; |
| 1282 | struct se_dev_entry *deve; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 1283 | char i_buf[PR_REG_ISID_ID_LEN] = { }; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1284 | |
Bart Van Assche | 618baaf | 2019-01-25 10:34:53 -0800 | [diff] [blame] | 1285 | lockdep_assert_held(&pr_tmpl->registration_lock); |
| 1286 | |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 1287 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1288 | |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 1289 | if (!list_empty(&pr_reg->pr_reg_list)) |
| 1290 | list_del(&pr_reg->pr_reg_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1291 | /* |
| 1292 | * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(), |
| 1293 | * so call core_scsi3_put_pr_reg() to decrement our reference. |
| 1294 | */ |
| 1295 | if (dec_holders) |
| 1296 | core_scsi3_put_pr_reg(pr_reg); |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1297 | |
| 1298 | spin_unlock(&pr_tmpl->registration_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1299 | /* |
| 1300 | * Wait until all reference from any other I_T nexuses for this |
| 1301 | * *pr_reg have been released. Because list_del() is called above, |
| 1302 | * the last core_scsi3_put_pr_reg(pr_reg) will release this reference |
| 1303 | * count back to zero, and we release *pr_reg. |
| 1304 | */ |
| 1305 | while (atomic_read(&pr_reg->pr_res_holders) != 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1306 | pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1307 | tfo->fabric_name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1308 | cpu_relax(); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 1311 | rcu_read_lock(); |
| 1312 | deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun); |
| 1313 | if (deve) |
| 1314 | clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags); |
| 1315 | rcu_read_unlock(); |
| 1316 | |
| 1317 | spin_lock(&pr_tmpl->registration_lock); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1318 | pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1319 | " Node: %s%s\n", tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1320 | pr_reg->pr_reg_nacl->initiatorname, |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 1321 | i_buf); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1322 | pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1323 | " Port(s)\n", tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1324 | (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1325 | dev->transport->name); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1326 | pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1327 | " 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1328 | pr_reg->pr_res_generation); |
| 1329 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1330 | if (!preempt_and_abort_list) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1331 | pr_reg->pr_reg_deve = NULL; |
| 1332 | pr_reg->pr_reg_nacl = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1333 | kmem_cache_free(t10_pr_reg_cache, pr_reg); |
| 1334 | return; |
| 1335 | } |
| 1336 | /* |
| 1337 | * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list |
| 1338 | * are released once the ABORT_TASK_SET has completed.. |
| 1339 | */ |
| 1340 | list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list); |
| 1341 | } |
| 1342 | |
| 1343 | void core_scsi3_free_pr_reg_from_nacl( |
| 1344 | struct se_device *dev, |
| 1345 | struct se_node_acl *nacl) |
| 1346 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1347 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1348 | struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 1349 | bool free_reg = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1350 | /* |
| 1351 | * If the passed se_node_acl matches the reservation holder, |
| 1352 | * release the reservation. |
| 1353 | */ |
| 1354 | spin_lock(&dev->dev_reservation_lock); |
| 1355 | pr_res_holder = dev->dev_pr_res_holder; |
| 1356 | if ((pr_res_holder != NULL) && |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 1357 | (pr_res_holder->pr_reg_nacl == nacl)) { |
| 1358 | __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1); |
| 1359 | free_reg = true; |
| 1360 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1361 | spin_unlock(&dev->dev_reservation_lock); |
| 1362 | /* |
| 1363 | * Release any registration associated with the struct se_node_acl. |
| 1364 | */ |
| 1365 | spin_lock(&pr_tmpl->registration_lock); |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 1366 | if (pr_res_holder && free_reg) |
| 1367 | __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0); |
| 1368 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1369 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 1370 | &pr_tmpl->registration_list, pr_reg_list) { |
| 1371 | |
| 1372 | if (pr_reg->pr_reg_nacl != nacl) |
| 1373 | continue; |
| 1374 | |
| 1375 | __core_scsi3_free_registration(dev, pr_reg, NULL, 0); |
| 1376 | } |
| 1377 | spin_unlock(&pr_tmpl->registration_lock); |
| 1378 | } |
| 1379 | |
| 1380 | void core_scsi3_free_all_registrations( |
| 1381 | struct se_device *dev) |
| 1382 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1383 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1384 | struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; |
| 1385 | |
| 1386 | spin_lock(&dev->dev_reservation_lock); |
| 1387 | pr_res_holder = dev->dev_pr_res_holder; |
| 1388 | if (pr_res_holder != NULL) { |
| 1389 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; |
| 1390 | __core_scsi3_complete_pro_release(dev, pr_res_nacl, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 1391 | pr_res_holder, 0, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1392 | } |
| 1393 | spin_unlock(&dev->dev_reservation_lock); |
| 1394 | |
| 1395 | spin_lock(&pr_tmpl->registration_lock); |
| 1396 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 1397 | &pr_tmpl->registration_list, pr_reg_list) { |
| 1398 | |
| 1399 | __core_scsi3_free_registration(dev, pr_reg, NULL, 0); |
| 1400 | } |
| 1401 | spin_unlock(&pr_tmpl->registration_lock); |
| 1402 | |
| 1403 | spin_lock(&pr_tmpl->aptpl_reg_lock); |
| 1404 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, |
| 1405 | pr_reg_aptpl_list) { |
| 1406 | list_del(&pr_reg->pr_reg_aptpl_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1407 | kmem_cache_free(t10_pr_reg_cache, pr_reg); |
| 1408 | } |
| 1409 | spin_unlock(&pr_tmpl->aptpl_reg_lock); |
| 1410 | } |
| 1411 | |
| 1412 | static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg) |
| 1413 | { |
Christoph Hellwig | d588cf8 | 2015-05-03 08:50:52 +0200 | [diff] [blame] | 1414 | return target_depend_item(&tpg->tpg_group.cg_item); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg) |
| 1418 | { |
Christoph Hellwig | d588cf8 | 2015-05-03 08:50:52 +0200 | [diff] [blame] | 1419 | target_undepend_item(&tpg->tpg_group.cg_item); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1420 | atomic_dec_mb(&tpg->tpg_pr_ref_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl) |
| 1424 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1425 | if (nacl->dynamic_node_acl) |
| 1426 | return 0; |
Christoph Hellwig | d588cf8 | 2015-05-03 08:50:52 +0200 | [diff] [blame] | 1427 | return target_depend_item(&nacl->acl_group.cg_item); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl) |
| 1431 | { |
Christoph Hellwig | d588cf8 | 2015-05-03 08:50:52 +0200 | [diff] [blame] | 1432 | if (!nacl->dynamic_node_acl) |
| 1433 | target_undepend_item(&nacl->acl_group.cg_item); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1434 | atomic_dec_mb(&nacl->acl_pr_ref_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve) |
| 1438 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1439 | /* |
| 1440 | * For nacl->dynamic_node_acl=1 |
| 1441 | */ |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 1442 | if (!se_deve->se_lun_acl) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1443 | return 0; |
| 1444 | |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 1445 | return target_depend_item(&se_deve->se_lun_acl->se_lun_group.cg_item); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve) |
| 1449 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1450 | /* |
| 1451 | * For nacl->dynamic_node_acl=1 |
| 1452 | */ |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 1453 | if (!se_deve->se_lun_acl) { |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 1454 | kref_put(&se_deve->pr_kref, target_pr_kref_release); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1455 | return; |
| 1456 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1457 | |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 1458 | target_undepend_item(&se_deve->se_lun_acl->se_lun_group.cg_item); |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 1459 | kref_put(&se_deve->pr_kref, target_pr_kref_release); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1462 | static sense_reason_t |
| 1463 | core_scsi3_decode_spec_i_port( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1464 | struct se_cmd *cmd, |
| 1465 | struct se_portal_group *tpg, |
| 1466 | unsigned char *l_isid, |
| 1467 | u64 sa_res_key, |
| 1468 | int all_tg_pt, |
| 1469 | int aptpl) |
| 1470 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1471 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1472 | struct se_portal_group *dest_tpg = NULL, *tmp_tpg; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1473 | struct se_session *se_sess = cmd->se_sess; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1474 | struct se_node_acl *dest_node_acl = NULL; |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1475 | struct se_dev_entry *dest_se_deve = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1476 | struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e; |
| 1477 | struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; |
Roland Dreier | d0f474e | 2012-01-12 10:41:18 -0800 | [diff] [blame] | 1478 | LIST_HEAD(tid_dest_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1479 | struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp; |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 1480 | unsigned char *buf, *ptr, proto_ident; |
Sagi Grimberg | 9b353cc | 2015-06-29 13:07:06 +0300 | [diff] [blame] | 1481 | const unsigned char *i_str = NULL; |
Julia Lawall | 13ba564 | 2014-11-30 19:14:12 +0100 | [diff] [blame] | 1482 | char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN]; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1483 | sense_reason_t ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1484 | u32 tpdl, tid_len = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1485 | u32 dest_rtpi = 0; |
| 1486 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1487 | /* |
| 1488 | * Allocate a struct pr_transport_id_holder and setup the |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1489 | * local_node_acl pointer and add to struct list_head tid_dest_list |
| 1490 | * for add registration processing in the loop of tid_dest_list below. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1491 | */ |
| 1492 | tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1493 | if (!tidh_new) { |
| 1494 | pr_err("Unable to allocate tidh_new\n"); |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 1495 | return TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1496 | } |
| 1497 | INIT_LIST_HEAD(&tidh_new->dest_list); |
| 1498 | tidh_new->dest_tpg = tpg; |
| 1499 | tidh_new->dest_node_acl = se_sess->se_node_acl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1500 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1501 | local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1502 | se_sess->se_node_acl, cmd->se_lun, |
| 1503 | NULL, cmd->orig_fe_lun, l_isid, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1504 | sa_res_key, all_tg_pt, aptpl); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1505 | if (!local_pr_reg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1506 | kfree(tidh_new); |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 1507 | return TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1508 | } |
Mike Christie | f32ba61 | 2020-07-01 20:43:19 -0500 | [diff] [blame] | 1509 | |
| 1510 | if (core_scsi3_lunacl_depend_item(local_pr_reg->pr_reg_deve)) { |
| 1511 | kfree(tidh_new); |
| 1512 | kref_put(&local_pr_reg->pr_reg_deve->pr_kref, |
| 1513 | target_pr_kref_release); |
| 1514 | kmem_cache_free(t10_pr_reg_cache, local_pr_reg); |
| 1515 | return TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
| 1516 | } |
| 1517 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1518 | tidh_new->dest_pr_reg = local_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1519 | list_add_tail(&tidh_new->dest_list, &tid_dest_list); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 1520 | |
Paolo Bonzini | 0d7f129 | 2012-09-07 17:30:33 +0200 | [diff] [blame] | 1521 | if (cmd->data_length < 28) { |
| 1522 | pr_warn("SPC-PR: Received PR OUT parameter list" |
| 1523 | " length too small: %u\n", cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1524 | ret = TCM_INVALID_PARAMETER_LIST; |
Paolo Bonzini | 0d7f129 | 2012-09-07 17:30:33 +0200 | [diff] [blame] | 1525 | goto out; |
| 1526 | } |
| 1527 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 1528 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1529 | if (!buf) { |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 1530 | ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1531 | goto out; |
| 1532 | } |
| 1533 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1534 | /* |
| 1535 | * For a PERSISTENT RESERVE OUT specify initiator ports payload, |
| 1536 | * first extract TransportID Parameter Data Length, and make sure |
| 1537 | * the value matches up to the SCSI expected data transfer length. |
| 1538 | */ |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 1539 | tpdl = get_unaligned_be32(&buf[24]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1540 | |
| 1541 | if ((tpdl + 28) != cmd->data_length) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1542 | pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1543 | " does not equal CDB data_length: %u\n", tpdl, |
| 1544 | cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1545 | ret = TCM_INVALID_PARAMETER_LIST; |
| 1546 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1547 | } |
| 1548 | /* |
| 1549 | * Start processing the received transport IDs using the |
| 1550 | * receiving I_T Nexus portal's fabric dependent methods to |
| 1551 | * obtain the SCSI Initiator Port/Device Identifiers. |
| 1552 | */ |
| 1553 | ptr = &buf[28]; |
| 1554 | |
| 1555 | while (tpdl > 0) { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1556 | struct se_lun *dest_lun, *tmp_lun; |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1557 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1558 | proto_ident = (ptr[0] & 0x0f); |
| 1559 | dest_tpg = NULL; |
| 1560 | |
| 1561 | spin_lock(&dev->se_port_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1562 | list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) { |
| 1563 | tmp_tpg = tmp_lun->lun_tpg; |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 1564 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1565 | /* |
| 1566 | * Look for the matching proto_ident provided by |
| 1567 | * the received TransportID |
| 1568 | */ |
Christoph Hellwig | 2aeeafa | 2015-05-01 17:47:57 +0200 | [diff] [blame] | 1569 | if (tmp_tpg->proto_id != proto_ident) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1570 | continue; |
Roman Bolshakov | b9e063a | 2023-03-01 11:45:10 +0300 | [diff] [blame] | 1571 | dest_rtpi = tmp_lun->lun_tpg->tpg_rtpi; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1572 | |
Mike Christie | 63c9ffe | 2020-07-01 20:43:17 -0500 | [diff] [blame] | 1573 | iport_ptr = NULL; |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 1574 | i_str = target_parse_pr_out_transport_id(tmp_tpg, |
Rasmus Villemoes | 094bb5d | 2017-11-21 01:12:43 +0100 | [diff] [blame] | 1575 | ptr, &tid_len, &iport_ptr); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1576 | if (!i_str) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1577 | continue; |
Mike Christie | 63c9ffe | 2020-07-01 20:43:17 -0500 | [diff] [blame] | 1578 | /* |
| 1579 | * Determine if this SCSI device server requires that |
| 1580 | * SCSI Intiatior TransportID w/ ISIDs is enforced |
| 1581 | * for fabric modules (iSCSI) requiring them. |
| 1582 | */ |
| 1583 | if (tpg->se_tpg_tfo->sess_get_initiator_sid && |
| 1584 | dev->dev_attrib.enforce_pr_isids && |
| 1585 | !iport_ptr) { |
| 1586 | pr_warn("SPC-PR: enforce_pr_isids is set but a isid has not been sent in the SPEC_I_PT data for %s.", |
| 1587 | i_str); |
| 1588 | ret = TCM_INVALID_PARAMETER_LIST; |
| 1589 | spin_unlock(&dev->se_port_lock); |
| 1590 | goto out_unmap; |
| 1591 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1592 | |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1593 | atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1594 | spin_unlock(&dev->se_port_lock); |
| 1595 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1596 | if (core_scsi3_tpg_depend_item(tmp_tpg)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1597 | pr_err(" core_scsi3_tpg_depend_item()" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1598 | " for tmp_tpg\n"); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1599 | atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1600 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1601 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1602 | } |
| 1603 | /* |
Masanari Iida | 35d1efe | 2012-08-16 22:43:13 +0900 | [diff] [blame] | 1604 | * Locate the destination initiator ACL to be registered |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1605 | * from the decoded fabric module specific TransportID |
| 1606 | * at *i_str. |
| 1607 | */ |
Nicholas Bellinger | 403edd7 | 2015-03-08 22:33:47 +0000 | [diff] [blame] | 1608 | mutex_lock(&tmp_tpg->acl_node_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1609 | dest_node_acl = __core_tpg_get_initiator_node_acl( |
| 1610 | tmp_tpg, i_str); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1611 | if (dest_node_acl) |
| 1612 | atomic_inc_mb(&dest_node_acl->acl_pr_ref_count); |
Nicholas Bellinger | 403edd7 | 2015-03-08 22:33:47 +0000 | [diff] [blame] | 1613 | mutex_unlock(&tmp_tpg->acl_node_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1614 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1615 | if (!dest_node_acl) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1616 | core_scsi3_tpg_undepend_item(tmp_tpg); |
| 1617 | spin_lock(&dev->se_port_lock); |
| 1618 | continue; |
| 1619 | } |
| 1620 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1621 | if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1622 | pr_err("configfs_depend_item() failed" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1623 | " for dest_node_acl->acl_group\n"); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1624 | atomic_dec_mb(&dest_node_acl->acl_pr_ref_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1625 | core_scsi3_tpg_undepend_item(tmp_tpg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1626 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1627 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | dest_tpg = tmp_tpg; |
Bart Van Assche | e15c745 | 2021-04-15 15:08:24 -0700 | [diff] [blame] | 1631 | pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s Port RTPI: %u\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1632 | dest_tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1633 | dest_node_acl->initiatorname, dest_rtpi); |
| 1634 | |
| 1635 | spin_lock(&dev->se_port_lock); |
| 1636 | break; |
| 1637 | } |
| 1638 | spin_unlock(&dev->se_port_lock); |
| 1639 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1640 | if (!dest_tpg) { |
| 1641 | pr_err("SPC-3 PR SPEC_I_PT: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1642 | " dest_tpg\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1643 | ret = TCM_INVALID_PARAMETER_LIST; |
| 1644 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1645 | } |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 1646 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1647 | pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1648 | " tid_len: %d for %s + %s\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1649 | dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1650 | tpdl, tid_len, i_str, iport_ptr); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 1651 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1652 | if (tid_len > tpdl) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1653 | pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1654 | " %u for Transport ID: %s\n", tid_len, ptr); |
| 1655 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1656 | core_scsi3_tpg_undepend_item(dest_tpg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1657 | ret = TCM_INVALID_PARAMETER_LIST; |
| 1658 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1659 | } |
| 1660 | /* |
| 1661 | * Locate the desintation struct se_dev_entry pointer for matching |
| 1662 | * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus |
| 1663 | * Target Port. |
| 1664 | */ |
| 1665 | dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, |
| 1666 | dest_rtpi); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1667 | if (!dest_se_deve) { |
Bart Van Assche | e15c745 | 2021-04-15 15:08:24 -0700 | [diff] [blame] | 1668 | pr_err("Unable to locate %s dest_se_deve from destination RTPI: %u\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1669 | dest_tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1670 | dest_rtpi); |
| 1671 | |
| 1672 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1673 | core_scsi3_tpg_undepend_item(dest_tpg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1674 | ret = TCM_INVALID_PARAMETER_LIST; |
| 1675 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1678 | if (core_scsi3_lunacl_depend_item(dest_se_deve)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1679 | pr_err("core_scsi3_lunacl_depend_item()" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1680 | " failed\n"); |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 1681 | kref_put(&dest_se_deve->pr_kref, target_pr_kref_release); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1682 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1683 | core_scsi3_tpg_undepend_item(dest_tpg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1684 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1685 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1686 | } |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 1687 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1688 | pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s" |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1689 | " dest_se_deve mapped_lun: %llu\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1690 | dest_tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1691 | dest_node_acl->initiatorname, dest_se_deve->mapped_lun); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 1692 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1693 | /* |
| 1694 | * Skip any TransportIDs that already have a registration for |
| 1695 | * this target port. |
| 1696 | */ |
| 1697 | pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl, |
| 1698 | iport_ptr); |
| 1699 | if (pr_reg_e) { |
| 1700 | core_scsi3_put_pr_reg(pr_reg_e); |
| 1701 | core_scsi3_lunacl_undepend_item(dest_se_deve); |
| 1702 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1703 | core_scsi3_tpg_undepend_item(dest_tpg); |
| 1704 | ptr += tid_len; |
| 1705 | tpdl -= tid_len; |
| 1706 | tid_len = 0; |
| 1707 | continue; |
| 1708 | } |
| 1709 | /* |
| 1710 | * Allocate a struct pr_transport_id_holder and setup |
| 1711 | * the dest_node_acl and dest_se_deve pointers for the |
| 1712 | * loop below. |
| 1713 | */ |
| 1714 | tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), |
| 1715 | GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1716 | if (!tidh_new) { |
| 1717 | pr_err("Unable to allocate tidh_new\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1718 | core_scsi3_lunacl_undepend_item(dest_se_deve); |
| 1719 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1720 | core_scsi3_tpg_undepend_item(dest_tpg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1721 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1722 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1723 | } |
| 1724 | INIT_LIST_HEAD(&tidh_new->dest_list); |
| 1725 | tidh_new->dest_tpg = dest_tpg; |
| 1726 | tidh_new->dest_node_acl = dest_node_acl; |
| 1727 | tidh_new->dest_se_deve = dest_se_deve; |
| 1728 | |
| 1729 | /* |
| 1730 | * Allocate, but do NOT add the registration for the |
| 1731 | * TransportID referenced SCSI Initiator port. This |
| 1732 | * done because of the following from spc4r17 in section |
| 1733 | * 6.14.3 wrt SPEC_I_PT: |
| 1734 | * |
| 1735 | * "If a registration fails for any initiator port (e.g., if th |
| 1736 | * logical unit does not have enough resources available to |
| 1737 | * hold the registration information), no registrations shall be |
| 1738 | * made, and the command shall be terminated with |
| 1739 | * CHECK CONDITION status." |
| 1740 | * |
| 1741 | * That means we call __core_scsi3_alloc_registration() here, |
| 1742 | * and then call __core_scsi3_add_registration() in the |
| 1743 | * 2nd loop which will never fail. |
| 1744 | */ |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 1745 | dest_lun = dest_se_deve->se_lun; |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1746 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1747 | dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1748 | dest_node_acl, dest_lun, dest_se_deve, |
| 1749 | dest_se_deve->mapped_lun, iport_ptr, |
| 1750 | sa_res_key, all_tg_pt, aptpl); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1751 | if (!dest_pr_reg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1752 | core_scsi3_lunacl_undepend_item(dest_se_deve); |
| 1753 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1754 | core_scsi3_tpg_undepend_item(dest_tpg); |
| 1755 | kfree(tidh_new); |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 1756 | ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1757 | goto out_unmap; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1758 | } |
| 1759 | tidh_new->dest_pr_reg = dest_pr_reg; |
| 1760 | list_add_tail(&tidh_new->dest_list, &tid_dest_list); |
| 1761 | |
| 1762 | ptr += tid_len; |
| 1763 | tpdl -= tid_len; |
| 1764 | tid_len = 0; |
| 1765 | |
| 1766 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 1767 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 1768 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 1769 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1770 | /* |
| 1771 | * Go ahead and create a registrations from tid_dest_list for the |
| 1772 | * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl |
| 1773 | * and dest_se_deve. |
| 1774 | * |
| 1775 | * The SA Reservation Key from the PROUT is set for the |
| 1776 | * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1 |
| 1777 | * means that the TransportID Initiator port will be |
| 1778 | * registered on all of the target ports in the SCSI target device |
| 1779 | * ALL_TG_PT=0 means the registration will only be for the |
| 1780 | * SCSI target port the PROUT REGISTER with SPEC_I_PT=1 |
| 1781 | * was received. |
| 1782 | */ |
| 1783 | list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { |
| 1784 | dest_tpg = tidh->dest_tpg; |
| 1785 | dest_node_acl = tidh->dest_node_acl; |
| 1786 | dest_se_deve = tidh->dest_se_deve; |
| 1787 | dest_pr_reg = tidh->dest_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1788 | |
| 1789 | list_del(&tidh->dest_list); |
| 1790 | kfree(tidh); |
| 1791 | |
| 1792 | memset(i_buf, 0, PR_REG_ISID_ID_LEN); |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 1793 | core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1794 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1795 | __core_scsi3_add_registration(cmd->se_dev, dest_node_acl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1796 | dest_pr_reg, 0, 0); |
| 1797 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1798 | pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1799 | " registered Transport ID for Node: %s%s Mapped LUN:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1800 | " %llu\n", dest_tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1801 | dest_node_acl->initiatorname, i_buf, (dest_se_deve) ? |
| 1802 | dest_se_deve->mapped_lun : 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1803 | |
Mike Christie | f32ba61 | 2020-07-01 20:43:19 -0500 | [diff] [blame] | 1804 | if (dest_pr_reg == local_pr_reg) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1805 | continue; |
Mike Christie | f32ba61 | 2020-07-01 20:43:19 -0500 | [diff] [blame] | 1806 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1807 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1808 | core_scsi3_tpg_undepend_item(dest_tpg); |
| 1809 | } |
| 1810 | |
| 1811 | return 0; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1812 | out_unmap: |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 1813 | transport_kunmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1814 | out: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1815 | /* |
| 1816 | * For the failure case, release everything from tid_dest_list |
| 1817 | * including *dest_pr_reg and the configfs dependances.. |
| 1818 | */ |
| 1819 | list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { |
Mike Christie | f32ba61 | 2020-07-01 20:43:19 -0500 | [diff] [blame] | 1820 | bool is_local = false; |
| 1821 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1822 | dest_tpg = tidh->dest_tpg; |
| 1823 | dest_node_acl = tidh->dest_node_acl; |
| 1824 | dest_se_deve = tidh->dest_se_deve; |
| 1825 | dest_pr_reg = tidh->dest_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1826 | |
Mike Christie | f32ba61 | 2020-07-01 20:43:19 -0500 | [diff] [blame] | 1827 | if (dest_pr_reg == local_pr_reg) |
| 1828 | is_local = true; |
| 1829 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1830 | list_del(&tidh->dest_list); |
| 1831 | kfree(tidh); |
| 1832 | /* |
| 1833 | * Release any extra ALL_TG_PT=1 registrations for |
| 1834 | * the SPEC_I_PT=1 case. |
| 1835 | */ |
| 1836 | list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, |
| 1837 | &dest_pr_reg->pr_reg_atp_list, |
| 1838 | pr_reg_atp_mem_list) { |
| 1839 | list_del(&pr_reg_tmp->pr_reg_atp_mem_list); |
| 1840 | core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); |
| 1841 | kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); |
| 1842 | } |
| 1843 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1844 | kmem_cache_free(t10_pr_reg_cache, dest_pr_reg); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1845 | core_scsi3_lunacl_undepend_item(dest_se_deve); |
Mike Christie | f32ba61 | 2020-07-01 20:43:19 -0500 | [diff] [blame] | 1846 | |
| 1847 | if (is_local) |
| 1848 | continue; |
| 1849 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1850 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 1851 | core_scsi3_tpg_undepend_item(dest_tpg); |
| 1852 | } |
| 1853 | return ret; |
| 1854 | } |
| 1855 | |
Andy Grover | 3c8a622 | 2013-05-16 10:40:58 -0700 | [diff] [blame] | 1856 | static int core_scsi3_update_aptpl_buf( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1857 | struct se_device *dev, |
| 1858 | unsigned char *buf, |
Andy Grover | 0607dec | 2013-05-16 10:40:57 -0700 | [diff] [blame] | 1859 | u32 pr_aptpl_buf_len) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1860 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1861 | struct se_portal_group *tpg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1862 | struct t10_pr_registration *pr_reg; |
| 1863 | unsigned char tmp[512], isid_buf[32]; |
| 1864 | ssize_t len = 0; |
| 1865 | int reg_count = 0; |
Andy Grover | 3c8a622 | 2013-05-16 10:40:58 -0700 | [diff] [blame] | 1866 | int ret = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1867 | |
Andy Grover | 3c8a622 | 2013-05-16 10:40:58 -0700 | [diff] [blame] | 1868 | spin_lock(&dev->dev_reservation_lock); |
| 1869 | spin_lock(&dev->t10_pr.registration_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1870 | /* |
| 1871 | * Walk the registration list.. |
| 1872 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1873 | list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1874 | pr_reg_list) { |
| 1875 | |
| 1876 | tmp[0] = '\0'; |
| 1877 | isid_buf[0] = '\0'; |
| 1878 | tpg = pr_reg->pr_reg_nacl->se_tpg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1879 | /* |
| 1880 | * Write out any ISID value to APTPL metadata that was included |
| 1881 | * in the original registration. |
| 1882 | */ |
| 1883 | if (pr_reg->isid_present_at_reg) |
| 1884 | snprintf(isid_buf, 32, "initiator_sid=%s\n", |
| 1885 | pr_reg->pr_reg_isid); |
| 1886 | /* |
| 1887 | * Include special metadata if the pr_reg matches the |
| 1888 | * reservation holder. |
| 1889 | */ |
| 1890 | if (dev->dev_pr_res_holder == pr_reg) { |
| 1891 | snprintf(tmp, 512, "PR_REG_START: %d" |
| 1892 | "\ninitiator_fabric=%s\n" |
| 1893 | "initiator_node=%s\n%s" |
| 1894 | "sa_res_key=%llu\n" |
| 1895 | "res_holder=1\nres_type=%02x\n" |
| 1896 | "res_scope=%02x\nres_all_tg_pt=%d\n" |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1897 | "mapped_lun=%llu\n", reg_count, |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1898 | tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1899 | pr_reg->pr_reg_nacl->initiatorname, isid_buf, |
| 1900 | pr_reg->pr_res_key, pr_reg->pr_res_type, |
| 1901 | pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt, |
| 1902 | pr_reg->pr_res_mapped_lun); |
| 1903 | } else { |
| 1904 | snprintf(tmp, 512, "PR_REG_START: %d\n" |
| 1905 | "initiator_fabric=%s\ninitiator_node=%s\n%s" |
| 1906 | "sa_res_key=%llu\nres_holder=0\n" |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1907 | "res_all_tg_pt=%d\nmapped_lun=%llu\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1908 | reg_count, tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1909 | pr_reg->pr_reg_nacl->initiatorname, isid_buf, |
| 1910 | pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt, |
| 1911 | pr_reg->pr_res_mapped_lun); |
| 1912 | } |
| 1913 | |
Dan Carpenter | 60d645a | 2011-06-15 10:03:05 -0700 | [diff] [blame] | 1914 | if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 1915 | pr_err("Unable to update renaming APTPL metadata," |
| 1916 | " reallocating larger buffer\n"); |
Andy Grover | 3c8a622 | 2013-05-16 10:40:58 -0700 | [diff] [blame] | 1917 | ret = -EMSGSIZE; |
| 1918 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1919 | } |
| 1920 | len += sprintf(buf+len, "%s", tmp); |
| 1921 | |
| 1922 | /* |
| 1923 | * Include information about the associated SCSI target port. |
| 1924 | */ |
| 1925 | snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n" |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1926 | "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 1927 | " %d\n", tpg->se_tpg_tfo->fabric_name, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1928 | tpg->se_tpg_tfo->tpg_get_wwn(tpg), |
| 1929 | tpg->se_tpg_tfo->tpg_get_tag(tpg), |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 1930 | pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun, |
| 1931 | reg_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1932 | |
Dan Carpenter | 60d645a | 2011-06-15 10:03:05 -0700 | [diff] [blame] | 1933 | if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 1934 | pr_err("Unable to update renaming APTPL metadata," |
| 1935 | " reallocating larger buffer\n"); |
Andy Grover | 3c8a622 | 2013-05-16 10:40:58 -0700 | [diff] [blame] | 1936 | ret = -EMSGSIZE; |
| 1937 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1938 | } |
| 1939 | len += sprintf(buf+len, "%s", tmp); |
| 1940 | reg_count++; |
| 1941 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1942 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1943 | if (!reg_count) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1944 | len += sprintf(buf+len, "No Registrations or Reservations"); |
| 1945 | |
Andy Grover | 3c8a622 | 2013-05-16 10:40:58 -0700 | [diff] [blame] | 1946 | out: |
| 1947 | spin_unlock(&dev->t10_pr.registration_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1948 | spin_unlock(&dev->dev_reservation_lock); |
| 1949 | |
| 1950 | return ret; |
| 1951 | } |
| 1952 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1953 | static int __core_scsi3_write_aptpl_to_file( |
| 1954 | struct se_device *dev, |
Andy Grover | 4dee96f | 2013-05-16 10:41:00 -0700 | [diff] [blame] | 1955 | unsigned char *buf) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1956 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1957 | struct t10_wwn *wwn = &dev->t10_wwn; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1958 | struct file *file; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1959 | int flags = O_RDWR | O_CREAT | O_TRUNC; |
David Disseldorp | bdc79f0 | 2017-10-19 01:39:19 +0200 | [diff] [blame] | 1960 | char *path; |
Andy Grover | 4dee96f | 2013-05-16 10:41:00 -0700 | [diff] [blame] | 1961 | u32 pr_aptpl_buf_len; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1962 | int ret; |
Christoph Hellwig | e13ec93 | 2017-09-01 17:39:14 +0200 | [diff] [blame] | 1963 | loff_t pos = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1964 | |
David Disseldorp | bdc79f0 | 2017-10-19 01:39:19 +0200 | [diff] [blame] | 1965 | path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root, |
| 1966 | &wwn->unit_serial[0]); |
| 1967 | if (!path) |
| 1968 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1969 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1970 | file = filp_open(path, flags, 0600); |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 1971 | if (IS_ERR(file)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1972 | pr_err("filp_open(%s) for APTPL metadata" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1973 | " failed\n", path); |
David Disseldorp | bdc79f0 | 2017-10-19 01:39:19 +0200 | [diff] [blame] | 1974 | kfree(path); |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 1975 | return PTR_ERR(file); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
Andy Grover | 4dee96f | 2013-05-16 10:41:00 -0700 | [diff] [blame] | 1978 | pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1979 | |
Christoph Hellwig | e13ec93 | 2017-09-01 17:39:14 +0200 | [diff] [blame] | 1980 | ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1981 | |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 1982 | if (ret < 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1983 | pr_debug("Error writing APTPL metadata file: %s\n", path); |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 1984 | fput(file); |
David Disseldorp | bdc79f0 | 2017-10-19 01:39:19 +0200 | [diff] [blame] | 1985 | kfree(path); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1986 | |
Gera Kazakov | f730f91 | 2013-09-09 15:47:06 -0700 | [diff] [blame] | 1987 | return (ret < 0) ? -EIO : 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 1990 | /* |
| 1991 | * Clear the APTPL metadata if APTPL has been disabled, otherwise |
| 1992 | * write out the updated metadata to struct file for this SCSI device. |
| 1993 | */ |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 1994 | static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1995 | { |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 1996 | unsigned char *buf; |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 1997 | int rc, len = PR_APTPL_BUF_LEN; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1998 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 1999 | if (!aptpl) { |
| 2000 | char *null_buf = "No Registrations or Reservations\n"; |
| 2001 | |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2002 | rc = __core_scsi3_write_aptpl_to_file(dev, null_buf); |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2003 | dev->t10_pr.pr_aptpl_active = 0; |
| 2004 | pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n"); |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2005 | |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2006 | if (rc) |
| 2007 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2008 | |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2009 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2010 | } |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 2011 | retry: |
| 2012 | buf = vzalloc(len); |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2013 | if (!buf) |
| 2014 | return TCM_OUT_OF_RESOURCES; |
| 2015 | |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 2016 | rc = core_scsi3_update_aptpl_buf(dev, buf, len); |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2017 | if (rc < 0) { |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 2018 | vfree(buf); |
| 2019 | len *= 2; |
| 2020 | goto retry; |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | rc = __core_scsi3_write_aptpl_to_file(dev, buf); |
| 2024 | if (rc != 0) { |
| 2025 | pr_err("SPC-3 PR: Could not update APTPL\n"); |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 2026 | vfree(buf); |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2027 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 2028 | } |
| 2029 | dev->t10_pr.pr_aptpl_active = 1; |
Nicholas Bellinger | f161d4b | 2015-02-11 18:34:40 -0800 | [diff] [blame] | 2030 | vfree(buf); |
Nicholas Bellinger | 8a39185 | 2013-06-24 22:18:40 -0700 | [diff] [blame] | 2031 | pr_debug("SPC-3 PR: Set APTPL Bit Activated\n"); |
| 2032 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2033 | } |
| 2034 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2035 | static sense_reason_t |
| 2036 | core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key, |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2037 | bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2038 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2039 | struct se_session *se_sess = cmd->se_sess; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2040 | struct se_device *dev = cmd->se_dev; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2041 | struct se_lun *se_lun = cmd->se_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2042 | struct se_portal_group *se_tpg; |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2043 | struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2044 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 2045 | unsigned char isid_buf[PR_REG_ISID_LEN] = { }; |
| 2046 | unsigned char *isid_ptr = NULL; |
Hannes Reinecke | a0d50f6 | 2012-12-17 09:53:34 +0100 | [diff] [blame] | 2047 | sense_reason_t ret = TCM_NO_SENSE; |
Nicholas Bellinger | fc09149 | 2014-01-30 13:08:49 -0800 | [diff] [blame] | 2048 | int pr_holder = 0, type; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2049 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2050 | if (!se_sess || !se_lun) { |
| 2051 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2052 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2053 | } |
| 2054 | se_tpg = se_sess->se_tpg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2055 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2056 | if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2057 | se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2058 | PR_REG_ISID_LEN); |
| 2059 | isid_ptr = &isid_buf[0]; |
| 2060 | } |
| 2061 | /* |
| 2062 | * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47 |
| 2063 | */ |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2064 | pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); |
| 2065 | if (!pr_reg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2066 | if (res_key) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2067 | pr_warn("SPC-3 PR: Reservation Key non-zero" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2068 | " for SA REGISTER, returning CONFLICT\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2069 | return TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2070 | } |
| 2071 | /* |
| 2072 | * Do nothing but return GOOD status. |
| 2073 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2074 | if (!sa_res_key) |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 2075 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2076 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2077 | if (!spec_i_pt) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2078 | /* |
| 2079 | * Perform the Service Action REGISTER on the Initiator |
| 2080 | * Port Endpoint that the PRO was received from on the |
| 2081 | * Logical Unit of the SCSI device server. |
| 2082 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2083 | if (core_scsi3_alloc_registration(cmd->se_dev, |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 2084 | se_sess->se_node_acl, cmd->se_lun, |
| 2085 | NULL, cmd->orig_fe_lun, isid_ptr, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2086 | sa_res_key, all_tg_pt, aptpl, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 2087 | register_type, 0)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2088 | pr_err("Unable to allocate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2089 | " struct t10_pr_registration\n"); |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 2090 | return TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2091 | } |
| 2092 | } else { |
| 2093 | /* |
| 2094 | * Register both the Initiator port that received |
| 2095 | * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI |
| 2096 | * TransportID from Parameter list and loop through |
| 2097 | * fabric dependent parameter list while calling |
| 2098 | * logic from of core_scsi3_alloc_registration() for |
| 2099 | * each TransportID provided SCSI Initiator Port/Device |
| 2100 | */ |
| 2101 | ret = core_scsi3_decode_spec_i_port(cmd, se_tpg, |
| 2102 | isid_ptr, sa_res_key, all_tg_pt, aptpl); |
| 2103 | if (ret != 0) |
| 2104 | return ret; |
| 2105 | } |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2106 | return core_scsi3_update_and_write_aptpl(dev, aptpl); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2107 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2108 | |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2109 | /* ok, existing registration */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2110 | |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2111 | if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) { |
| 2112 | pr_err("SPC-3 PR REGISTER: Received" |
| 2113 | " res_key: 0x%016Lx does not match" |
| 2114 | " existing SA REGISTER res_key:" |
| 2115 | " 0x%016Lx\n", res_key, |
| 2116 | pr_reg->pr_res_key); |
| 2117 | ret = TCM_RESERVATION_CONFLICT; |
| 2118 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2119 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2120 | |
| 2121 | if (spec_i_pt) { |
Andy Grover | 1f070cc | 2013-05-16 10:40:56 -0700 | [diff] [blame] | 2122 | pr_err("SPC-3 PR REGISTER: SPEC_I_PT" |
| 2123 | " set on a registered nexus\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2124 | ret = TCM_INVALID_PARAMETER_LIST; |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2125 | goto out; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | /* |
| 2129 | * An existing ALL_TG_PT=1 registration being released |
| 2130 | * must also set ALL_TG_PT=1 in the incoming PROUT. |
| 2131 | */ |
Andy Grover | 1f070cc | 2013-05-16 10:40:56 -0700 | [diff] [blame] | 2132 | if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) { |
| 2133 | pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1" |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2134 | " registration exists, but ALL_TG_PT=1 bit not" |
| 2135 | " present in received PROUT\n"); |
| 2136 | ret = TCM_INVALID_CDB_FIELD; |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2137 | goto out; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /* |
Andy Grover | 51d9c41 | 2013-05-16 10:41:03 -0700 | [diff] [blame] | 2141 | * sa_res_key=1 Change Reservation Key for registered I_T Nexus. |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2142 | */ |
Andy Grover | 51d9c41 | 2013-05-16 10:41:03 -0700 | [diff] [blame] | 2143 | if (sa_res_key) { |
| 2144 | /* |
| 2145 | * Increment PRgeneration counter for struct se_device" |
| 2146 | * upon a successful REGISTER, see spc4r17 section 6.3.2 |
| 2147 | * READ_KEYS service action. |
| 2148 | */ |
| 2149 | pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev); |
| 2150 | pr_reg->pr_res_key = sa_res_key; |
| 2151 | pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation" |
| 2152 | " Key for %s to: 0x%016Lx PRgeneration:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2153 | " 0x%08x\n", cmd->se_tfo->fabric_name, |
Andy Grover | 51d9c41 | 2013-05-16 10:41:03 -0700 | [diff] [blame] | 2154 | (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "", |
| 2155 | pr_reg->pr_reg_nacl->initiatorname, |
| 2156 | pr_reg->pr_res_key, pr_reg->pr_res_generation); |
| 2157 | |
| 2158 | } else { |
| 2159 | /* |
| 2160 | * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus. |
| 2161 | */ |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2162 | type = pr_reg->pr_res_type; |
| 2163 | pr_holder = core_scsi3_check_implicit_release(cmd->se_dev, |
| 2164 | pr_reg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2165 | if (pr_holder < 0) { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2166 | ret = TCM_RESERVATION_CONFLICT; |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2167 | goto out; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | spin_lock(&pr_tmpl->registration_lock); |
| 2171 | /* |
| 2172 | * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port |
| 2173 | * and matching pr_res_key. |
| 2174 | */ |
| 2175 | if (pr_reg->pr_reg_all_tg_pt) { |
| 2176 | list_for_each_entry_safe(pr_reg_p, pr_reg_tmp, |
| 2177 | &pr_tmpl->registration_list, |
| 2178 | pr_reg_list) { |
| 2179 | |
| 2180 | if (!pr_reg_p->pr_reg_all_tg_pt) |
| 2181 | continue; |
| 2182 | if (pr_reg_p->pr_res_key != res_key) |
| 2183 | continue; |
| 2184 | if (pr_reg == pr_reg_p) |
| 2185 | continue; |
| 2186 | if (strcmp(pr_reg->pr_reg_nacl->initiatorname, |
| 2187 | pr_reg_p->pr_reg_nacl->initiatorname)) |
| 2188 | continue; |
| 2189 | |
| 2190 | __core_scsi3_free_registration(dev, |
| 2191 | pr_reg_p, NULL, 0); |
| 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * Release the calling I_T Nexus registration now.. |
| 2197 | */ |
| 2198 | __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1); |
Nicholas Bellinger | fc09149 | 2014-01-30 13:08:49 -0800 | [diff] [blame] | 2199 | pr_reg = NULL; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2200 | |
| 2201 | /* |
| 2202 | * From spc4r17, section 5.7.11.3 Unregistering |
| 2203 | * |
| 2204 | * If the persistent reservation is a registrants only |
| 2205 | * type, the device server shall establish a unit |
| 2206 | * attention condition for the initiator port associated |
| 2207 | * with every registered I_T nexus except for the I_T |
| 2208 | * nexus on which the PERSISTENT RESERVE OUT command was |
| 2209 | * received, with the additional sense code set to |
| 2210 | * RESERVATIONS RELEASED. |
| 2211 | */ |
| 2212 | if (pr_holder && |
Nicholas Bellinger | fc09149 | 2014-01-30 13:08:49 -0800 | [diff] [blame] | 2213 | (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY || |
| 2214 | type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2215 | list_for_each_entry(pr_reg_p, |
| 2216 | &pr_tmpl->registration_list, |
| 2217 | pr_reg_list) { |
| 2218 | |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 2219 | target_ua_allocate_lun( |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2220 | pr_reg_p->pr_reg_nacl, |
| 2221 | pr_reg_p->pr_res_mapped_lun, |
| 2222 | 0x2A, |
| 2223 | ASCQ_2AH_RESERVATIONS_RELEASED); |
| 2224 | } |
| 2225 | } |
Andy Grover | 51d9c41 | 2013-05-16 10:41:03 -0700 | [diff] [blame] | 2226 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2227 | spin_unlock(&pr_tmpl->registration_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2228 | } |
| 2229 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2230 | ret = core_scsi3_update_and_write_aptpl(dev, aptpl); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2231 | |
Andy Grover | bc118fe | 2013-05-16 10:41:04 -0700 | [diff] [blame] | 2232 | out: |
Nicholas Bellinger | fc09149 | 2014-01-30 13:08:49 -0800 | [diff] [blame] | 2233 | if (pr_reg) |
| 2234 | core_scsi3_put_pr_reg(pr_reg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2235 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | unsigned char *core_scsi3_pr_dump_type(int type) |
| 2239 | { |
| 2240 | switch (type) { |
| 2241 | case PR_TYPE_WRITE_EXCLUSIVE: |
| 2242 | return "Write Exclusive Access"; |
| 2243 | case PR_TYPE_EXCLUSIVE_ACCESS: |
| 2244 | return "Exclusive Access"; |
| 2245 | case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: |
| 2246 | return "Write Exclusive Access, Registrants Only"; |
| 2247 | case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: |
| 2248 | return "Exclusive Access, Registrants Only"; |
| 2249 | case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: |
| 2250 | return "Write Exclusive Access, All Registrants"; |
| 2251 | case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: |
| 2252 | return "Exclusive Access, All Registrants"; |
| 2253 | default: |
| 2254 | break; |
| 2255 | } |
| 2256 | |
| 2257 | return "Unknown SPC-3 PR Type"; |
| 2258 | } |
| 2259 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2260 | static sense_reason_t |
| 2261 | core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2262 | { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2263 | struct se_device *dev = cmd->se_dev; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2264 | struct se_session *se_sess = cmd->se_sess; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2265 | struct se_lun *se_lun = cmd->se_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2266 | struct t10_pr_registration *pr_reg, *pr_res_holder; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2267 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 2268 | char i_buf[PR_REG_ISID_ID_LEN] = { }; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2269 | sense_reason_t ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2270 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2271 | if (!se_sess || !se_lun) { |
| 2272 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2273 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2274 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2275 | /* |
| 2276 | * Locate the existing *pr_reg via struct se_node_acl pointers |
| 2277 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2278 | pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2279 | se_sess); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2280 | if (!pr_reg) { |
| 2281 | pr_err("SPC-3 PR: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2282 | " PR_REGISTERED *pr_reg for RESERVE\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2283 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2284 | } |
| 2285 | /* |
| 2286 | * From spc4r17 Section 5.7.9: Reserving: |
| 2287 | * |
| 2288 | * An application client creates a persistent reservation by issuing |
| 2289 | * a PERSISTENT RESERVE OUT command with RESERVE service action through |
| 2290 | * a registered I_T nexus with the following parameters: |
| 2291 | * a) RESERVATION KEY set to the value of the reservation key that is |
| 2292 | * registered with the logical unit for the I_T nexus; and |
| 2293 | */ |
| 2294 | if (res_key != pr_reg->pr_res_key) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2295 | pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2296 | " does not match existing SA REGISTER res_key:" |
| 2297 | " 0x%016Lx\n", res_key, pr_reg->pr_res_key); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2298 | ret = TCM_RESERVATION_CONFLICT; |
| 2299 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2300 | } |
| 2301 | /* |
| 2302 | * From spc4r17 Section 5.7.9: Reserving: |
| 2303 | * |
| 2304 | * From above: |
| 2305 | * b) TYPE field and SCOPE field set to the persistent reservation |
| 2306 | * being created. |
| 2307 | * |
| 2308 | * Only one persistent reservation is allowed at a time per logical unit |
| 2309 | * and that persistent reservation has a scope of LU_SCOPE. |
| 2310 | */ |
| 2311 | if (scope != PR_SCOPE_LU_SCOPE) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2312 | pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2313 | ret = TCM_INVALID_PARAMETER_LIST; |
| 2314 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2315 | } |
| 2316 | /* |
| 2317 | * See if we have an existing PR reservation holder pointer at |
| 2318 | * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration |
| 2319 | * *pr_res_holder. |
| 2320 | */ |
| 2321 | spin_lock(&dev->dev_reservation_lock); |
| 2322 | pr_res_holder = dev->dev_pr_res_holder; |
Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 2323 | if (pr_res_holder) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2324 | /* |
| 2325 | * From spc4r17 Section 5.7.9: Reserving: |
| 2326 | * |
| 2327 | * If the device server receives a PERSISTENT RESERVE OUT |
| 2328 | * command from an I_T nexus other than a persistent reservation |
| 2329 | * holder (see 5.7.10) that attempts to create a persistent |
| 2330 | * reservation when a persistent reservation already exists for |
| 2331 | * the logical unit, then the command shall be completed with |
| 2332 | * RESERVATION CONFLICT status. |
| 2333 | */ |
Ilias Tsitsimpis | e673dc9 | 2015-03-26 17:12:42 +0200 | [diff] [blame] | 2334 | if (!is_reservation_holder(pr_res_holder, pr_reg)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2335 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2336 | pr_err("SPC-3 PR: Attempted RESERVE from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2337 | " [%s]: %s while reservation already held by" |
| 2338 | " [%s]: %s, returning RESERVATION_CONFLICT\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2339 | cmd->se_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2340 | se_sess->se_node_acl->initiatorname, |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2341 | pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2342 | pr_res_holder->pr_reg_nacl->initiatorname); |
| 2343 | |
| 2344 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2345 | ret = TCM_RESERVATION_CONFLICT; |
| 2346 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2347 | } |
| 2348 | /* |
| 2349 | * From spc4r17 Section 5.7.9: Reserving: |
| 2350 | * |
| 2351 | * If a persistent reservation holder attempts to modify the |
| 2352 | * type or scope of an existing persistent reservation, the |
| 2353 | * command shall be completed with RESERVATION CONFLICT status. |
| 2354 | */ |
| 2355 | if ((pr_res_holder->pr_res_type != type) || |
| 2356 | (pr_res_holder->pr_res_scope != scope)) { |
| 2357 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2358 | pr_err("SPC-3 PR: Attempted RESERVE from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2359 | " [%s]: %s trying to change TYPE and/or SCOPE," |
| 2360 | " while reservation already held by [%s]: %s," |
| 2361 | " returning RESERVATION_CONFLICT\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2362 | cmd->se_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2363 | se_sess->se_node_acl->initiatorname, |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2364 | pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2365 | pr_res_holder->pr_reg_nacl->initiatorname); |
| 2366 | |
| 2367 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2368 | ret = TCM_RESERVATION_CONFLICT; |
| 2369 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2370 | } |
| 2371 | /* |
| 2372 | * From spc4r17 Section 5.7.9: Reserving: |
| 2373 | * |
| 2374 | * If the device server receives a PERSISTENT RESERVE OUT |
| 2375 | * command with RESERVE service action where the TYPE field and |
| 2376 | * the SCOPE field contain the same values as the existing type |
| 2377 | * and scope from a persistent reservation holder, it shall not |
| 2378 | * make any change to the existing persistent reservation and |
| 2379 | * shall completethe command with GOOD status. |
| 2380 | */ |
| 2381 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2382 | ret = 0; |
| 2383 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2384 | } |
| 2385 | /* |
| 2386 | * Otherwise, our *pr_reg becomes the PR reservation holder for said |
| 2387 | * TYPE/SCOPE. Also set the received scope and type in *pr_reg. |
| 2388 | */ |
| 2389 | pr_reg->pr_res_scope = scope; |
| 2390 | pr_reg->pr_res_type = type; |
| 2391 | pr_reg->pr_res_holder = 1; |
| 2392 | dev->dev_pr_res_holder = pr_reg; |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 2393 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2394 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2395 | pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2396 | " reservation holder TYPE: %s ALL_TG_PT: %d\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2397 | cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2398 | (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2399 | pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2400 | cmd->se_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2401 | se_sess->se_node_acl->initiatorname, |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 2402 | i_buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2403 | spin_unlock(&dev->dev_reservation_lock); |
| 2404 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2405 | if (pr_tmpl->pr_aptpl_active) |
| 2406 | core_scsi3_update_and_write_aptpl(cmd->se_dev, true); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2407 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2408 | ret = 0; |
| 2409 | out_put_pr_reg: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2410 | core_scsi3_put_pr_reg(pr_reg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2411 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2414 | static sense_reason_t |
| 2415 | core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope, |
| 2416 | u64 res_key) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2417 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2418 | switch (type) { |
| 2419 | case PR_TYPE_WRITE_EXCLUSIVE: |
| 2420 | case PR_TYPE_EXCLUSIVE_ACCESS: |
| 2421 | case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: |
| 2422 | case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: |
| 2423 | case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: |
| 2424 | case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2425 | return core_scsi3_pro_reserve(cmd, type, scope, res_key); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2426 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2427 | pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2428 | " 0x%02x\n", type); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2429 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2430 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2431 | } |
| 2432 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2433 | static void __core_scsi3_complete_pro_release( |
| 2434 | struct se_device *dev, |
| 2435 | struct se_node_acl *se_nacl, |
| 2436 | struct t10_pr_registration *pr_reg, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2437 | int explicit, |
| 2438 | int unreg) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2439 | { |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 2440 | const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 2441 | char i_buf[PR_REG_ISID_ID_LEN] = { }; |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2442 | int pr_res_type = 0, pr_res_scope = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2443 | |
Bart Van Assche | 618baaf | 2019-01-25 10:34:53 -0800 | [diff] [blame] | 2444 | lockdep_assert_held(&dev->dev_reservation_lock); |
| 2445 | |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 2446 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2447 | /* |
| 2448 | * Go ahead and release the current PR reservation holder. |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2449 | * If an All Registrants reservation is currently active and |
| 2450 | * a unregister operation is requested, replace the current |
| 2451 | * dev_pr_res_holder with another active registration. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2452 | */ |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2453 | if (dev->dev_pr_res_holder) { |
| 2454 | pr_res_type = dev->dev_pr_res_holder->pr_res_type; |
| 2455 | pr_res_scope = dev->dev_pr_res_holder->pr_res_scope; |
| 2456 | dev->dev_pr_res_holder->pr_res_type = 0; |
| 2457 | dev->dev_pr_res_holder->pr_res_scope = 0; |
| 2458 | dev->dev_pr_res_holder->pr_res_holder = 0; |
| 2459 | dev->dev_pr_res_holder = NULL; |
| 2460 | } |
| 2461 | if (!unreg) |
| 2462 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2463 | |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2464 | spin_lock(&dev->t10_pr.registration_lock); |
| 2465 | list_del_init(&pr_reg->pr_reg_list); |
| 2466 | /* |
| 2467 | * If the I_T nexus is a reservation holder, the persistent reservation |
| 2468 | * is of an all registrants type, and the I_T nexus is the last remaining |
| 2469 | * registered I_T nexus, then the device server shall also release the |
| 2470 | * persistent reservation. |
| 2471 | */ |
| 2472 | if (!list_empty(&dev->t10_pr.registration_list) && |
| 2473 | ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || |
| 2474 | (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) { |
| 2475 | dev->dev_pr_res_holder = |
| 2476 | list_entry(dev->t10_pr.registration_list.next, |
| 2477 | struct t10_pr_registration, pr_reg_list); |
| 2478 | dev->dev_pr_res_holder->pr_res_type = pr_res_type; |
| 2479 | dev->dev_pr_res_holder->pr_res_scope = pr_res_scope; |
| 2480 | dev->dev_pr_res_holder->pr_res_holder = 1; |
| 2481 | } |
| 2482 | spin_unlock(&dev->t10_pr.registration_lock); |
| 2483 | out: |
| 2484 | if (!dev->dev_pr_res_holder) { |
| 2485 | pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared" |
| 2486 | " reservation holder TYPE: %s ALL_TG_PT: %d\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2487 | tfo->fabric_name, (explicit) ? "explicit" : |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2488 | "implicit", core_scsi3_pr_dump_type(pr_res_type), |
| 2489 | (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); |
| 2490 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2491 | pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2492 | tfo->fabric_name, se_nacl->initiatorname, |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 2493 | i_buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2494 | /* |
| 2495 | * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE |
| 2496 | */ |
| 2497 | pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0; |
| 2498 | } |
| 2499 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2500 | static sense_reason_t |
| 2501 | core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope, |
| 2502 | u64 res_key) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2503 | { |
| 2504 | struct se_device *dev = cmd->se_dev; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2505 | struct se_session *se_sess = cmd->se_sess; |
| 2506 | struct se_lun *se_lun = cmd->se_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2507 | struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2508 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2509 | sense_reason_t ret = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2510 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2511 | if (!se_sess || !se_lun) { |
| 2512 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2513 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2514 | } |
| 2515 | /* |
| 2516 | * Locate the existing *pr_reg via struct se_node_acl pointers |
| 2517 | */ |
| 2518 | pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2519 | if (!pr_reg) { |
| 2520 | pr_err("SPC-3 PR: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2521 | " PR_REGISTERED *pr_reg for RELEASE\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2522 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2523 | } |
| 2524 | /* |
| 2525 | * From spc4r17 Section 5.7.11.2 Releasing: |
| 2526 | * |
| 2527 | * If there is no persistent reservation or in response to a persistent |
| 2528 | * reservation release request from a registered I_T nexus that is not a |
| 2529 | * persistent reservation holder (see 5.7.10), the device server shall |
| 2530 | * do the following: |
| 2531 | * |
| 2532 | * a) Not release the persistent reservation, if any; |
| 2533 | * b) Not remove any registrations; and |
| 2534 | * c) Complete the command with GOOD status. |
| 2535 | */ |
| 2536 | spin_lock(&dev->dev_reservation_lock); |
| 2537 | pr_res_holder = dev->dev_pr_res_holder; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2538 | if (!pr_res_holder) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2539 | /* |
| 2540 | * No persistent reservation, return GOOD status. |
| 2541 | */ |
| 2542 | spin_unlock(&dev->dev_reservation_lock); |
Nicholas Bellinger | bb7a8c8 | 2012-11-06 15:46:25 -0800 | [diff] [blame] | 2543 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2544 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2545 | |
Ilias Tsitsimpis | e673dc9 | 2015-03-26 17:12:42 +0200 | [diff] [blame] | 2546 | if (!is_reservation_holder(pr_res_holder, pr_reg)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2547 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2548 | * Release request from a registered I_T nexus that is not a |
| 2549 | * persistent reservation holder. return GOOD status. |
| 2550 | */ |
| 2551 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2552 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2553 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2554 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2555 | /* |
| 2556 | * From spc4r17 Section 5.7.11.2 Releasing: |
| 2557 | * |
| 2558 | * Only the persistent reservation holder (see 5.7.10) is allowed to |
| 2559 | * release a persistent reservation. |
| 2560 | * |
| 2561 | * An application client releases the persistent reservation by issuing |
| 2562 | * a PERSISTENT RESERVE OUT command with RELEASE service action through |
| 2563 | * an I_T nexus that is a persistent reservation holder with the |
| 2564 | * following parameters: |
| 2565 | * |
| 2566 | * a) RESERVATION KEY field set to the value of the reservation key |
| 2567 | * that is registered with the logical unit for the I_T nexus; |
| 2568 | */ |
| 2569 | if (res_key != pr_reg->pr_res_key) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2570 | pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2571 | " does not match existing SA REGISTER res_key:" |
| 2572 | " 0x%016Lx\n", res_key, pr_reg->pr_res_key); |
| 2573 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2574 | ret = TCM_RESERVATION_CONFLICT; |
| 2575 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2576 | } |
| 2577 | /* |
| 2578 | * From spc4r17 Section 5.7.11.2 Releasing and above: |
| 2579 | * |
| 2580 | * b) TYPE field and SCOPE field set to match the persistent |
| 2581 | * reservation being released. |
| 2582 | */ |
| 2583 | if ((pr_res_holder->pr_res_type != type) || |
| 2584 | (pr_res_holder->pr_res_scope != scope)) { |
| 2585 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2586 | pr_err("SPC-3 PR RELEASE: Attempted to release" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2587 | " reservation from [%s]: %s with different TYPE " |
| 2588 | "and/or SCOPE while reservation already held by" |
| 2589 | " [%s]: %s, returning RESERVATION_CONFLICT\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2590 | cmd->se_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2591 | se_sess->se_node_acl->initiatorname, |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2592 | pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2593 | pr_res_holder->pr_reg_nacl->initiatorname); |
| 2594 | |
| 2595 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2596 | ret = TCM_RESERVATION_CONFLICT; |
| 2597 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2598 | } |
| 2599 | /* |
| 2600 | * In response to a persistent reservation release request from the |
| 2601 | * persistent reservation holder the device server shall perform a |
| 2602 | * release by doing the following as an uninterrupted series of actions: |
| 2603 | * a) Release the persistent reservation; |
| 2604 | * b) Not remove any registration(s); |
| 2605 | * c) If the released persistent reservation is a registrants only type |
| 2606 | * or all registrants type persistent reservation, |
| 2607 | * the device server shall establish a unit attention condition for |
| 2608 | * the initiator port associated with every regis- |
| 2609 | * tered I_T nexus other than I_T nexus on which the PERSISTENT |
| 2610 | * RESERVE OUT command with RELEASE service action was received, |
| 2611 | * with the additional sense code set to RESERVATIONS RELEASED; and |
| 2612 | * d) If the persistent reservation is of any other type, the device |
| 2613 | * server shall not establish a unit attention condition. |
| 2614 | */ |
| 2615 | __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2616 | pr_reg, 1, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2617 | |
| 2618 | spin_unlock(&dev->dev_reservation_lock); |
| 2619 | |
| 2620 | if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) && |
| 2621 | (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) && |
| 2622 | (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) && |
| 2623 | (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { |
| 2624 | /* |
| 2625 | * If no UNIT ATTENTION conditions will be established for |
| 2626 | * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS |
| 2627 | * go ahead and check for APTPL=1 update+write below |
| 2628 | */ |
| 2629 | goto write_aptpl; |
| 2630 | } |
| 2631 | |
| 2632 | spin_lock(&pr_tmpl->registration_lock); |
| 2633 | list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list, |
| 2634 | pr_reg_list) { |
| 2635 | /* |
| 2636 | * Do not establish a UNIT ATTENTION condition |
| 2637 | * for the calling I_T Nexus |
| 2638 | */ |
| 2639 | if (pr_reg_p == pr_reg) |
| 2640 | continue; |
| 2641 | |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 2642 | target_ua_allocate_lun(pr_reg_p->pr_reg_nacl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2643 | pr_reg_p->pr_res_mapped_lun, |
| 2644 | 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED); |
| 2645 | } |
| 2646 | spin_unlock(&pr_tmpl->registration_lock); |
| 2647 | |
| 2648 | write_aptpl: |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2649 | if (pr_tmpl->pr_aptpl_active) |
| 2650 | core_scsi3_update_and_write_aptpl(cmd->se_dev, true); |
| 2651 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2652 | out_put_pr_reg: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2653 | core_scsi3_put_pr_reg(pr_reg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2654 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2655 | } |
| 2656 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2657 | static sense_reason_t |
| 2658 | core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2659 | { |
| 2660 | struct se_device *dev = cmd->se_dev; |
| 2661 | struct se_node_acl *pr_reg_nacl; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2662 | struct se_session *se_sess = cmd->se_sess; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2663 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2664 | struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 2665 | u64 pr_res_mapped_lun = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2666 | int calling_it_nexus = 0; |
| 2667 | /* |
| 2668 | * Locate the existing *pr_reg via struct se_node_acl pointers |
| 2669 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2670 | pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2671 | se_sess->se_node_acl, se_sess); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2672 | if (!pr_reg_n) { |
| 2673 | pr_err("SPC-3 PR: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2674 | " PR_REGISTERED *pr_reg for CLEAR\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2675 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2676 | } |
| 2677 | /* |
| 2678 | * From spc4r17 section 5.7.11.6, Clearing: |
| 2679 | * |
| 2680 | * Any application client may release the persistent reservation and |
| 2681 | * remove all registrations from a device server by issuing a |
| 2682 | * PERSISTENT RESERVE OUT command with CLEAR service action through a |
| 2683 | * registered I_T nexus with the following parameter: |
| 2684 | * |
| 2685 | * a) RESERVATION KEY field set to the value of the reservation key |
| 2686 | * that is registered with the logical unit for the I_T nexus. |
| 2687 | */ |
| 2688 | if (res_key != pr_reg_n->pr_res_key) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2689 | pr_err("SPC-3 PR REGISTER: Received" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2690 | " res_key: 0x%016Lx does not match" |
| 2691 | " existing SA REGISTER res_key:" |
| 2692 | " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key); |
| 2693 | core_scsi3_put_pr_reg(pr_reg_n); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2694 | return TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2695 | } |
| 2696 | /* |
| 2697 | * a) Release the persistent reservation, if any; |
| 2698 | */ |
| 2699 | spin_lock(&dev->dev_reservation_lock); |
| 2700 | pr_res_holder = dev->dev_pr_res_holder; |
| 2701 | if (pr_res_holder) { |
| 2702 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; |
| 2703 | __core_scsi3_complete_pro_release(dev, pr_res_nacl, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2704 | pr_res_holder, 0, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2705 | } |
| 2706 | spin_unlock(&dev->dev_reservation_lock); |
| 2707 | /* |
| 2708 | * b) Remove all registration(s) (see spc4r17 5.7.7); |
| 2709 | */ |
| 2710 | spin_lock(&pr_tmpl->registration_lock); |
| 2711 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 2712 | &pr_tmpl->registration_list, pr_reg_list) { |
| 2713 | |
| 2714 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; |
| 2715 | pr_reg_nacl = pr_reg->pr_reg_nacl; |
| 2716 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; |
| 2717 | __core_scsi3_free_registration(dev, pr_reg, NULL, |
| 2718 | calling_it_nexus); |
| 2719 | /* |
| 2720 | * e) Establish a unit attention condition for the initiator |
| 2721 | * port associated with every registered I_T nexus other |
| 2722 | * than the I_T nexus on which the PERSISTENT RESERVE OUT |
| 2723 | * command with CLEAR service action was received, with the |
| 2724 | * additional sense code set to RESERVATIONS PREEMPTED. |
| 2725 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2726 | if (!calling_it_nexus) |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 2727 | target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2728 | 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED); |
| 2729 | } |
| 2730 | spin_unlock(&pr_tmpl->registration_lock); |
| 2731 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2732 | pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2733 | cmd->se_tfo->fabric_name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2734 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2735 | core_scsi3_update_and_write_aptpl(cmd->se_dev, false); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2736 | |
| 2737 | core_scsi3_pr_generation(dev); |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2741 | static void __core_scsi3_complete_pro_preempt( |
| 2742 | struct se_device *dev, |
| 2743 | struct t10_pr_registration *pr_reg, |
| 2744 | struct list_head *preempt_and_abort_list, |
| 2745 | int type, |
| 2746 | int scope, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 2747 | enum preempt_type preempt_type) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2748 | { |
| 2749 | struct se_node_acl *nacl = pr_reg->pr_reg_nacl; |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 2750 | const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 2751 | char i_buf[PR_REG_ISID_ID_LEN] = { }; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2752 | |
Bart Van Assche | 618baaf | 2019-01-25 10:34:53 -0800 | [diff] [blame] | 2753 | lockdep_assert_held(&dev->dev_reservation_lock); |
| 2754 | |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 2755 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2756 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2757 | * Do an implicit RELEASE of the existing reservation. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2758 | */ |
| 2759 | if (dev->dev_pr_res_holder) |
| 2760 | __core_scsi3_complete_pro_release(dev, nacl, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 2761 | dev->dev_pr_res_holder, 0, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2762 | |
| 2763 | dev->dev_pr_res_holder = pr_reg; |
| 2764 | pr_reg->pr_res_holder = 1; |
| 2765 | pr_reg->pr_res_type = type; |
| 2766 | pr_reg->pr_res_scope = scope; |
| 2767 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2768 | pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2769 | " reservation holder TYPE: %s ALL_TG_PT: %d\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2770 | tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2771 | core_scsi3_pr_dump_type(type), |
| 2772 | (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2773 | pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 2774 | tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 2775 | nacl->initiatorname, i_buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2776 | /* |
| 2777 | * For PREEMPT_AND_ABORT, add the preempting reservation's |
| 2778 | * struct t10_pr_registration to the list that will be compared |
| 2779 | * against received CDBs.. |
| 2780 | */ |
| 2781 | if (preempt_and_abort_list) |
| 2782 | list_add_tail(&pr_reg->pr_reg_abort_list, |
| 2783 | preempt_and_abort_list); |
| 2784 | } |
| 2785 | |
| 2786 | static void core_scsi3_release_preempt_and_abort( |
| 2787 | struct list_head *preempt_and_abort_list, |
| 2788 | struct t10_pr_registration *pr_reg_holder) |
| 2789 | { |
| 2790 | struct t10_pr_registration *pr_reg, *pr_reg_tmp; |
| 2791 | |
| 2792 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list, |
| 2793 | pr_reg_abort_list) { |
| 2794 | |
| 2795 | list_del(&pr_reg->pr_reg_abort_list); |
| 2796 | if (pr_reg_holder == pr_reg) |
| 2797 | continue; |
| 2798 | if (pr_reg->pr_res_holder) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2799 | pr_warn("pr_reg->pr_res_holder still set\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2800 | continue; |
| 2801 | } |
| 2802 | |
| 2803 | pr_reg->pr_reg_deve = NULL; |
| 2804 | pr_reg->pr_reg_nacl = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2805 | kmem_cache_free(t10_pr_reg_cache, pr_reg); |
| 2806 | } |
| 2807 | } |
| 2808 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2809 | static sense_reason_t |
| 2810 | core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 2811 | u64 sa_res_key, enum preempt_type preempt_type) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2812 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2813 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2814 | struct se_node_acl *pr_reg_nacl; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2815 | struct se_session *se_sess = cmd->se_sess; |
Roland Dreier | d0f474e | 2012-01-12 10:41:18 -0800 | [diff] [blame] | 2816 | LIST_HEAD(preempt_and_abort_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2817 | struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2818 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 2819 | u64 pr_res_mapped_lun = 0; |
Steven Allen | b6932ee | 2014-10-24 15:18:26 -0700 | [diff] [blame] | 2820 | int all_reg = 0, calling_it_nexus = 0; |
| 2821 | bool sa_res_key_unmatched = sa_res_key != 0; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2822 | int prh_type = 0, prh_scope = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2823 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2824 | if (!se_sess) |
| 2825 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2826 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2827 | pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2828 | se_sess); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2829 | if (!pr_reg_n) { |
| 2830 | pr_err("SPC-3 PR: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2831 | " PR_REGISTERED *pr_reg for PREEMPT%s\n", |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 2832 | (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : ""); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2833 | return TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2834 | } |
| 2835 | if (pr_reg_n->pr_res_key != res_key) { |
| 2836 | core_scsi3_put_pr_reg(pr_reg_n); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2837 | return TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2838 | } |
| 2839 | if (scope != PR_SCOPE_LU_SCOPE) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2840 | pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2841 | core_scsi3_put_pr_reg(pr_reg_n); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2842 | return TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2843 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2844 | |
| 2845 | spin_lock(&dev->dev_reservation_lock); |
| 2846 | pr_res_holder = dev->dev_pr_res_holder; |
| 2847 | if (pr_res_holder && |
| 2848 | ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || |
| 2849 | (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) |
| 2850 | all_reg = 1; |
| 2851 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2852 | if (!all_reg && !sa_res_key) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2853 | spin_unlock(&dev->dev_reservation_lock); |
| 2854 | core_scsi3_put_pr_reg(pr_reg_n); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2855 | return TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2856 | } |
| 2857 | /* |
| 2858 | * From spc4r17, section 5.7.11.4.4 Removing Registrations: |
| 2859 | * |
| 2860 | * If the SERVICE ACTION RESERVATION KEY field does not identify a |
| 2861 | * persistent reservation holder or there is no persistent reservation |
| 2862 | * holder (i.e., there is no persistent reservation), then the device |
| 2863 | * server shall perform a preempt by doing the following in an |
| 2864 | * uninterrupted series of actions. (See below..) |
| 2865 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2866 | if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2867 | /* |
| 2868 | * No existing or SA Reservation Key matching reservations.. |
| 2869 | * |
| 2870 | * PROUT SA PREEMPT with All Registrant type reservations are |
| 2871 | * allowed to be processed without a matching SA Reservation Key |
| 2872 | */ |
| 2873 | spin_lock(&pr_tmpl->registration_lock); |
| 2874 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 2875 | &pr_tmpl->registration_list, pr_reg_list) { |
| 2876 | /* |
| 2877 | * Removing of registrations in non all registrants |
| 2878 | * type reservations without a matching SA reservation |
| 2879 | * key. |
| 2880 | * |
| 2881 | * a) Remove the registrations for all I_T nexuses |
| 2882 | * specified by the SERVICE ACTION RESERVATION KEY |
| 2883 | * field; |
| 2884 | * b) Ignore the contents of the SCOPE and TYPE fields; |
| 2885 | * c) Process tasks as defined in 5.7.1; and |
| 2886 | * d) Establish a unit attention condition for the |
| 2887 | * initiator port associated with every I_T nexus |
| 2888 | * that lost its registration other than the I_T |
| 2889 | * nexus on which the PERSISTENT RESERVE OUT command |
| 2890 | * was received, with the additional sense code set |
| 2891 | * to REGISTRATIONS PREEMPTED. |
| 2892 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2893 | if (!all_reg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2894 | if (pr_reg->pr_res_key != sa_res_key) |
| 2895 | continue; |
Steven Allen | b6932ee | 2014-10-24 15:18:26 -0700 | [diff] [blame] | 2896 | sa_res_key_unmatched = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2897 | |
| 2898 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; |
| 2899 | pr_reg_nacl = pr_reg->pr_reg_nacl; |
| 2900 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; |
| 2901 | __core_scsi3_free_registration(dev, pr_reg, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 2902 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2903 | NULL, calling_it_nexus); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2904 | } else { |
| 2905 | /* |
| 2906 | * Case for any existing all registrants type |
| 2907 | * reservation, follow logic in spc4r17 section |
| 2908 | * 5.7.11.4 Preempting, Table 52 and Figure 7. |
| 2909 | * |
| 2910 | * For a ZERO SA Reservation key, release |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2911 | * all other registrations and do an implicit |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2912 | * release of active persistent reservation. |
| 2913 | * |
| 2914 | * For a non-ZERO SA Reservation key, only |
| 2915 | * release the matching reservation key from |
| 2916 | * registrations. |
| 2917 | */ |
| 2918 | if ((sa_res_key) && |
| 2919 | (pr_reg->pr_res_key != sa_res_key)) |
| 2920 | continue; |
Steven Allen | b6932ee | 2014-10-24 15:18:26 -0700 | [diff] [blame] | 2921 | sa_res_key_unmatched = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2922 | |
| 2923 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; |
| 2924 | if (calling_it_nexus) |
| 2925 | continue; |
| 2926 | |
| 2927 | pr_reg_nacl = pr_reg->pr_reg_nacl; |
| 2928 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; |
| 2929 | __core_scsi3_free_registration(dev, pr_reg, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 2930 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2931 | NULL, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2932 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2933 | if (!calling_it_nexus) |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 2934 | target_ua_allocate_lun(pr_reg_nacl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2935 | pr_res_mapped_lun, 0x2A, |
Marco Sanvido | 9e08e34 | 2012-01-03 17:12:57 -0800 | [diff] [blame] | 2936 | ASCQ_2AH_REGISTRATIONS_PREEMPTED); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2937 | } |
| 2938 | spin_unlock(&pr_tmpl->registration_lock); |
| 2939 | /* |
| 2940 | * If a PERSISTENT RESERVE OUT with a PREEMPT service action or |
| 2941 | * a PREEMPT AND ABORT service action sets the SERVICE ACTION |
| 2942 | * RESERVATION KEY field to a value that does not match any |
| 2943 | * registered reservation key, then the device server shall |
| 2944 | * complete the command with RESERVATION CONFLICT status. |
| 2945 | */ |
Steven Allen | b6932ee | 2014-10-24 15:18:26 -0700 | [diff] [blame] | 2946 | if (sa_res_key_unmatched) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2947 | spin_unlock(&dev->dev_reservation_lock); |
| 2948 | core_scsi3_put_pr_reg(pr_reg_n); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2949 | return TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2950 | } |
| 2951 | /* |
| 2952 | * For an existing all registrants type reservation |
| 2953 | * with a zero SA rservation key, preempt the existing |
| 2954 | * reservation with the new PR type and scope. |
| 2955 | */ |
| 2956 | if (pr_res_holder && all_reg && !(sa_res_key)) { |
| 2957 | __core_scsi3_complete_pro_preempt(dev, pr_reg_n, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 2958 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, |
| 2959 | type, scope, preempt_type); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2960 | } |
Dmitry Bogdanov | f050a7c | 2022-09-09 12:04:23 +0300 | [diff] [blame] | 2961 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2962 | spin_unlock(&dev->dev_reservation_lock); |
| 2963 | |
Dmitry Bogdanov | 49790e6 | 2022-09-09 12:04:24 +0300 | [diff] [blame] | 2964 | /* |
| 2965 | * SPC-4 5.12.11.2.6 Preempting and aborting |
| 2966 | * The actions described in this subclause shall be performed |
| 2967 | * for all I_T nexuses that are registered with the non-zero |
| 2968 | * SERVICE ACTION RESERVATION KEY value, without regard for |
| 2969 | * whether the preempted I_T nexuses hold the persistent |
| 2970 | * reservation. If the SERVICE ACTION RESERVATION KEY field is |
| 2971 | * set to zero and an all registrants persistent reservation is |
| 2972 | * present, the device server shall abort all commands for all |
| 2973 | * registered I_T nexuses. |
| 2974 | */ |
| 2975 | if (preempt_type == PREEMPT_AND_ABORT) { |
| 2976 | core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, |
| 2977 | cmd); |
Dmitry Bogdanov | f050a7c | 2022-09-09 12:04:23 +0300 | [diff] [blame] | 2978 | core_scsi3_release_preempt_and_abort( |
| 2979 | &preempt_and_abort_list, pr_reg_n); |
Dmitry Bogdanov | 49790e6 | 2022-09-09 12:04:24 +0300 | [diff] [blame] | 2980 | } |
Dmitry Bogdanov | f050a7c | 2022-09-09 12:04:23 +0300 | [diff] [blame] | 2981 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 2982 | if (pr_tmpl->pr_aptpl_active) |
| 2983 | core_scsi3_update_and_write_aptpl(cmd->se_dev, true); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2984 | |
| 2985 | core_scsi3_put_pr_reg(pr_reg_n); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2986 | core_scsi3_pr_generation(cmd->se_dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2987 | return 0; |
| 2988 | } |
| 2989 | /* |
| 2990 | * The PREEMPTing SA reservation key matches that of the |
| 2991 | * existing persistent reservation, first, we check if |
| 2992 | * we are preempting our own reservation. |
| 2993 | * From spc4r17, section 5.7.11.4.3 Preempting |
| 2994 | * persistent reservations and registration handling |
| 2995 | * |
| 2996 | * If an all registrants persistent reservation is not |
| 2997 | * present, it is not an error for the persistent |
| 2998 | * reservation holder to preempt itself (i.e., a |
| 2999 | * PERSISTENT RESERVE OUT with a PREEMPT service action |
| 3000 | * or a PREEMPT AND ABORT service action with the |
| 3001 | * SERVICE ACTION RESERVATION KEY value equal to the |
| 3002 | * persistent reservation holder's reservation key that |
| 3003 | * is received from the persistent reservation holder). |
| 3004 | * In that case, the device server shall establish the |
| 3005 | * new persistent reservation and maintain the |
| 3006 | * registration. |
| 3007 | */ |
| 3008 | prh_type = pr_res_holder->pr_res_type; |
| 3009 | prh_scope = pr_res_holder->pr_res_scope; |
| 3010 | /* |
| 3011 | * If the SERVICE ACTION RESERVATION KEY field identifies a |
| 3012 | * persistent reservation holder (see 5.7.10), the device |
| 3013 | * server shall perform a preempt by doing the following as |
| 3014 | * an uninterrupted series of actions: |
| 3015 | * |
| 3016 | * a) Release the persistent reservation for the holder |
| 3017 | * identified by the SERVICE ACTION RESERVATION KEY field; |
| 3018 | */ |
| 3019 | if (pr_reg_n != pr_res_holder) |
| 3020 | __core_scsi3_complete_pro_release(dev, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 3021 | pr_res_holder->pr_reg_nacl, |
| 3022 | dev->dev_pr_res_holder, 0, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3023 | /* |
| 3024 | * b) Remove the registrations for all I_T nexuses identified |
| 3025 | * by the SERVICE ACTION RESERVATION KEY field, except the |
| 3026 | * I_T nexus that is being used for the PERSISTENT RESERVE |
| 3027 | * OUT command. If an all registrants persistent reservation |
| 3028 | * is present and the SERVICE ACTION RESERVATION KEY field |
| 3029 | * is set to zero, then all registrations shall be removed |
| 3030 | * except for that of the I_T nexus that is being used for |
| 3031 | * the PERSISTENT RESERVE OUT command; |
| 3032 | */ |
| 3033 | spin_lock(&pr_tmpl->registration_lock); |
| 3034 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 3035 | &pr_tmpl->registration_list, pr_reg_list) { |
| 3036 | |
| 3037 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; |
| 3038 | if (calling_it_nexus) |
| 3039 | continue; |
| 3040 | |
Dmitry Bogdanov | 1f1009e | 2022-09-09 12:04:22 +0300 | [diff] [blame] | 3041 | if (sa_res_key && pr_reg->pr_res_key != sa_res_key) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3042 | continue; |
| 3043 | |
| 3044 | pr_reg_nacl = pr_reg->pr_reg_nacl; |
| 3045 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; |
| 3046 | __core_scsi3_free_registration(dev, pr_reg, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3047 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3048 | calling_it_nexus); |
| 3049 | /* |
| 3050 | * e) Establish a unit attention condition for the initiator |
| 3051 | * port associated with every I_T nexus that lost its |
| 3052 | * persistent reservation and/or registration, with the |
| 3053 | * additional sense code set to REGISTRATIONS PREEMPTED; |
| 3054 | */ |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 3055 | target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A, |
Marco Sanvido | 9e08e34 | 2012-01-03 17:12:57 -0800 | [diff] [blame] | 3056 | ASCQ_2AH_REGISTRATIONS_PREEMPTED); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3057 | } |
| 3058 | spin_unlock(&pr_tmpl->registration_lock); |
| 3059 | /* |
| 3060 | * c) Establish a persistent reservation for the preempting |
| 3061 | * I_T nexus using the contents of the SCOPE and TYPE fields; |
| 3062 | */ |
| 3063 | __core_scsi3_complete_pro_preempt(dev, pr_reg_n, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3064 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, |
| 3065 | type, scope, preempt_type); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3066 | /* |
| 3067 | * d) Process tasks as defined in 5.7.1; |
| 3068 | * e) See above.. |
| 3069 | * f) If the type or scope has changed, then for every I_T nexus |
| 3070 | * whose reservation key was not removed, except for the I_T |
| 3071 | * nexus on which the PERSISTENT RESERVE OUT command was |
| 3072 | * received, the device server shall establish a unit |
| 3073 | * attention condition for the initiator port associated with |
| 3074 | * that I_T nexus, with the additional sense code set to |
| 3075 | * RESERVATIONS RELEASED. If the type or scope have not |
| 3076 | * changed, then no unit attention condition(s) shall be |
| 3077 | * established for this reason. |
| 3078 | */ |
| 3079 | if ((prh_type != type) || (prh_scope != scope)) { |
| 3080 | spin_lock(&pr_tmpl->registration_lock); |
| 3081 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 3082 | &pr_tmpl->registration_list, pr_reg_list) { |
| 3083 | |
| 3084 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; |
| 3085 | if (calling_it_nexus) |
| 3086 | continue; |
| 3087 | |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 3088 | target_ua_allocate_lun(pr_reg->pr_reg_nacl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3089 | pr_reg->pr_res_mapped_lun, 0x2A, |
| 3090 | ASCQ_2AH_RESERVATIONS_RELEASED); |
| 3091 | } |
| 3092 | spin_unlock(&pr_tmpl->registration_lock); |
| 3093 | } |
| 3094 | spin_unlock(&dev->dev_reservation_lock); |
| 3095 | /* |
| 3096 | * Call LUN_RESET logic upon list of struct t10_pr_registration, |
| 3097 | * All received CDBs for the matching existing reservation and |
| 3098 | * registrations undergo ABORT_TASK logic. |
| 3099 | * |
| 3100 | * From there, core_scsi3_release_preempt_and_abort() will |
| 3101 | * release every registration in the list (which have already |
| 3102 | * been removed from the primary pr_reg list), except the |
| 3103 | * new persistent reservation holder, the calling Initiator Port. |
| 3104 | */ |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3105 | if (preempt_type == PREEMPT_AND_ABORT) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3106 | core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd); |
| 3107 | core_scsi3_release_preempt_and_abort(&preempt_and_abort_list, |
| 3108 | pr_reg_n); |
| 3109 | } |
| 3110 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 3111 | if (pr_tmpl->pr_aptpl_active) |
| 3112 | core_scsi3_update_and_write_aptpl(cmd->se_dev, true); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3113 | |
| 3114 | core_scsi3_put_pr_reg(pr_reg_n); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3115 | core_scsi3_pr_generation(cmd->se_dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3116 | return 0; |
| 3117 | } |
| 3118 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3119 | static sense_reason_t |
| 3120 | core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3121 | u64 res_key, u64 sa_res_key, enum preempt_type preempt_type) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3122 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3123 | switch (type) { |
| 3124 | case PR_TYPE_WRITE_EXCLUSIVE: |
| 3125 | case PR_TYPE_EXCLUSIVE_ACCESS: |
| 3126 | case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: |
| 3127 | case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: |
| 3128 | case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: |
| 3129 | case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3130 | return core_scsi3_pro_preempt(cmd, type, scope, res_key, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3131 | sa_res_key, preempt_type); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3132 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3133 | pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s" |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3134 | " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3135 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3136 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3140 | static sense_reason_t |
| 3141 | core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key, |
| 3142 | u64 sa_res_key, int aptpl, int unreg) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3143 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3144 | struct se_session *se_sess = cmd->se_sess; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3145 | struct se_device *dev = cmd->se_dev; |
Jörn Engel | 2816890 | 2012-03-15 15:06:58 -0400 | [diff] [blame] | 3146 | struct se_dev_entry *dest_se_deve = NULL; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 3147 | struct se_lun *se_lun = cmd->se_lun, *tmp_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3148 | struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3149 | struct se_portal_group *se_tpg, *dest_se_tpg = NULL; |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3150 | const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3151 | struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3152 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3153 | unsigned char *buf; |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 3154 | const unsigned char *initiator_str; |
Chaitanya Kulkarni | 4db6dfe | 2021-02-27 21:56:42 -0800 | [diff] [blame] | 3155 | char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN] = { }; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3156 | u32 tid_len, tmp_tid_len; |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 3157 | int new_reg = 0, type, scope, matching_iname; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3158 | sense_reason_t ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3159 | unsigned short rtpi; |
| 3160 | unsigned char proto_ident; |
| 3161 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3162 | if (!se_sess || !se_lun) { |
| 3163 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3164 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3165 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3166 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3167 | se_tpg = se_sess->se_tpg; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3168 | tf_ops = se_tpg->se_tpg_tfo; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3169 | /* |
| 3170 | * Follow logic from spc4r17 Section 5.7.8, Table 50 -- |
| 3171 | * Register behaviors for a REGISTER AND MOVE service action |
| 3172 | * |
| 3173 | * Locate the existing *pr_reg via struct se_node_acl pointers |
| 3174 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3175 | pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3176 | se_sess); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3177 | if (!pr_reg) { |
| 3178 | pr_err("SPC-3 PR: Unable to locate PR_REGISTERED" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3179 | " *pr_reg for REGISTER_AND_MOVE\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3180 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3181 | } |
| 3182 | /* |
| 3183 | * The provided reservation key much match the existing reservation key |
| 3184 | * provided during this initiator's I_T nexus registration. |
| 3185 | */ |
| 3186 | if (res_key != pr_reg->pr_res_key) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3187 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3188 | " res_key: 0x%016Lx does not match existing SA REGISTER" |
| 3189 | " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3190 | ret = TCM_RESERVATION_CONFLICT; |
| 3191 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3192 | } |
| 3193 | /* |
| 3194 | * The service active reservation key needs to be non zero |
| 3195 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3196 | if (!sa_res_key) { |
| 3197 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3198 | " sa_res_key\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3199 | ret = TCM_INVALID_PARAMETER_LIST; |
| 3200 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3201 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3202 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3203 | /* |
| 3204 | * Determine the Relative Target Port Identifier where the reservation |
| 3205 | * will be moved to for the TransportID containing SCSI initiator WWN |
| 3206 | * information. |
| 3207 | */ |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3208 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3209 | if (!buf) { |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 3210 | ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3211 | goto out_put_pr_reg; |
| 3212 | } |
| 3213 | |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3214 | rtpi = get_unaligned_be16(&buf[18]); |
| 3215 | tid_len = get_unaligned_be32(&buf[20]); |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3216 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3217 | buf = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3218 | |
| 3219 | if ((tid_len + 24) != cmd->data_length) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3220 | pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3221 | " does not equal CDB data_length: %u\n", tid_len, |
| 3222 | cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3223 | ret = TCM_INVALID_PARAMETER_LIST; |
| 3224 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | spin_lock(&dev->se_port_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 3228 | list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) { |
Roman Bolshakov | b9e063a | 2023-03-01 11:45:10 +0300 | [diff] [blame] | 3229 | if (tmp_lun->lun_tpg->tpg_rtpi != rtpi) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3230 | continue; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 3231 | dest_se_tpg = tmp_lun->lun_tpg; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3232 | dest_tf_ops = dest_se_tpg->se_tpg_tfo; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3233 | if (!dest_tf_ops) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3234 | continue; |
| 3235 | |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 3236 | atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3237 | spin_unlock(&dev->se_port_lock); |
| 3238 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3239 | if (core_scsi3_tpg_depend_item(dest_se_tpg)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3240 | pr_err("core_scsi3_tpg_depend_item() failed" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3241 | " for dest_se_tpg\n"); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 3242 | atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3243 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 3244 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | spin_lock(&dev->se_port_lock); |
| 3248 | break; |
| 3249 | } |
| 3250 | spin_unlock(&dev->se_port_lock); |
| 3251 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3252 | if (!dest_se_tpg || !dest_tf_ops) { |
| 3253 | pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3254 | " fabric ops from Relative Target Port Identifier:" |
| 3255 | " %hu\n", rtpi); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3256 | ret = TCM_INVALID_PARAMETER_LIST; |
| 3257 | goto out_put_pr_reg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3258 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3259 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3260 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3261 | if (!buf) { |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 3262 | ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3263 | goto out_put_pr_reg; |
| 3264 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3265 | proto_ident = (buf[24] & 0x0f); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3266 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3267 | pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3268 | " 0x%02x\n", proto_ident); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3269 | |
Christoph Hellwig | 2aeeafa | 2015-05-01 17:47:57 +0200 | [diff] [blame] | 3270 | if (proto_ident != dest_se_tpg->proto_id) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3271 | pr_err("SPC-3 PR REGISTER_AND_MOVE: Received" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3272 | " proto_ident: 0x%02x does not match ident: 0x%02x" |
| 3273 | " from fabric: %s\n", proto_ident, |
Christoph Hellwig | 2aeeafa | 2015-05-01 17:47:57 +0200 | [diff] [blame] | 3274 | dest_se_tpg->proto_id, |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3275 | dest_tf_ops->fabric_name); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3276 | ret = TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3277 | goto out; |
| 3278 | } |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 3279 | initiator_str = target_parse_pr_out_transport_id(dest_se_tpg, |
Rasmus Villemoes | 094bb5d | 2017-11-21 01:12:43 +0100 | [diff] [blame] | 3280 | &buf[24], &tmp_tid_len, &iport_ptr); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3281 | if (!initiator_str) { |
| 3282 | pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3283 | " initiator_str from Transport ID\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3284 | ret = TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3285 | goto out; |
| 3286 | } |
| 3287 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3288 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3289 | buf = NULL; |
| 3290 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3291 | pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3292 | " %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ? |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3293 | "port" : "device", initiator_str, (iport_ptr != NULL) ? |
| 3294 | iport_ptr : ""); |
| 3295 | /* |
| 3296 | * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service |
| 3297 | * action specifies a TransportID that is the same as the initiator port |
| 3298 | * of the I_T nexus for the command received, then the command shall |
| 3299 | * be terminated with CHECK CONDITION status, with the sense key set to |
| 3300 | * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD |
| 3301 | * IN PARAMETER LIST. |
| 3302 | */ |
| 3303 | pr_reg_nacl = pr_reg->pr_reg_nacl; |
| 3304 | matching_iname = (!strcmp(initiator_str, |
| 3305 | pr_reg_nacl->initiatorname)) ? 1 : 0; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3306 | if (!matching_iname) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3307 | goto after_iport_check; |
| 3308 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3309 | if (!iport_ptr || !pr_reg->isid_present_at_reg) { |
| 3310 | pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3311 | " matches: %s on received I_T Nexus\n", initiator_str, |
| 3312 | pr_reg_nacl->initiatorname); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3313 | ret = TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3314 | goto out; |
| 3315 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3316 | if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) { |
| 3317 | pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3318 | " matches: %s %s on received I_T Nexus\n", |
| 3319 | initiator_str, iport_ptr, pr_reg_nacl->initiatorname, |
| 3320 | pr_reg->pr_reg_isid); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3321 | ret = TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3322 | goto out; |
| 3323 | } |
| 3324 | after_iport_check: |
| 3325 | /* |
| 3326 | * Locate the destination struct se_node_acl from the received Transport ID |
| 3327 | */ |
Nicholas Bellinger | 403edd7 | 2015-03-08 22:33:47 +0000 | [diff] [blame] | 3328 | mutex_lock(&dest_se_tpg->acl_node_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3329 | dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg, |
| 3330 | initiator_str); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 3331 | if (dest_node_acl) |
| 3332 | atomic_inc_mb(&dest_node_acl->acl_pr_ref_count); |
Nicholas Bellinger | 403edd7 | 2015-03-08 22:33:47 +0000 | [diff] [blame] | 3333 | mutex_unlock(&dest_se_tpg->acl_node_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3334 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3335 | if (!dest_node_acl) { |
| 3336 | pr_err("Unable to locate %s dest_node_acl for" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3337 | " TransportID%s\n", dest_tf_ops->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3338 | initiator_str); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3339 | ret = TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3340 | goto out; |
| 3341 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3342 | |
| 3343 | if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3344 | pr_err("core_scsi3_nodeacl_depend_item() for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3345 | " dest_node_acl\n"); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 3346 | atomic_dec_mb(&dest_node_acl->acl_pr_ref_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3347 | dest_node_acl = NULL; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3348 | ret = TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3349 | goto out; |
| 3350 | } |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3351 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3352 | pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3353 | " %s from TransportID\n", dest_tf_ops->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3354 | dest_node_acl->initiatorname); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3355 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3356 | /* |
| 3357 | * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET |
| 3358 | * PORT IDENTIFIER. |
| 3359 | */ |
| 3360 | dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3361 | if (!dest_se_deve) { |
| 3362 | pr_err("Unable to locate %s dest_se_deve from RTPI:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3363 | " %hu\n", dest_tf_ops->fabric_name, rtpi); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3364 | ret = TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3365 | goto out; |
| 3366 | } |
| 3367 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3368 | if (core_scsi3_lunacl_depend_item(dest_se_deve)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3369 | pr_err("core_scsi3_lunacl_depend_item() failed\n"); |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 3370 | kref_put(&dest_se_deve->pr_kref, target_pr_kref_release); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3371 | dest_se_deve = NULL; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3372 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3373 | goto out; |
| 3374 | } |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3375 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3376 | pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN" |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 3377 | " ACL for dest_se_deve->mapped_lun: %llu\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3378 | dest_tf_ops->fabric_name, dest_node_acl->initiatorname, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3379 | dest_se_deve->mapped_lun); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3380 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3381 | /* |
| 3382 | * A persistent reservation needs to already existing in order to |
| 3383 | * successfully complete the REGISTER_AND_MOVE service action.. |
| 3384 | */ |
| 3385 | spin_lock(&dev->dev_reservation_lock); |
| 3386 | pr_res_holder = dev->dev_pr_res_holder; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3387 | if (!pr_res_holder) { |
| 3388 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3389 | " currently held\n"); |
| 3390 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3391 | ret = TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3392 | goto out; |
| 3393 | } |
| 3394 | /* |
| 3395 | * The received on I_T Nexus must be the reservation holder. |
| 3396 | * |
| 3397 | * From spc4r17 section 5.7.8 Table 50 -- |
| 3398 | * Register behaviors for a REGISTER AND MOVE service action |
| 3399 | */ |
Ilias Tsitsimpis | e673dc9 | 2015-03-26 17:12:42 +0200 | [diff] [blame] | 3400 | if (!is_reservation_holder(pr_res_holder, pr_reg)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3401 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3402 | " Nexus is not reservation holder\n"); |
| 3403 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3404 | ret = TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3405 | goto out; |
| 3406 | } |
| 3407 | /* |
| 3408 | * From spc4r17 section 5.7.8: registering and moving reservation |
| 3409 | * |
| 3410 | * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service |
| 3411 | * action is received and the established persistent reservation is a |
| 3412 | * Write Exclusive - All Registrants type or Exclusive Access - |
| 3413 | * All Registrants type reservation, then the command shall be completed |
| 3414 | * with RESERVATION CONFLICT status. |
| 3415 | */ |
| 3416 | if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || |
| 3417 | (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3418 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3419 | " reservation for type: %s\n", |
| 3420 | core_scsi3_pr_dump_type(pr_res_holder->pr_res_type)); |
| 3421 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3422 | ret = TCM_RESERVATION_CONFLICT; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3423 | goto out; |
| 3424 | } |
| 3425 | pr_res_nacl = pr_res_holder->pr_reg_nacl; |
| 3426 | /* |
| 3427 | * b) Ignore the contents of the (received) SCOPE and TYPE fields; |
| 3428 | */ |
| 3429 | type = pr_res_holder->pr_res_type; |
| 3430 | scope = pr_res_holder->pr_res_type; |
| 3431 | /* |
| 3432 | * c) Associate the reservation key specified in the SERVICE ACTION |
| 3433 | * RESERVATION KEY field with the I_T nexus specified as the |
| 3434 | * destination of the register and move, where: |
| 3435 | * A) The I_T nexus is specified by the TransportID and the |
| 3436 | * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and |
| 3437 | * B) Regardless of the TransportID format used, the association for |
| 3438 | * the initiator port is based on either the initiator port name |
| 3439 | * (see 3.1.71) on SCSI transport protocols where port names are |
| 3440 | * required or the initiator port identifier (see 3.1.70) on SCSI |
| 3441 | * transport protocols where port names are not required; |
| 3442 | * d) Register the reservation key specified in the SERVICE ACTION |
| 3443 | * RESERVATION KEY field; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3444 | * |
| 3445 | * Also, It is not an error for a REGISTER AND MOVE service action to |
| 3446 | * register an I_T nexus that is already registered with the same |
| 3447 | * reservation key or a different reservation key. |
| 3448 | */ |
| 3449 | dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, |
| 3450 | iport_ptr); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3451 | if (!dest_pr_reg) { |
Dmitry Bogdanov | ef4f7e4 | 2022-07-28 00:41:25 +0300 | [diff] [blame] | 3452 | struct se_lun *dest_lun = dest_se_deve->se_lun; |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 3453 | |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 3454 | spin_unlock(&dev->dev_reservation_lock); |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 3455 | if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl, |
| 3456 | dest_lun, dest_se_deve, dest_se_deve->mapped_lun, |
| 3457 | iport_ptr, sa_res_key, 0, aptpl, 2, 1)) { |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 3458 | ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3459 | goto out; |
| 3460 | } |
Nicholas Bellinger | 80bfdfa | 2015-03-25 01:02:57 -0700 | [diff] [blame] | 3461 | spin_lock(&dev->dev_reservation_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3462 | dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, |
| 3463 | iport_ptr); |
| 3464 | new_reg = 1; |
Dmitry Bogdanov | 3e2deba | 2022-09-09 12:04:25 +0300 | [diff] [blame] | 3465 | } else { |
| 3466 | /* |
| 3467 | * e) Retain the reservation key specified in the SERVICE ACTION |
| 3468 | * RESERVATION KEY field and associated information; |
| 3469 | */ |
| 3470 | dest_pr_reg->pr_res_key = sa_res_key; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3471 | } |
| 3472 | /* |
| 3473 | * f) Release the persistent reservation for the persistent reservation |
| 3474 | * holder (i.e., the I_T nexus on which the |
| 3475 | */ |
| 3476 | __core_scsi3_complete_pro_release(dev, pr_res_nacl, |
Nicholas Bellinger | 6c3c9ba | 2014-12-15 11:50:26 -0800 | [diff] [blame] | 3477 | dev->dev_pr_res_holder, 0, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3478 | /* |
| 3479 | * g) Move the persistent reservation to the specified I_T nexus using |
| 3480 | * the same scope and type as the persistent reservation released in |
| 3481 | * item f); and |
| 3482 | */ |
| 3483 | dev->dev_pr_res_holder = dest_pr_reg; |
| 3484 | dest_pr_reg->pr_res_holder = 1; |
| 3485 | dest_pr_reg->pr_res_type = type; |
| 3486 | pr_reg->pr_res_scope = scope; |
Andy Grover | d2843c1 | 2013-05-16 10:40:55 -0700 | [diff] [blame] | 3487 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3488 | /* |
| 3489 | * Increment PRGeneration for existing registrations.. |
| 3490 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3491 | if (!new_reg) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3492 | dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++; |
| 3493 | spin_unlock(&dev->dev_reservation_lock); |
| 3494 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3495 | pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3496 | " created new reservation holder TYPE: %s on object RTPI:" |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3497 | " %hu PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3498 | core_scsi3_pr_dump_type(type), rtpi, |
| 3499 | dest_pr_reg->pr_res_generation); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3500 | pr_debug("SPC-3 PR Successfully moved reservation from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3501 | " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n", |
David Disseldorp | 30c7ca9 | 2018-11-23 18:36:12 +0100 | [diff] [blame] | 3502 | tf_ops->fabric_name, pr_reg_nacl->initiatorname, |
| 3503 | i_buf, dest_tf_ops->fabric_name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3504 | dest_node_acl->initiatorname, (iport_ptr != NULL) ? |
| 3505 | iport_ptr : ""); |
| 3506 | /* |
| 3507 | * It is now safe to release configfs group dependencies for destination |
| 3508 | * of Transport ID Initiator Device/Port Identifier |
| 3509 | */ |
| 3510 | core_scsi3_lunacl_undepend_item(dest_se_deve); |
| 3511 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 3512 | core_scsi3_tpg_undepend_item(dest_se_tpg); |
| 3513 | /* |
| 3514 | * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T |
| 3515 | * nexus on which PERSISTENT RESERVE OUT command was received. |
| 3516 | */ |
| 3517 | if (unreg) { |
| 3518 | spin_lock(&pr_tmpl->registration_lock); |
| 3519 | __core_scsi3_free_registration(dev, pr_reg, NULL, 1); |
| 3520 | spin_unlock(&pr_tmpl->registration_lock); |
| 3521 | } else |
| 3522 | core_scsi3_put_pr_reg(pr_reg); |
| 3523 | |
Andy Grover | 459f213 | 2013-05-16 10:41:02 -0700 | [diff] [blame] | 3524 | core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3525 | |
| 3526 | core_scsi3_put_pr_reg(dest_pr_reg); |
| 3527 | return 0; |
| 3528 | out: |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3529 | if (buf) |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3530 | transport_kunmap_data_sg(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3531 | if (dest_se_deve) |
| 3532 | core_scsi3_lunacl_undepend_item(dest_se_deve); |
| 3533 | if (dest_node_acl) |
| 3534 | core_scsi3_nodeacl_undepend_item(dest_node_acl); |
| 3535 | core_scsi3_tpg_undepend_item(dest_se_tpg); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3536 | |
| 3537 | out_put_pr_reg: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3538 | core_scsi3_put_pr_reg(pr_reg); |
| 3539 | return ret; |
| 3540 | } |
| 3541 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 3542 | static sense_reason_t |
| 3543 | target_try_pr_out_pt(struct se_cmd *cmd, u8 sa, u64 res_key, u64 sa_res_key, |
| 3544 | u8 type, bool aptpl, bool all_tg_pt, bool spec_i_pt) |
| 3545 | { |
| 3546 | struct exec_cmd_ops *ops = cmd->protocol_data; |
| 3547 | |
| 3548 | if (!cmd->se_sess || !cmd->se_lun) { |
| 3549 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); |
| 3550 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 3551 | } |
| 3552 | |
| 3553 | if (!ops->execute_pr_out) { |
| 3554 | pr_err("SPC-3 PR: Device has been configured for PR passthrough but it's not supported by the backend.\n"); |
| 3555 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 3556 | } |
| 3557 | |
Mike Christie | 8455799 | 2023-04-07 15:05:50 -0500 | [diff] [blame] | 3558 | switch (sa) { |
| 3559 | case PRO_REGISTER_AND_MOVE: |
| 3560 | case PRO_REPLACE_LOST_RESERVATION: |
| 3561 | pr_err("SPC-3 PR: PRO_REGISTER_AND_MOVE and PRO_REPLACE_LOST_RESERVATION are not supported by PR passthrough.\n"); |
| 3562 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 3563 | } |
| 3564 | |
| 3565 | if (spec_i_pt || all_tg_pt) { |
| 3566 | pr_err("SPC-3 PR: SPEC_I_PT and ALL_TG_PT are not supported by PR passthrough.\n"); |
| 3567 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 3568 | } |
| 3569 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 3570 | return ops->execute_pr_out(cmd, sa, res_key, sa_res_key, type, aptpl); |
| 3571 | } |
| 3572 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3573 | /* |
| 3574 | * See spc4r17 section 6.14 Table 170 |
| 3575 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3576 | sense_reason_t |
| 3577 | target_scsi3_emulate_pr_out(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3578 | { |
Nicholas Bellinger | 92404e6 | 2014-10-04 01:06:08 +0000 | [diff] [blame] | 3579 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 3580 | unsigned char *cdb = &cmd->t_task_cdb[0]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3581 | unsigned char *buf; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3582 | u64 res_key, sa_res_key; |
| 3583 | int sa, scope, type, aptpl; |
| 3584 | int spec_i_pt = 0, all_tg_pt = 0, unreg = 0; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3585 | sense_reason_t ret; |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 3586 | |
| 3587 | /* |
| 3588 | * Following spc2r20 5.5.1 Reservations overview: |
| 3589 | * |
| 3590 | * If a logical unit has been reserved by any RESERVE command and is |
| 3591 | * still reserved by any initiator, all PERSISTENT RESERVE IN and all |
| 3592 | * PERSISTENT RESERVE OUT commands shall conflict regardless of |
| 3593 | * initiator or service action and shall terminate with a RESERVATION |
| 3594 | * CONFLICT status. |
| 3595 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3596 | if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 3597 | pr_err("Received PERSISTENT_RESERVE CDB while legacy" |
| 3598 | " SPC-2 reservation is held, returning" |
| 3599 | " RESERVATION_CONFLICT\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3600 | return TCM_RESERVATION_CONFLICT; |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 3601 | } |
| 3602 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3603 | /* |
| 3604 | * FIXME: A NULL struct se_session pointer means an this is not coming from |
| 3605 | * a $FABRIC_MOD's nexus, but from internal passthrough ops. |
| 3606 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3607 | if (!cmd->se_sess) |
| 3608 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3609 | |
| 3610 | if (cmd->data_length < 24) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3611 | pr_warn("SPC-PR: Received PR OUT parameter list" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3612 | " length too small: %u\n", cmd->data_length); |
Tang Wenji | 7ee0031 | 2017-07-08 11:28:25 +0800 | [diff] [blame] | 3613 | return TCM_PARAMETER_LIST_LENGTH_ERROR; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3614 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3615 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3616 | /* |
| 3617 | * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB) |
| 3618 | */ |
| 3619 | sa = (cdb[1] & 0x1f); |
| 3620 | scope = (cdb[2] & 0xf0); |
| 3621 | type = (cdb[2] & 0x0f); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3622 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3623 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3624 | if (!buf) |
| 3625 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 3626 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3627 | /* |
| 3628 | * From PERSISTENT_RESERVE_OUT parameter list (payload) |
| 3629 | */ |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3630 | res_key = get_unaligned_be64(&buf[0]); |
| 3631 | sa_res_key = get_unaligned_be64(&buf[8]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3632 | /* |
| 3633 | * REGISTER_AND_MOVE uses a different SA parameter list containing |
| 3634 | * SCSI TransportIDs. |
| 3635 | */ |
| 3636 | if (sa != PRO_REGISTER_AND_MOVE) { |
| 3637 | spec_i_pt = (buf[20] & 0x08); |
| 3638 | all_tg_pt = (buf[20] & 0x04); |
| 3639 | aptpl = (buf[20] & 0x01); |
| 3640 | } else { |
| 3641 | aptpl = (buf[17] & 0x01); |
| 3642 | unreg = (buf[17] & 0x02); |
| 3643 | } |
Nicholas Bellinger | 92404e6 | 2014-10-04 01:06:08 +0000 | [diff] [blame] | 3644 | /* |
| 3645 | * If the backend device has been configured to force APTPL metadata |
| 3646 | * write-out, go ahead and propigate aptpl=1 down now. |
| 3647 | */ |
| 3648 | if (dev->dev_attrib.force_pr_aptpl) |
| 3649 | aptpl = 1; |
| 3650 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3651 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3652 | buf = NULL; |
| 3653 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3654 | /* |
| 3655 | * SPEC_I_PT=1 is only valid for Service action: REGISTER |
| 3656 | */ |
Tang Wenji | 7ee0031 | 2017-07-08 11:28:25 +0800 | [diff] [blame] | 3657 | if (spec_i_pt && (sa != PRO_REGISTER)) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3658 | return TCM_INVALID_PARAMETER_LIST; |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3659 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3660 | /* |
| 3661 | * From spc4r17 section 6.14: |
| 3662 | * |
| 3663 | * If the SPEC_I_PT bit is set to zero, the service action is not |
| 3664 | * REGISTER AND MOVE, and the parameter list length is not 24, then |
| 3665 | * the command shall be terminated with CHECK CONDITION status, with |
| 3666 | * the sense key set to ILLEGAL REQUEST, and the additional sense |
| 3667 | * code set to PARAMETER LIST LENGTH ERROR. |
| 3668 | */ |
Tang Wenji | 7ee0031 | 2017-07-08 11:28:25 +0800 | [diff] [blame] | 3669 | if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) && |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3670 | (cmd->data_length != 24)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3671 | pr_warn("SPC-PR: Received PR OUT illegal parameter" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3672 | " list length: %u\n", cmd->data_length); |
Tang Wenji | 7ee0031 | 2017-07-08 11:28:25 +0800 | [diff] [blame] | 3673 | return TCM_PARAMETER_LIST_LENGTH_ERROR; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3674 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3675 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 3676 | if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) { |
| 3677 | ret = target_try_pr_out_pt(cmd, sa, res_key, sa_res_key, type, |
| 3678 | aptpl, all_tg_pt, spec_i_pt); |
| 3679 | goto done; |
| 3680 | } |
| 3681 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3682 | /* |
| 3683 | * (core_scsi3_emulate_pro_* function parameters |
| 3684 | * are defined by spc4r17 Table 174: |
| 3685 | * PERSISTENT_RESERVE_OUT service actions and valid parameters. |
| 3686 | */ |
| 3687 | switch (sa) { |
| 3688 | case PRO_REGISTER: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3689 | ret = core_scsi3_emulate_pro_register(cmd, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3690 | res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3691 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3692 | case PRO_RESERVE: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3693 | ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key); |
| 3694 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3695 | case PRO_RELEASE: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3696 | ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key); |
| 3697 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3698 | case PRO_CLEAR: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3699 | ret = core_scsi3_emulate_pro_clear(cmd, res_key); |
| 3700 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3701 | case PRO_PREEMPT: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3702 | ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3703 | res_key, sa_res_key, PREEMPT); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3704 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3705 | case PRO_PREEMPT_AND_ABORT: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3706 | ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3707 | res_key, sa_res_key, PREEMPT_AND_ABORT); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3708 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3709 | case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3710 | ret = core_scsi3_emulate_pro_register(cmd, |
Andy Grover | 33ce6a8 | 2013-05-16 10:40:54 -0700 | [diff] [blame] | 3711 | 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3712 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3713 | case PRO_REGISTER_AND_MOVE: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3714 | ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3715 | sa_res_key, aptpl, unreg); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3716 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3717 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3718 | pr_err("Unknown PERSISTENT_RESERVE_OUT service" |
Tang Wenji | 7ee0031 | 2017-07-08 11:28:25 +0800 | [diff] [blame] | 3719 | " action: 0x%02x\n", sa); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3720 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3721 | } |
| 3722 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 3723 | done: |
Christoph Hellwig | 6bb35e0 | 2012-04-23 11:35:33 -0400 | [diff] [blame] | 3724 | if (!ret) |
Hannes Reinecke | 14b40c1e | 2021-04-27 10:30:45 +0200 | [diff] [blame] | 3725 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 3726 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | /* |
| 3730 | * PERSISTENT_RESERVE_IN Service Action READ_KEYS |
| 3731 | * |
| 3732 | * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160 |
| 3733 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3734 | static sense_reason_t |
| 3735 | core_scsi3_pri_read_keys(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3736 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3737 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3738 | struct t10_pr_registration *pr_reg; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3739 | unsigned char *buf; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3740 | u32 add_len = 0, off = 8; |
| 3741 | |
| 3742 | if (cmd->data_length < 8) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3743 | pr_err("PRIN SA READ_KEYS SCSI Data Length: %u" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3744 | " too small\n", cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3745 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3746 | } |
| 3747 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3748 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3749 | if (!buf) |
| 3750 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 3751 | |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3752 | put_unaligned_be32(dev->t10_pr.pr_generation, buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3753 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3754 | spin_lock(&dev->t10_pr.registration_lock); |
| 3755 | list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3756 | pr_reg_list) { |
| 3757 | /* |
| 3758 | * Check for overflow of 8byte PRI READ_KEYS payload and |
| 3759 | * next reservation key list descriptor. |
| 3760 | */ |
David Disseldorp | 63ce3c3 | 2018-06-19 17:58:24 +0200 | [diff] [blame] | 3761 | if (off + 8 <= cmd->data_length) { |
| 3762 | put_unaligned_be64(pr_reg->pr_res_key, &buf[off]); |
| 3763 | off += 8; |
| 3764 | } |
| 3765 | /* |
| 3766 | * SPC5r17: 6.16.2 READ KEYS service action |
| 3767 | * The ADDITIONAL LENGTH field indicates the number of bytes in |
| 3768 | * the Reservation key list. The contents of the ADDITIONAL |
| 3769 | * LENGTH field are not altered based on the allocation length |
| 3770 | */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3771 | add_len += 8; |
| 3772 | } |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3773 | spin_unlock(&dev->t10_pr.registration_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3774 | |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3775 | put_unaligned_be32(add_len, &buf[4]); |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3776 | target_set_cmd_data_length(cmd, 8 + add_len); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3777 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3778 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3779 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3780 | return 0; |
| 3781 | } |
| 3782 | |
| 3783 | /* |
| 3784 | * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION |
| 3785 | * |
| 3786 | * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162 |
| 3787 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3788 | static sense_reason_t |
| 3789 | core_scsi3_pri_read_reservation(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3790 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3791 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3792 | struct t10_pr_registration *pr_reg; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3793 | unsigned char *buf; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3794 | u64 pr_res_key; |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3795 | u32 add_len = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3796 | |
| 3797 | if (cmd->data_length < 8) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3798 | pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3799 | " too small\n", cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3800 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3801 | } |
| 3802 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3803 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3804 | if (!buf) |
| 3805 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 3806 | |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3807 | put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3808 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3809 | spin_lock(&dev->dev_reservation_lock); |
| 3810 | pr_reg = dev->dev_pr_res_holder; |
Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 3811 | if (pr_reg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3812 | /* |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3813 | * Set the Additional Length to 16 when a reservation is held |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3814 | */ |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3815 | add_len = 16; |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3816 | put_unaligned_be32(add_len, &buf[4]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3817 | |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3818 | if (cmd->data_length < 22) |
| 3819 | goto err; |
| 3820 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3821 | /* |
| 3822 | * Set the Reservation key. |
| 3823 | * |
| 3824 | * From spc4r17, section 5.7.10: |
| 3825 | * A persistent reservation holder has its reservation key |
| 3826 | * returned in the parameter data from a PERSISTENT |
| 3827 | * RESERVE IN command with READ RESERVATION service action as |
| 3828 | * follows: |
| 3829 | * a) For a persistent reservation of the type Write Exclusive |
| 3830 | * - All Registrants or Exclusive Access  All Regitrants, |
| 3831 | * the reservation key shall be set to zero; or |
| 3832 | * b) For all other persistent reservation types, the |
| 3833 | * reservation key shall be set to the registered |
| 3834 | * reservation key for the I_T nexus that holds the |
| 3835 | * persistent reservation. |
| 3836 | */ |
| 3837 | if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || |
| 3838 | (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) |
| 3839 | pr_res_key = 0; |
| 3840 | else |
| 3841 | pr_res_key = pr_reg->pr_res_key; |
| 3842 | |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3843 | put_unaligned_be64(pr_res_key, &buf[8]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3844 | /* |
| 3845 | * Set the SCOPE and TYPE |
| 3846 | */ |
| 3847 | buf[21] = (pr_reg->pr_res_scope & 0xf0) | |
| 3848 | (pr_reg->pr_res_type & 0x0f); |
| 3849 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3850 | |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3851 | target_set_cmd_data_length(cmd, 8 + add_len); |
| 3852 | |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3853 | err: |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3854 | spin_unlock(&dev->dev_reservation_lock); |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3855 | transport_kunmap_data_sg(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3856 | |
| 3857 | return 0; |
| 3858 | } |
| 3859 | |
| 3860 | /* |
| 3861 | * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES |
| 3862 | * |
| 3863 | * See spc4r17 section 6.13.4 Table 165 |
| 3864 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3865 | static sense_reason_t |
| 3866 | core_scsi3_pri_report_capabilities(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3867 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3868 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3869 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3870 | unsigned char *buf; |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3871 | u16 len = 8; /* Hardcoded to 8. */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3872 | |
| 3873 | if (cmd->data_length < 6) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3874 | pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3875 | " %u too small\n", cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3876 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3877 | } |
| 3878 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3879 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3880 | if (!buf) |
| 3881 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3882 | |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3883 | put_unaligned_be16(len, &buf[0]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3884 | buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */ |
| 3885 | buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */ |
| 3886 | buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */ |
| 3887 | buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */ |
| 3888 | /* |
| 3889 | * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so |
| 3890 | * set the TMV: Task Mask Valid bit. |
| 3891 | */ |
| 3892 | buf[3] |= 0x80; |
| 3893 | /* |
| 3894 | * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166 |
| 3895 | */ |
| 3896 | buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */ |
| 3897 | /* |
| 3898 | * PTPL_A: Persistence across Target Power Loss Active bit |
| 3899 | */ |
| 3900 | if (pr_tmpl->pr_aptpl_active) |
| 3901 | buf[3] |= 0x01; |
| 3902 | /* |
| 3903 | * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167 |
| 3904 | */ |
| 3905 | buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ |
| 3906 | buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */ |
| 3907 | buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */ |
| 3908 | buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */ |
| 3909 | buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */ |
| 3910 | buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ |
| 3911 | |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 3912 | target_set_cmd_data_length(cmd, len); |
| 3913 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3914 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3915 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3916 | return 0; |
| 3917 | } |
| 3918 | |
| 3919 | /* |
| 3920 | * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS |
| 3921 | * |
| 3922 | * See spc4r17 section 6.13.5 Table 168 and 169 |
| 3923 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3924 | static sense_reason_t |
| 3925 | core_scsi3_pri_read_full_status(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3926 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3927 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3928 | struct se_node_acl *se_nacl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3929 | struct se_portal_group *se_tpg; |
| 3930 | struct t10_pr_registration *pr_reg, *pr_reg_tmp; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 3931 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3932 | unsigned char *buf; |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 3933 | u32 add_desc_len = 0, add_len = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3934 | u32 off = 8; /* off into first Full Status descriptor */ |
Nicholas Bellinger | d16ca7c | 2014-12-14 01:47:19 -0800 | [diff] [blame] | 3935 | int format_code = 0, pr_res_type = 0, pr_res_scope = 0; |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 3936 | int exp_desc_len, desc_len; |
Nicholas Bellinger | d16ca7c | 2014-12-14 01:47:19 -0800 | [diff] [blame] | 3937 | bool all_reg = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3938 | |
| 3939 | if (cmd->data_length < 8) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3940 | pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3941 | " too small\n", cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3942 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3943 | } |
| 3944 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 3945 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3946 | if (!buf) |
| 3947 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3948 | |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3949 | put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3950 | |
Nicholas Bellinger | d16ca7c | 2014-12-14 01:47:19 -0800 | [diff] [blame] | 3951 | spin_lock(&dev->dev_reservation_lock); |
| 3952 | if (dev->dev_pr_res_holder) { |
| 3953 | struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder; |
| 3954 | |
| 3955 | if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG || |
| 3956 | pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) { |
| 3957 | all_reg = true; |
| 3958 | pr_res_type = pr_holder->pr_res_type; |
| 3959 | pr_res_scope = pr_holder->pr_res_scope; |
| 3960 | } |
| 3961 | } |
| 3962 | spin_unlock(&dev->dev_reservation_lock); |
| 3963 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3964 | spin_lock(&pr_tmpl->registration_lock); |
| 3965 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, |
| 3966 | &pr_tmpl->registration_list, pr_reg_list) { |
| 3967 | |
| 3968 | se_nacl = pr_reg->pr_reg_nacl; |
| 3969 | se_tpg = pr_reg->pr_reg_nacl->se_tpg; |
| 3970 | add_desc_len = 0; |
| 3971 | |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 3972 | atomic_inc_mb(&pr_reg->pr_res_holders); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3973 | spin_unlock(&pr_tmpl->registration_lock); |
| 3974 | /* |
| 3975 | * Determine expected length of $FABRIC_MOD specific |
| 3976 | * TransportID full status descriptor.. |
| 3977 | */ |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 3978 | exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg, |
| 3979 | &format_code); |
| 3980 | if (exp_desc_len < 0 || |
| 3981 | exp_desc_len + add_len > cmd->data_length) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3982 | pr_warn("SPC-3 PRIN READ_FULL_STATUS ran" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3983 | " out of buffer: %d\n", cmd->data_length); |
| 3984 | spin_lock(&pr_tmpl->registration_lock); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 3985 | atomic_dec_mb(&pr_reg->pr_res_holders); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3986 | break; |
| 3987 | } |
| 3988 | /* |
| 3989 | * Set RESERVATION KEY |
| 3990 | */ |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 3991 | put_unaligned_be64(pr_reg->pr_res_key, &buf[off]); |
| 3992 | off += 8; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3993 | off += 4; /* Skip Over Reserved area */ |
| 3994 | |
| 3995 | /* |
| 3996 | * Set ALL_TG_PT bit if PROUT SA REGISTER had this set. |
| 3997 | */ |
| 3998 | if (pr_reg->pr_reg_all_tg_pt) |
| 3999 | buf[off] = 0x02; |
| 4000 | /* |
| 4001 | * The struct se_lun pointer will be present for the |
| 4002 | * reservation holder for PR_HOLDER bit. |
| 4003 | * |
| 4004 | * Also, if this registration is the reservation |
Nicholas Bellinger | d16ca7c | 2014-12-14 01:47:19 -0800 | [diff] [blame] | 4005 | * holder or there is an All Registrants reservation |
| 4006 | * active, fill in SCOPE and TYPE in the next byte. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4007 | */ |
| 4008 | if (pr_reg->pr_res_holder) { |
| 4009 | buf[off++] |= 0x01; |
| 4010 | buf[off++] = (pr_reg->pr_res_scope & 0xf0) | |
| 4011 | (pr_reg->pr_res_type & 0x0f); |
Nicholas Bellinger | d16ca7c | 2014-12-14 01:47:19 -0800 | [diff] [blame] | 4012 | } else if (all_reg) { |
| 4013 | buf[off++] |= 0x01; |
| 4014 | buf[off++] = (pr_res_scope & 0xf0) | |
| 4015 | (pr_res_type & 0x0f); |
| 4016 | } else { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4017 | off += 2; |
Nicholas Bellinger | d16ca7c | 2014-12-14 01:47:19 -0800 | [diff] [blame] | 4018 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4019 | |
| 4020 | off += 4; /* Skip over reserved area */ |
| 4021 | /* |
| 4022 | * From spc4r17 6.3.15: |
| 4023 | * |
| 4024 | * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT |
| 4025 | * IDENTIFIER field contains the relative port identifier (see |
| 4026 | * 3.1.120) of the target port that is part of the I_T nexus |
| 4027 | * described by this full status descriptor. If the ALL_TG_PT |
| 4028 | * bit is set to one, the contents of the RELATIVE TARGET PORT |
| 4029 | * IDENTIFIER field are not defined by this standard. |
| 4030 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 4031 | if (!pr_reg->pr_reg_all_tg_pt) { |
Nicholas Bellinger | 79dc9c9 | 2015-03-27 04:51:03 +0000 | [diff] [blame] | 4032 | u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4033 | |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 4034 | put_unaligned_be16(sep_rtpi, &buf[off]); |
| 4035 | off += 2; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4036 | } else |
Masanari Iida | 35d1efe | 2012-08-16 22:43:13 +0900 | [diff] [blame] | 4037 | off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4038 | |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 4039 | buf[off+4] = se_tpg->proto_id; |
| 4040 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4041 | /* |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 4042 | * Now, have the $FABRIC_MOD fill in the transport ID. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4043 | */ |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 4044 | desc_len = target_get_pr_transport_id(se_nacl, pr_reg, |
| 4045 | &format_code, &buf[off+4]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4046 | |
| 4047 | spin_lock(&pr_tmpl->registration_lock); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 4048 | atomic_dec_mb(&pr_reg->pr_res_holders); |
Christoph Hellwig | 2650d71 | 2015-05-01 17:47:58 +0200 | [diff] [blame] | 4049 | |
| 4050 | if (desc_len < 0) |
| 4051 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4052 | /* |
| 4053 | * Set the ADDITIONAL DESCRIPTOR LENGTH |
| 4054 | */ |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 4055 | put_unaligned_be32(desc_len, &buf[off]); |
tangwenji | c58a252 | 2017-08-17 19:51:54 +0800 | [diff] [blame] | 4056 | off += 4; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4057 | /* |
| 4058 | * Size of full desctipor header minus TransportID |
| 4059 | * containing $FABRIC_MOD specific) initiator device/port |
| 4060 | * WWN information. |
| 4061 | * |
| 4062 | * See spc4r17 Section 6.13.5 Table 169 |
| 4063 | */ |
| 4064 | add_desc_len = (24 + desc_len); |
| 4065 | |
| 4066 | off += desc_len; |
| 4067 | add_len += add_desc_len; |
| 4068 | } |
| 4069 | spin_unlock(&pr_tmpl->registration_lock); |
| 4070 | /* |
| 4071 | * Set ADDITIONAL_LENGTH |
| 4072 | */ |
Bart Van Assche | a85d667 | 2017-05-23 16:48:27 -0700 | [diff] [blame] | 4073 | put_unaligned_be32(add_len, &buf[4]); |
Aleksandr Miloserdov | 14d24e2 | 2021-02-09 10:22:02 +0300 | [diff] [blame] | 4074 | target_set_cmd_data_length(cmd, 8 + add_len); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4075 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 4076 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 4077 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4078 | return 0; |
| 4079 | } |
| 4080 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 4081 | static sense_reason_t target_try_pr_in_pt(struct se_cmd *cmd, u8 sa) |
| 4082 | { |
| 4083 | struct exec_cmd_ops *ops = cmd->protocol_data; |
| 4084 | unsigned char *buf; |
| 4085 | sense_reason_t ret; |
| 4086 | |
| 4087 | if (cmd->data_length < 8) { |
| 4088 | pr_err("PRIN SA SCSI Data Length: %u too small\n", |
| 4089 | cmd->data_length); |
| 4090 | return TCM_INVALID_CDB_FIELD; |
| 4091 | } |
| 4092 | |
| 4093 | if (!ops->execute_pr_in) { |
| 4094 | pr_err("SPC-3 PR: Device has been configured for PR passthrough but it's not supported by the backend.\n"); |
| 4095 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 4096 | } |
| 4097 | |
Mike Christie | 8455799 | 2023-04-07 15:05:50 -0500 | [diff] [blame] | 4098 | if (sa == PRI_READ_FULL_STATUS) { |
| 4099 | pr_err("SPC-3 PR: PRI_READ_FULL_STATUS is not supported by PR passthrough.\n"); |
| 4100 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 4101 | } |
| 4102 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 4103 | buf = transport_kmap_data_sg(cmd); |
| 4104 | if (!buf) |
| 4105 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 4106 | |
| 4107 | ret = ops->execute_pr_in(cmd, sa, buf); |
| 4108 | |
| 4109 | transport_kunmap_data_sg(cmd); |
| 4110 | return ret; |
| 4111 | } |
| 4112 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 4113 | sense_reason_t |
| 4114 | target_scsi3_emulate_pr_in(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4115 | { |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 4116 | u8 sa = cmd->t_task_cdb[1] & 0x1f; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 4117 | sense_reason_t ret; |
Christoph Hellwig | e76a35d | 2011-11-03 17:50:42 -0400 | [diff] [blame] | 4118 | |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 4119 | /* |
| 4120 | * Following spc2r20 5.5.1 Reservations overview: |
| 4121 | * |
| 4122 | * If a logical unit has been reserved by any RESERVE command and is |
| 4123 | * still reserved by any initiator, all PERSISTENT RESERVE IN and all |
| 4124 | * PERSISTENT RESERVE OUT commands shall conflict regardless of |
| 4125 | * initiator or service action and shall terminate with a RESERVATION |
| 4126 | * CONFLICT status. |
| 4127 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 4128 | if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 4129 | pr_err("Received PERSISTENT_RESERVE CDB while legacy" |
| 4130 | " SPC-2 reservation is held, returning" |
| 4131 | " RESERVATION_CONFLICT\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 4132 | return TCM_RESERVATION_CONFLICT; |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 4133 | } |
| 4134 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 4135 | if (cmd->se_dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) { |
| 4136 | ret = target_try_pr_in_pt(cmd, sa); |
| 4137 | goto done; |
| 4138 | } |
| 4139 | |
| 4140 | switch (sa) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4141 | case PRI_READ_KEYS: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 4142 | ret = core_scsi3_pri_read_keys(cmd); |
| 4143 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4144 | case PRI_READ_RESERVATION: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 4145 | ret = core_scsi3_pri_read_reservation(cmd); |
| 4146 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4147 | case PRI_REPORT_CAPABILITIES: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 4148 | ret = core_scsi3_pri_report_capabilities(cmd); |
| 4149 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4150 | case PRI_READ_FULL_STATUS: |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 4151 | ret = core_scsi3_pri_read_full_status(cmd); |
| 4152 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4153 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 4154 | pr_err("Unknown PERSISTENT_RESERVE_IN service" |
Christoph Hellwig | 617c0e0 | 2011-11-03 17:50:41 -0400 | [diff] [blame] | 4155 | " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 4156 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4157 | } |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 4158 | |
Mike Christie | 53062ac | 2023-04-07 15:05:48 -0500 | [diff] [blame] | 4159 | done: |
Christoph Hellwig | 6bb35e0 | 2012-04-23 11:35:33 -0400 | [diff] [blame] | 4160 | if (!ret) |
Hannes Reinecke | 14b40c1e | 2021-04-27 10:30:45 +0200 | [diff] [blame] | 4161 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
Christoph Hellwig | d29a5b6 | 2011-11-03 17:50:44 -0400 | [diff] [blame] | 4162 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4163 | } |
| 4164 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 4165 | sense_reason_t |
| 4166 | target_check_reservation(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4167 | { |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 4168 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 4169 | sense_reason_t ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4170 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 4171 | if (!cmd->se_sess) |
| 4172 | return 0; |
| 4173 | if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) |
| 4174 | return 0; |
David Disseldorp | b49d6f7 | 2018-11-07 14:11:07 +0100 | [diff] [blame] | 4175 | if (!dev->dev_attrib.emulate_pr) |
| 4176 | return 0; |
Bodo Stroesser | 69088a0 | 2020-04-27 17:08:22 +0200 | [diff] [blame] | 4177 | if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 4178 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4179 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 4180 | spin_lock(&dev->dev_reservation_lock); |
| 4181 | if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) |
| 4182 | ret = target_scsi2_reservation_check(cmd); |
| 4183 | else |
| 4184 | ret = target_scsi3_pr_reservation_check(cmd); |
| 4185 | spin_unlock(&dev->dev_reservation_lock); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 4186 | |
Christoph Hellwig | d977f43 | 2012-10-10 17:37:15 -0400 | [diff] [blame] | 4187 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4188 | } |