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