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