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