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