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