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