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 | ||
c66ac9db NB |
1930 | static int __core_scsi3_write_aptpl_to_file( |
1931 | struct se_device *dev, | |
4dee96fb | 1932 | unsigned char *buf) |
c66ac9db | 1933 | { |
0fd97ccf | 1934 | struct t10_wwn *wwn = &dev->t10_wwn; |
c66ac9db | 1935 | struct file *file; |
c66ac9db NB |
1936 | int flags = O_RDWR | O_CREAT | O_TRUNC; |
1937 | char path[512]; | |
4dee96fb | 1938 | u32 pr_aptpl_buf_len; |
c66ac9db NB |
1939 | int ret; |
1940 | ||
c66ac9db NB |
1941 | memset(path, 0, 512); |
1942 | ||
60d645a4 | 1943 | if (strlen(&wwn->unit_serial[0]) >= 512) { |
6708bb27 | 1944 | pr_err("WWN value for struct se_device does not fit" |
c66ac9db | 1945 | " into path buffer\n"); |
e3d6f909 | 1946 | return -EMSGSIZE; |
c66ac9db NB |
1947 | } |
1948 | ||
1949 | snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]); | |
1950 | file = filp_open(path, flags, 0600); | |
0e9b10a9 | 1951 | if (IS_ERR(file)) { |
6708bb27 | 1952 | pr_err("filp_open(%s) for APTPL metadata" |
c66ac9db | 1953 | " failed\n", path); |
0e9b10a9 | 1954 | return PTR_ERR(file); |
c66ac9db NB |
1955 | } |
1956 | ||
4dee96fb | 1957 | pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */ |
c66ac9db | 1958 | |
0e9b10a9 | 1959 | ret = kernel_write(file, buf, pr_aptpl_buf_len, 0); |
c66ac9db | 1960 | |
0e9b10a9 | 1961 | if (ret < 0) |
6708bb27 | 1962 | pr_debug("Error writing APTPL metadata file: %s\n", path); |
0e9b10a9 | 1963 | fput(file); |
c66ac9db | 1964 | |
0e9b10a9 | 1965 | return ret ? -EIO : 0; |
c66ac9db NB |
1966 | } |
1967 | ||
de103c93 CH |
1968 | static int |
1969 | core_scsi3_update_and_write_aptpl(struct se_device *dev, unsigned char *in_buf, | |
1970 | u32 in_pr_aptpl_buf_len) | |
c66ac9db NB |
1971 | { |
1972 | unsigned char null_buf[64], *buf; | |
de103c93 CH |
1973 | int ret; |
1974 | ||
c66ac9db NB |
1975 | /* |
1976 | * Can be called with a NULL pointer from PROUT service action CLEAR | |
1977 | */ | |
6708bb27 | 1978 | if (!in_buf) { |
0607decd AG |
1979 | snprintf(null_buf, 64, "No Registrations or Reservations\n"); |
1980 | buf = null_buf; | |
c66ac9db | 1981 | } else { |
0607decd AG |
1982 | ret = core_scsi3_update_aptpl_buf(dev, in_buf, in_pr_aptpl_buf_len); |
1983 | if (ret != 0) | |
1984 | return ret; | |
c66ac9db | 1985 | buf = in_buf; |
c66ac9db NB |
1986 | } |
1987 | ||
c66ac9db NB |
1988 | /* |
1989 | * __core_scsi3_write_aptpl_to_file() will call strlen() | |
1990 | * on the passed buf to determine pr_aptpl_buf_len. | |
1991 | */ | |
4dee96fb | 1992 | return __core_scsi3_write_aptpl_to_file(dev, buf); |
c66ac9db NB |
1993 | } |
1994 | ||
de103c93 CH |
1995 | static sense_reason_t |
1996 | core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key, | |
33ce6a87 | 1997 | int aptpl, int all_tg_pt, int spec_i_pt, enum register_type register_type) |
c66ac9db | 1998 | { |
e3d6f909 | 1999 | struct se_session *se_sess = cmd->se_sess; |
5951146d | 2000 | struct se_device *dev = cmd->se_dev; |
c66ac9db | 2001 | struct se_dev_entry *se_deve; |
e3d6f909 | 2002 | struct se_lun *se_lun = cmd->se_lun; |
c66ac9db NB |
2003 | struct se_portal_group *se_tpg; |
2004 | struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e; | |
0fd97ccf | 2005 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
c66ac9db NB |
2006 | /* Used for APTPL metadata w/ UNREGISTER */ |
2007 | unsigned char *pr_aptpl_buf = NULL; | |
2008 | unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL; | |
a0d50f62 | 2009 | sense_reason_t ret = TCM_NO_SENSE; |
de103c93 | 2010 | int pr_holder = 0, type; |
c66ac9db | 2011 | |
6708bb27 AG |
2012 | if (!se_sess || !se_lun) { |
2013 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); | |
de103c93 | 2014 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
2015 | } |
2016 | se_tpg = se_sess->se_tpg; | |
f2083241 | 2017 | se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun]; |
c66ac9db | 2018 | |
e3d6f909 | 2019 | if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) { |
c66ac9db | 2020 | memset(&isid_buf[0], 0, PR_REG_ISID_LEN); |
e3d6f909 | 2021 | se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0], |
c66ac9db NB |
2022 | PR_REG_ISID_LEN); |
2023 | isid_ptr = &isid_buf[0]; | |
2024 | } | |
2025 | /* | |
2026 | * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47 | |
2027 | */ | |
2028 | pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); | |
6708bb27 | 2029 | if (!pr_reg_e) { |
c66ac9db | 2030 | if (res_key) { |
6708bb27 | 2031 | pr_warn("SPC-3 PR: Reservation Key non-zero" |
c66ac9db | 2032 | " for SA REGISTER, returning CONFLICT\n"); |
de103c93 | 2033 | return TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
2034 | } |
2035 | /* | |
2036 | * Do nothing but return GOOD status. | |
2037 | */ | |
6708bb27 | 2038 | if (!sa_res_key) |
03e98c9e | 2039 | return 0; |
c66ac9db | 2040 | |
6708bb27 | 2041 | if (!spec_i_pt) { |
c66ac9db NB |
2042 | /* |
2043 | * Perform the Service Action REGISTER on the Initiator | |
2044 | * Port Endpoint that the PRO was received from on the | |
2045 | * Logical Unit of the SCSI device server. | |
2046 | */ | |
de103c93 | 2047 | if (core_scsi3_alloc_registration(cmd->se_dev, |
c66ac9db NB |
2048 | se_sess->se_node_acl, se_deve, isid_ptr, |
2049 | sa_res_key, all_tg_pt, aptpl, | |
33ce6a87 | 2050 | register_type, 0)) { |
6708bb27 | 2051 | pr_err("Unable to allocate" |
c66ac9db | 2052 | " struct t10_pr_registration\n"); |
de103c93 | 2053 | return TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
2054 | } |
2055 | } else { | |
2056 | /* | |
2057 | * Register both the Initiator port that received | |
2058 | * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI | |
2059 | * TransportID from Parameter list and loop through | |
2060 | * fabric dependent parameter list while calling | |
2061 | * logic from of core_scsi3_alloc_registration() for | |
2062 | * each TransportID provided SCSI Initiator Port/Device | |
2063 | */ | |
2064 | ret = core_scsi3_decode_spec_i_port(cmd, se_tpg, | |
2065 | isid_ptr, sa_res_key, all_tg_pt, aptpl); | |
2066 | if (ret != 0) | |
2067 | return ret; | |
2068 | } | |
2069 | /* | |
2070 | * Nothing left to do for the APTPL=0 case. | |
2071 | */ | |
6708bb27 | 2072 | if (!aptpl) { |
c66ac9db | 2073 | pr_tmpl->pr_aptpl_active = 0; |
5951146d | 2074 | core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0); |
6708bb27 | 2075 | pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for" |
c66ac9db NB |
2076 | " REGISTER\n"); |
2077 | return 0; | |
2078 | } | |
2079 | /* | |
2080 | * Locate the newly allocated local I_T Nexus *pr_reg, and | |
2081 | * update the APTPL metadata information using its | |
2082 | * preallocated *pr_reg->pr_aptpl_buf. | |
2083 | */ | |
5951146d | 2084 | pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, |
c66ac9db NB |
2085 | se_sess->se_node_acl, se_sess); |
2086 | ||
de103c93 | 2087 | if (core_scsi3_update_and_write_aptpl(cmd->se_dev, |
4e529be2 | 2088 | pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
c66ac9db | 2089 | pr_tmpl->pr_aptpl_active = 1; |
6708bb27 | 2090 | pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n"); |
c66ac9db NB |
2091 | } |
2092 | ||
de103c93 CH |
2093 | goto out_put_pr_reg; |
2094 | } | |
2095 | ||
2096 | /* | |
2097 | * Locate the existing *pr_reg via struct se_node_acl pointers | |
2098 | */ | |
2099 | pr_reg = pr_reg_e; | |
2100 | type = pr_reg->pr_res_type; | |
2101 | ||
33ce6a87 | 2102 | if (register_type == REGISTER) { |
de103c93 CH |
2103 | if (res_key != pr_reg->pr_res_key) { |
2104 | pr_err("SPC-3 PR REGISTER: Received" | |
2105 | " res_key: 0x%016Lx does not match" | |
2106 | " existing SA REGISTER res_key:" | |
2107 | " 0x%016Lx\n", res_key, | |
2108 | pr_reg->pr_res_key); | |
2109 | ret = TCM_RESERVATION_CONFLICT; | |
2110 | goto out_put_pr_reg; | |
c66ac9db | 2111 | } |
de103c93 CH |
2112 | } |
2113 | ||
2114 | if (spec_i_pt) { | |
1f070cc2 AG |
2115 | pr_err("SPC-3 PR REGISTER: SPEC_I_PT" |
2116 | " set on a registered nexus\n"); | |
de103c93 CH |
2117 | ret = TCM_INVALID_PARAMETER_LIST; |
2118 | goto out_put_pr_reg; | |
2119 | } | |
2120 | ||
2121 | /* | |
2122 | * An existing ALL_TG_PT=1 registration being released | |
2123 | * must also set ALL_TG_PT=1 in the incoming PROUT. | |
2124 | */ | |
1f070cc2 AG |
2125 | if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) { |
2126 | pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1" | |
de103c93 CH |
2127 | " registration exists, but ALL_TG_PT=1 bit not" |
2128 | " present in received PROUT\n"); | |
2129 | ret = TCM_INVALID_CDB_FIELD; | |
2130 | goto out_put_pr_reg; | |
2131 | } | |
2132 | ||
2133 | /* | |
2134 | * Allocate APTPL metadata buffer used for UNREGISTER ops | |
2135 | */ | |
2136 | if (aptpl) { | |
4e529be2 | 2137 | pr_aptpl_buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL); |
de103c93 CH |
2138 | if (!pr_aptpl_buf) { |
2139 | pr_err("Unable to allocate" | |
2140 | " pr_aptpl_buf\n"); | |
2141 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
2142 | goto out_put_pr_reg; | |
c66ac9db | 2143 | } |
de103c93 CH |
2144 | } |
2145 | ||
2146 | /* | |
2147 | * sa_res_key=0 Unregister Reservation Key for registered I_T | |
2148 | * Nexus sa_res_key=1 Change Reservation Key for registered I_T | |
2149 | * Nexus. | |
2150 | */ | |
2151 | if (!sa_res_key) { | |
2152 | pr_holder = core_scsi3_check_implict_release( | |
2153 | cmd->se_dev, pr_reg); | |
2154 | if (pr_holder < 0) { | |
2155 | kfree(pr_aptpl_buf); | |
2156 | ret = TCM_RESERVATION_CONFLICT; | |
2157 | goto out_put_pr_reg; | |
c66ac9db | 2158 | } |
de103c93 CH |
2159 | |
2160 | spin_lock(&pr_tmpl->registration_lock); | |
c66ac9db | 2161 | /* |
de103c93 CH |
2162 | * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port |
2163 | * and matching pr_res_key. | |
c66ac9db | 2164 | */ |
de103c93 CH |
2165 | if (pr_reg->pr_reg_all_tg_pt) { |
2166 | list_for_each_entry_safe(pr_reg_p, pr_reg_tmp, | |
2167 | &pr_tmpl->registration_list, | |
2168 | pr_reg_list) { | |
2169 | ||
2170 | if (!pr_reg_p->pr_reg_all_tg_pt) | |
2171 | continue; | |
2172 | if (pr_reg_p->pr_res_key != res_key) | |
2173 | continue; | |
2174 | if (pr_reg == pr_reg_p) | |
2175 | continue; | |
2176 | if (strcmp(pr_reg->pr_reg_nacl->initiatorname, | |
2177 | pr_reg_p->pr_reg_nacl->initiatorname)) | |
2178 | continue; | |
2179 | ||
2180 | __core_scsi3_free_registration(dev, | |
2181 | pr_reg_p, NULL, 0); | |
c66ac9db NB |
2182 | } |
2183 | } | |
de103c93 | 2184 | |
c66ac9db | 2185 | /* |
de103c93 | 2186 | * Release the calling I_T Nexus registration now.. |
c66ac9db | 2187 | */ |
de103c93 | 2188 | __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1); |
c66ac9db | 2189 | |
de103c93 CH |
2190 | /* |
2191 | * From spc4r17, section 5.7.11.3 Unregistering | |
2192 | * | |
2193 | * If the persistent reservation is a registrants only | |
2194 | * type, the device server shall establish a unit | |
2195 | * attention condition for the initiator port associated | |
2196 | * with every registered I_T nexus except for the I_T | |
2197 | * nexus on which the PERSISTENT RESERVE OUT command was | |
2198 | * received, with the additional sense code set to | |
2199 | * RESERVATIONS RELEASED. | |
2200 | */ | |
2201 | if (pr_holder && | |
2202 | (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY || | |
2203 | type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) { | |
2204 | list_for_each_entry(pr_reg_p, | |
2205 | &pr_tmpl->registration_list, | |
2206 | pr_reg_list) { | |
2207 | ||
2208 | core_scsi3_ua_allocate( | |
2209 | pr_reg_p->pr_reg_nacl, | |
2210 | pr_reg_p->pr_res_mapped_lun, | |
2211 | 0x2A, | |
2212 | ASCQ_2AH_RESERVATIONS_RELEASED); | |
c66ac9db | 2213 | } |
de103c93 CH |
2214 | } |
2215 | spin_unlock(&pr_tmpl->registration_lock); | |
c66ac9db | 2216 | |
de103c93 CH |
2217 | if (!aptpl) { |
2218 | pr_tmpl->pr_aptpl_active = 0; | |
2219 | core_scsi3_update_and_write_aptpl(dev, NULL, 0); | |
2220 | pr_debug("SPC-3 PR: Set APTPL Bit Deactivated" | |
2221 | " for UNREGISTER\n"); | |
2222 | return 0; | |
2223 | } | |
c66ac9db | 2224 | |
4e529be2 | 2225 | if (!core_scsi3_update_and_write_aptpl(dev, pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
de103c93 CH |
2226 | pr_tmpl->pr_aptpl_active = 1; |
2227 | pr_debug("SPC-3 PR: Set APTPL Bit Activated" | |
2228 | " for UNREGISTER\n"); | |
2229 | } | |
c66ac9db | 2230 | |
de103c93 CH |
2231 | goto out_free_aptpl_buf; |
2232 | } | |
c66ac9db | 2233 | |
de103c93 CH |
2234 | /* |
2235 | * Increment PRgeneration counter for struct se_device" | |
2236 | * upon a successful REGISTER, see spc4r17 section 6.3.2 | |
2237 | * READ_KEYS service action. | |
2238 | */ | |
2239 | pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev); | |
2240 | pr_reg->pr_res_key = sa_res_key; | |
2241 | pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation" | |
2242 | " Key for %s to: 0x%016Lx PRgeneration:" | |
2243 | " 0x%08x\n", cmd->se_tfo->get_fabric_name(), | |
33ce6a87 | 2244 | (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "", |
de103c93 CH |
2245 | pr_reg->pr_reg_nacl->initiatorname, |
2246 | pr_reg->pr_res_key, pr_reg->pr_res_generation); | |
c66ac9db | 2247 | |
de103c93 CH |
2248 | if (!aptpl) { |
2249 | pr_tmpl->pr_aptpl_active = 0; | |
2250 | core_scsi3_update_and_write_aptpl(dev, NULL, 0); | |
2251 | pr_debug("SPC-3 PR: Set APTPL Bit Deactivated" | |
2252 | " for REGISTER\n"); | |
2253 | ret = 0; | |
2254 | goto out_put_pr_reg; | |
c66ac9db | 2255 | } |
de103c93 | 2256 | |
4e529be2 | 2257 | if (!core_scsi3_update_and_write_aptpl(dev, pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
de103c93 CH |
2258 | pr_tmpl->pr_aptpl_active = 1; |
2259 | pr_debug("SPC-3 PR: Set APTPL Bit Activated" | |
2260 | " for REGISTER\n"); | |
2261 | } | |
2262 | ||
2263 | out_free_aptpl_buf: | |
2264 | kfree(pr_aptpl_buf); | |
2265 | ret = 0; | |
2266 | out_put_pr_reg: | |
2267 | core_scsi3_put_pr_reg(pr_reg); | |
2268 | return ret; | |
c66ac9db NB |
2269 | } |
2270 | ||
2271 | unsigned char *core_scsi3_pr_dump_type(int type) | |
2272 | { | |
2273 | switch (type) { | |
2274 | case PR_TYPE_WRITE_EXCLUSIVE: | |
2275 | return "Write Exclusive Access"; | |
2276 | case PR_TYPE_EXCLUSIVE_ACCESS: | |
2277 | return "Exclusive Access"; | |
2278 | case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: | |
2279 | return "Write Exclusive Access, Registrants Only"; | |
2280 | case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: | |
2281 | return "Exclusive Access, Registrants Only"; | |
2282 | case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: | |
2283 | return "Write Exclusive Access, All Registrants"; | |
2284 | case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: | |
2285 | return "Exclusive Access, All Registrants"; | |
2286 | default: | |
2287 | break; | |
2288 | } | |
2289 | ||
2290 | return "Unknown SPC-3 PR Type"; | |
2291 | } | |
2292 | ||
de103c93 CH |
2293 | static sense_reason_t |
2294 | core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key) | |
c66ac9db | 2295 | { |
de103c93 | 2296 | struct se_device *dev = cmd->se_dev; |
e3d6f909 | 2297 | struct se_session *se_sess = cmd->se_sess; |
e3d6f909 | 2298 | struct se_lun *se_lun = cmd->se_lun; |
c66ac9db | 2299 | struct t10_pr_registration *pr_reg, *pr_res_holder; |
0fd97ccf | 2300 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
c66ac9db | 2301 | char i_buf[PR_REG_ISID_ID_LEN]; |
de103c93 | 2302 | sense_reason_t ret; |
c66ac9db NB |
2303 | |
2304 | memset(i_buf, 0, PR_REG_ISID_ID_LEN); | |
2305 | ||
6708bb27 AG |
2306 | if (!se_sess || !se_lun) { |
2307 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); | |
de103c93 | 2308 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db | 2309 | } |
c66ac9db NB |
2310 | /* |
2311 | * Locate the existing *pr_reg via struct se_node_acl pointers | |
2312 | */ | |
5951146d | 2313 | pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, |
c66ac9db | 2314 | se_sess); |
6708bb27 AG |
2315 | if (!pr_reg) { |
2316 | pr_err("SPC-3 PR: Unable to locate" | |
c66ac9db | 2317 | " PR_REGISTERED *pr_reg for RESERVE\n"); |
de103c93 | 2318 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
2319 | } |
2320 | /* | |
2321 | * From spc4r17 Section 5.7.9: Reserving: | |
2322 | * | |
2323 | * An application client creates a persistent reservation by issuing | |
2324 | * a PERSISTENT RESERVE OUT command with RESERVE service action through | |
2325 | * a registered I_T nexus with the following parameters: | |
2326 | * a) RESERVATION KEY set to the value of the reservation key that is | |
2327 | * registered with the logical unit for the I_T nexus; and | |
2328 | */ | |
2329 | if (res_key != pr_reg->pr_res_key) { | |
6708bb27 | 2330 | pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx" |
c66ac9db NB |
2331 | " does not match existing SA REGISTER res_key:" |
2332 | " 0x%016Lx\n", res_key, pr_reg->pr_res_key); | |
de103c93 CH |
2333 | ret = TCM_RESERVATION_CONFLICT; |
2334 | goto out_put_pr_reg; | |
c66ac9db NB |
2335 | } |
2336 | /* | |
2337 | * From spc4r17 Section 5.7.9: Reserving: | |
2338 | * | |
2339 | * From above: | |
2340 | * b) TYPE field and SCOPE field set to the persistent reservation | |
2341 | * being created. | |
2342 | * | |
2343 | * Only one persistent reservation is allowed at a time per logical unit | |
2344 | * and that persistent reservation has a scope of LU_SCOPE. | |
2345 | */ | |
2346 | if (scope != PR_SCOPE_LU_SCOPE) { | |
6708bb27 | 2347 | pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); |
de103c93 CH |
2348 | ret = TCM_INVALID_PARAMETER_LIST; |
2349 | goto out_put_pr_reg; | |
c66ac9db NB |
2350 | } |
2351 | /* | |
2352 | * See if we have an existing PR reservation holder pointer at | |
2353 | * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration | |
2354 | * *pr_res_holder. | |
2355 | */ | |
2356 | spin_lock(&dev->dev_reservation_lock); | |
2357 | pr_res_holder = dev->dev_pr_res_holder; | |
ee1b1b9c | 2358 | if (pr_res_holder) { |
c66ac9db NB |
2359 | /* |
2360 | * From spc4r17 Section 5.7.9: Reserving: | |
2361 | * | |
2362 | * If the device server receives a PERSISTENT RESERVE OUT | |
2363 | * command from an I_T nexus other than a persistent reservation | |
2364 | * holder (see 5.7.10) that attempts to create a persistent | |
2365 | * reservation when a persistent reservation already exists for | |
2366 | * the logical unit, then the command shall be completed with | |
2367 | * RESERVATION CONFLICT status. | |
2368 | */ | |
2369 | if (pr_res_holder != pr_reg) { | |
2370 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; | |
6708bb27 | 2371 | pr_err("SPC-3 PR: Attempted RESERVE from" |
c66ac9db NB |
2372 | " [%s]: %s while reservation already held by" |
2373 | " [%s]: %s, returning RESERVATION_CONFLICT\n", | |
e3d6f909 | 2374 | cmd->se_tfo->get_fabric_name(), |
c66ac9db | 2375 | se_sess->se_node_acl->initiatorname, |
e3d6f909 | 2376 | pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db NB |
2377 | pr_res_holder->pr_reg_nacl->initiatorname); |
2378 | ||
2379 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 CH |
2380 | ret = TCM_RESERVATION_CONFLICT; |
2381 | goto out_put_pr_reg; | |
c66ac9db NB |
2382 | } |
2383 | /* | |
2384 | * From spc4r17 Section 5.7.9: Reserving: | |
2385 | * | |
2386 | * If a persistent reservation holder attempts to modify the | |
2387 | * type or scope of an existing persistent reservation, the | |
2388 | * command shall be completed with RESERVATION CONFLICT status. | |
2389 | */ | |
2390 | if ((pr_res_holder->pr_res_type != type) || | |
2391 | (pr_res_holder->pr_res_scope != scope)) { | |
2392 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; | |
6708bb27 | 2393 | pr_err("SPC-3 PR: Attempted RESERVE from" |
c66ac9db NB |
2394 | " [%s]: %s trying to change TYPE and/or SCOPE," |
2395 | " while reservation already held by [%s]: %s," | |
2396 | " returning RESERVATION_CONFLICT\n", | |
e3d6f909 | 2397 | cmd->se_tfo->get_fabric_name(), |
c66ac9db | 2398 | se_sess->se_node_acl->initiatorname, |
e3d6f909 | 2399 | pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db NB |
2400 | pr_res_holder->pr_reg_nacl->initiatorname); |
2401 | ||
2402 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 CH |
2403 | ret = TCM_RESERVATION_CONFLICT; |
2404 | goto out_put_pr_reg; | |
c66ac9db NB |
2405 | } |
2406 | /* | |
2407 | * From spc4r17 Section 5.7.9: Reserving: | |
2408 | * | |
2409 | * If the device server receives a PERSISTENT RESERVE OUT | |
2410 | * command with RESERVE service action where the TYPE field and | |
2411 | * the SCOPE field contain the same values as the existing type | |
2412 | * and scope from a persistent reservation holder, it shall not | |
2413 | * make any change to the existing persistent reservation and | |
2414 | * shall completethe command with GOOD status. | |
2415 | */ | |
2416 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 CH |
2417 | ret = 0; |
2418 | goto out_put_pr_reg; | |
c66ac9db NB |
2419 | } |
2420 | /* | |
2421 | * Otherwise, our *pr_reg becomes the PR reservation holder for said | |
2422 | * TYPE/SCOPE. Also set the received scope and type in *pr_reg. | |
2423 | */ | |
2424 | pr_reg->pr_res_scope = scope; | |
2425 | pr_reg->pr_res_type = type; | |
2426 | pr_reg->pr_res_holder = 1; | |
2427 | dev->dev_pr_res_holder = pr_reg; | |
d2843c17 | 2428 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
c66ac9db | 2429 | |
6708bb27 | 2430 | pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new" |
c66ac9db | 2431 | " reservation holder TYPE: %s ALL_TG_PT: %d\n", |
e3d6f909 | 2432 | cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type), |
c66ac9db | 2433 | (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); |
6708bb27 | 2434 | pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", |
e3d6f909 | 2435 | cmd->se_tfo->get_fabric_name(), |
c66ac9db | 2436 | se_sess->se_node_acl->initiatorname, |
d2843c17 | 2437 | i_buf); |
c66ac9db NB |
2438 | spin_unlock(&dev->dev_reservation_lock); |
2439 | ||
2440 | if (pr_tmpl->pr_aptpl_active) { | |
de103c93 | 2441 | if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, |
4e529be2 | 2442 | pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
6708bb27 | 2443 | pr_debug("SPC-3 PR: Updated APTPL metadata" |
c66ac9db | 2444 | " for RESERVE\n"); |
de103c93 | 2445 | } |
c66ac9db NB |
2446 | } |
2447 | ||
de103c93 CH |
2448 | ret = 0; |
2449 | out_put_pr_reg: | |
c66ac9db | 2450 | core_scsi3_put_pr_reg(pr_reg); |
de103c93 | 2451 | return ret; |
c66ac9db NB |
2452 | } |
2453 | ||
de103c93 CH |
2454 | static sense_reason_t |
2455 | core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope, | |
2456 | u64 res_key) | |
c66ac9db | 2457 | { |
c66ac9db NB |
2458 | switch (type) { |
2459 | case PR_TYPE_WRITE_EXCLUSIVE: | |
2460 | case PR_TYPE_EXCLUSIVE_ACCESS: | |
2461 | case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: | |
2462 | case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: | |
2463 | case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: | |
2464 | case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: | |
de103c93 | 2465 | return core_scsi3_pro_reserve(cmd, type, scope, res_key); |
c66ac9db | 2466 | default: |
6708bb27 | 2467 | pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:" |
c66ac9db | 2468 | " 0x%02x\n", type); |
de103c93 | 2469 | return TCM_INVALID_CDB_FIELD; |
c66ac9db | 2470 | } |
c66ac9db NB |
2471 | } |
2472 | ||
2473 | /* | |
2474 | * Called with struct se_device->dev_reservation_lock held. | |
2475 | */ | |
2476 | static void __core_scsi3_complete_pro_release( | |
2477 | struct se_device *dev, | |
2478 | struct se_node_acl *se_nacl, | |
2479 | struct t10_pr_registration *pr_reg, | |
2480 | int explict) | |
2481 | { | |
2482 | struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo; | |
2483 | char i_buf[PR_REG_ISID_ID_LEN]; | |
c66ac9db NB |
2484 | |
2485 | memset(i_buf, 0, PR_REG_ISID_ID_LEN); | |
d2843c17 | 2486 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
c66ac9db NB |
2487 | /* |
2488 | * Go ahead and release the current PR reservation holder. | |
2489 | */ | |
2490 | dev->dev_pr_res_holder = NULL; | |
2491 | ||
6708bb27 | 2492 | pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared" |
c66ac9db NB |
2493 | " reservation holder TYPE: %s ALL_TG_PT: %d\n", |
2494 | tfo->get_fabric_name(), (explict) ? "explict" : "implict", | |
2495 | core_scsi3_pr_dump_type(pr_reg->pr_res_type), | |
2496 | (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); | |
6708bb27 | 2497 | pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n", |
c66ac9db | 2498 | tfo->get_fabric_name(), se_nacl->initiatorname, |
d2843c17 | 2499 | i_buf); |
c66ac9db NB |
2500 | /* |
2501 | * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE | |
2502 | */ | |
2503 | pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0; | |
2504 | } | |
2505 | ||
de103c93 CH |
2506 | static sense_reason_t |
2507 | core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope, | |
2508 | u64 res_key) | |
c66ac9db NB |
2509 | { |
2510 | struct se_device *dev = cmd->se_dev; | |
e3d6f909 AG |
2511 | struct se_session *se_sess = cmd->se_sess; |
2512 | struct se_lun *se_lun = cmd->se_lun; | |
c66ac9db | 2513 | struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder; |
0fd97ccf | 2514 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
de103c93 CH |
2515 | int all_reg = 0; |
2516 | sense_reason_t ret = 0; | |
c66ac9db | 2517 | |
6708bb27 AG |
2518 | if (!se_sess || !se_lun) { |
2519 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); | |
de103c93 | 2520 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
2521 | } |
2522 | /* | |
2523 | * Locate the existing *pr_reg via struct se_node_acl pointers | |
2524 | */ | |
2525 | pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); | |
6708bb27 AG |
2526 | if (!pr_reg) { |
2527 | pr_err("SPC-3 PR: Unable to locate" | |
c66ac9db | 2528 | " PR_REGISTERED *pr_reg for RELEASE\n"); |
de103c93 | 2529 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
2530 | } |
2531 | /* | |
2532 | * From spc4r17 Section 5.7.11.2 Releasing: | |
2533 | * | |
2534 | * If there is no persistent reservation or in response to a persistent | |
2535 | * reservation release request from a registered I_T nexus that is not a | |
2536 | * persistent reservation holder (see 5.7.10), the device server shall | |
2537 | * do the following: | |
2538 | * | |
2539 | * a) Not release the persistent reservation, if any; | |
2540 | * b) Not remove any registrations; and | |
2541 | * c) Complete the command with GOOD status. | |
2542 | */ | |
2543 | spin_lock(&dev->dev_reservation_lock); | |
2544 | pr_res_holder = dev->dev_pr_res_holder; | |
6708bb27 | 2545 | if (!pr_res_holder) { |
c66ac9db NB |
2546 | /* |
2547 | * No persistent reservation, return GOOD status. | |
2548 | */ | |
2549 | spin_unlock(&dev->dev_reservation_lock); | |
bb7a8c8e | 2550 | goto out_put_pr_reg; |
c66ac9db NB |
2551 | } |
2552 | if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || | |
2553 | (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) | |
2554 | all_reg = 1; | |
2555 | ||
2556 | if ((all_reg == 0) && (pr_res_holder != pr_reg)) { | |
2557 | /* | |
2558 | * Non 'All Registrants' PR Type cases.. | |
2559 | * Release request from a registered I_T nexus that is not a | |
2560 | * persistent reservation holder. return GOOD status. | |
2561 | */ | |
2562 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 | 2563 | goto out_put_pr_reg; |
c66ac9db | 2564 | } |
de103c93 | 2565 | |
c66ac9db NB |
2566 | /* |
2567 | * From spc4r17 Section 5.7.11.2 Releasing: | |
2568 | * | |
2569 | * Only the persistent reservation holder (see 5.7.10) is allowed to | |
2570 | * release a persistent reservation. | |
2571 | * | |
2572 | * An application client releases the persistent reservation by issuing | |
2573 | * a PERSISTENT RESERVE OUT command with RELEASE service action through | |
2574 | * an I_T nexus that is a persistent reservation holder with the | |
2575 | * following parameters: | |
2576 | * | |
2577 | * a) RESERVATION KEY field set to the value of the reservation key | |
2578 | * that is registered with the logical unit for the I_T nexus; | |
2579 | */ | |
2580 | if (res_key != pr_reg->pr_res_key) { | |
6708bb27 | 2581 | pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx" |
c66ac9db NB |
2582 | " does not match existing SA REGISTER res_key:" |
2583 | " 0x%016Lx\n", res_key, pr_reg->pr_res_key); | |
2584 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 CH |
2585 | ret = TCM_RESERVATION_CONFLICT; |
2586 | goto out_put_pr_reg; | |
c66ac9db NB |
2587 | } |
2588 | /* | |
2589 | * From spc4r17 Section 5.7.11.2 Releasing and above: | |
2590 | * | |
2591 | * b) TYPE field and SCOPE field set to match the persistent | |
2592 | * reservation being released. | |
2593 | */ | |
2594 | if ((pr_res_holder->pr_res_type != type) || | |
2595 | (pr_res_holder->pr_res_scope != scope)) { | |
2596 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; | |
6708bb27 | 2597 | pr_err("SPC-3 PR RELEASE: Attempted to release" |
c66ac9db NB |
2598 | " reservation from [%s]: %s with different TYPE " |
2599 | "and/or SCOPE while reservation already held by" | |
2600 | " [%s]: %s, returning RESERVATION_CONFLICT\n", | |
e3d6f909 | 2601 | cmd->se_tfo->get_fabric_name(), |
c66ac9db | 2602 | se_sess->se_node_acl->initiatorname, |
e3d6f909 | 2603 | pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db NB |
2604 | pr_res_holder->pr_reg_nacl->initiatorname); |
2605 | ||
2606 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 CH |
2607 | ret = TCM_RESERVATION_CONFLICT; |
2608 | goto out_put_pr_reg; | |
c66ac9db NB |
2609 | } |
2610 | /* | |
2611 | * In response to a persistent reservation release request from the | |
2612 | * persistent reservation holder the device server shall perform a | |
2613 | * release by doing the following as an uninterrupted series of actions: | |
2614 | * a) Release the persistent reservation; | |
2615 | * b) Not remove any registration(s); | |
2616 | * c) If the released persistent reservation is a registrants only type | |
2617 | * or all registrants type persistent reservation, | |
2618 | * the device server shall establish a unit attention condition for | |
2619 | * the initiator port associated with every regis- | |
2620 | * tered I_T nexus other than I_T nexus on which the PERSISTENT | |
2621 | * RESERVE OUT command with RELEASE service action was received, | |
2622 | * with the additional sense code set to RESERVATIONS RELEASED; and | |
2623 | * d) If the persistent reservation is of any other type, the device | |
2624 | * server shall not establish a unit attention condition. | |
2625 | */ | |
2626 | __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl, | |
2627 | pr_reg, 1); | |
2628 | ||
2629 | spin_unlock(&dev->dev_reservation_lock); | |
2630 | ||
2631 | if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) && | |
2632 | (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) && | |
2633 | (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) && | |
2634 | (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { | |
2635 | /* | |
2636 | * If no UNIT ATTENTION conditions will be established for | |
2637 | * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS | |
2638 | * go ahead and check for APTPL=1 update+write below | |
2639 | */ | |
2640 | goto write_aptpl; | |
2641 | } | |
2642 | ||
2643 | spin_lock(&pr_tmpl->registration_lock); | |
2644 | list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list, | |
2645 | pr_reg_list) { | |
2646 | /* | |
2647 | * Do not establish a UNIT ATTENTION condition | |
2648 | * for the calling I_T Nexus | |
2649 | */ | |
2650 | if (pr_reg_p == pr_reg) | |
2651 | continue; | |
2652 | ||
2653 | core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl, | |
2654 | pr_reg_p->pr_res_mapped_lun, | |
2655 | 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED); | |
2656 | } | |
2657 | spin_unlock(&pr_tmpl->registration_lock); | |
2658 | ||
2659 | write_aptpl: | |
2660 | if (pr_tmpl->pr_aptpl_active) { | |
de103c93 | 2661 | if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, |
4e529be2 | 2662 | pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
6708bb27 | 2663 | pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n"); |
de103c93 | 2664 | } |
c66ac9db | 2665 | } |
de103c93 | 2666 | out_put_pr_reg: |
c66ac9db | 2667 | core_scsi3_put_pr_reg(pr_reg); |
de103c93 | 2668 | return ret; |
c66ac9db NB |
2669 | } |
2670 | ||
de103c93 CH |
2671 | static sense_reason_t |
2672 | core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key) | |
c66ac9db NB |
2673 | { |
2674 | struct se_device *dev = cmd->se_dev; | |
2675 | struct se_node_acl *pr_reg_nacl; | |
e3d6f909 | 2676 | struct se_session *se_sess = cmd->se_sess; |
0fd97ccf | 2677 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
c66ac9db NB |
2678 | struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; |
2679 | u32 pr_res_mapped_lun = 0; | |
2680 | int calling_it_nexus = 0; | |
2681 | /* | |
2682 | * Locate the existing *pr_reg via struct se_node_acl pointers | |
2683 | */ | |
5951146d | 2684 | pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, |
c66ac9db | 2685 | se_sess->se_node_acl, se_sess); |
6708bb27 AG |
2686 | if (!pr_reg_n) { |
2687 | pr_err("SPC-3 PR: Unable to locate" | |
c66ac9db | 2688 | " PR_REGISTERED *pr_reg for CLEAR\n"); |
de103c93 | 2689 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
2690 | } |
2691 | /* | |
2692 | * From spc4r17 section 5.7.11.6, Clearing: | |
2693 | * | |
2694 | * Any application client may release the persistent reservation and | |
2695 | * remove all registrations from a device server by issuing a | |
2696 | * PERSISTENT RESERVE OUT command with CLEAR service action through a | |
2697 | * registered I_T nexus with the following parameter: | |
2698 | * | |
2699 | * a) RESERVATION KEY field set to the value of the reservation key | |
2700 | * that is registered with the logical unit for the I_T nexus. | |
2701 | */ | |
2702 | if (res_key != pr_reg_n->pr_res_key) { | |
6708bb27 | 2703 | pr_err("SPC-3 PR REGISTER: Received" |
c66ac9db NB |
2704 | " res_key: 0x%016Lx does not match" |
2705 | " existing SA REGISTER res_key:" | |
2706 | " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key); | |
2707 | core_scsi3_put_pr_reg(pr_reg_n); | |
de103c93 | 2708 | return TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
2709 | } |
2710 | /* | |
2711 | * a) Release the persistent reservation, if any; | |
2712 | */ | |
2713 | spin_lock(&dev->dev_reservation_lock); | |
2714 | pr_res_holder = dev->dev_pr_res_holder; | |
2715 | if (pr_res_holder) { | |
2716 | struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; | |
2717 | __core_scsi3_complete_pro_release(dev, pr_res_nacl, | |
2718 | pr_res_holder, 0); | |
2719 | } | |
2720 | spin_unlock(&dev->dev_reservation_lock); | |
2721 | /* | |
2722 | * b) Remove all registration(s) (see spc4r17 5.7.7); | |
2723 | */ | |
2724 | spin_lock(&pr_tmpl->registration_lock); | |
2725 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, | |
2726 | &pr_tmpl->registration_list, pr_reg_list) { | |
2727 | ||
2728 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; | |
2729 | pr_reg_nacl = pr_reg->pr_reg_nacl; | |
2730 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; | |
2731 | __core_scsi3_free_registration(dev, pr_reg, NULL, | |
2732 | calling_it_nexus); | |
2733 | /* | |
2734 | * e) Establish a unit attention condition for the initiator | |
2735 | * port associated with every registered I_T nexus other | |
2736 | * than the I_T nexus on which the PERSISTENT RESERVE OUT | |
2737 | * command with CLEAR service action was received, with the | |
2738 | * additional sense code set to RESERVATIONS PREEMPTED. | |
2739 | */ | |
6708bb27 | 2740 | if (!calling_it_nexus) |
c66ac9db NB |
2741 | core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, |
2742 | 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED); | |
2743 | } | |
2744 | spin_unlock(&pr_tmpl->registration_lock); | |
2745 | ||
6708bb27 | 2746 | pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n", |
e3d6f909 | 2747 | cmd->se_tfo->get_fabric_name()); |
c66ac9db NB |
2748 | |
2749 | if (pr_tmpl->pr_aptpl_active) { | |
5951146d | 2750 | core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0); |
6708bb27 | 2751 | pr_debug("SPC-3 PR: Updated APTPL metadata" |
c66ac9db NB |
2752 | " for CLEAR\n"); |
2753 | } | |
2754 | ||
2755 | core_scsi3_pr_generation(dev); | |
2756 | return 0; | |
2757 | } | |
2758 | ||
2759 | /* | |
2760 | * Called with struct se_device->dev_reservation_lock held. | |
2761 | */ | |
2762 | static void __core_scsi3_complete_pro_preempt( | |
2763 | struct se_device *dev, | |
2764 | struct t10_pr_registration *pr_reg, | |
2765 | struct list_head *preempt_and_abort_list, | |
2766 | int type, | |
2767 | int scope, | |
33ce6a87 | 2768 | enum preempt_type preempt_type) |
c66ac9db NB |
2769 | { |
2770 | struct se_node_acl *nacl = pr_reg->pr_reg_nacl; | |
2771 | struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; | |
2772 | char i_buf[PR_REG_ISID_ID_LEN]; | |
c66ac9db NB |
2773 | |
2774 | memset(i_buf, 0, PR_REG_ISID_ID_LEN); | |
d2843c17 | 2775 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
c66ac9db NB |
2776 | /* |
2777 | * Do an implict RELEASE of the existing reservation. | |
2778 | */ | |
2779 | if (dev->dev_pr_res_holder) | |
2780 | __core_scsi3_complete_pro_release(dev, nacl, | |
2781 | dev->dev_pr_res_holder, 0); | |
2782 | ||
2783 | dev->dev_pr_res_holder = pr_reg; | |
2784 | pr_reg->pr_res_holder = 1; | |
2785 | pr_reg->pr_res_type = type; | |
2786 | pr_reg->pr_res_scope = scope; | |
2787 | ||
6708bb27 | 2788 | pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new" |
c66ac9db | 2789 | " reservation holder TYPE: %s ALL_TG_PT: %d\n", |
33ce6a87 | 2790 | tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", |
c66ac9db NB |
2791 | core_scsi3_pr_dump_type(type), |
2792 | (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); | |
6708bb27 | 2793 | pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n", |
33ce6a87 | 2794 | tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", |
d2843c17 | 2795 | nacl->initiatorname, i_buf); |
c66ac9db NB |
2796 | /* |
2797 | * For PREEMPT_AND_ABORT, add the preempting reservation's | |
2798 | * struct t10_pr_registration to the list that will be compared | |
2799 | * against received CDBs.. | |
2800 | */ | |
2801 | if (preempt_and_abort_list) | |
2802 | list_add_tail(&pr_reg->pr_reg_abort_list, | |
2803 | preempt_and_abort_list); | |
2804 | } | |
2805 | ||
2806 | static void core_scsi3_release_preempt_and_abort( | |
2807 | struct list_head *preempt_and_abort_list, | |
2808 | struct t10_pr_registration *pr_reg_holder) | |
2809 | { | |
2810 | struct t10_pr_registration *pr_reg, *pr_reg_tmp; | |
2811 | ||
2812 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list, | |
2813 | pr_reg_abort_list) { | |
2814 | ||
2815 | list_del(&pr_reg->pr_reg_abort_list); | |
2816 | if (pr_reg_holder == pr_reg) | |
2817 | continue; | |
2818 | if (pr_reg->pr_res_holder) { | |
6708bb27 | 2819 | pr_warn("pr_reg->pr_res_holder still set\n"); |
c66ac9db NB |
2820 | continue; |
2821 | } | |
2822 | ||
2823 | pr_reg->pr_reg_deve = NULL; | |
2824 | pr_reg->pr_reg_nacl = NULL; | |
2825 | kfree(pr_reg->pr_aptpl_buf); | |
2826 | kmem_cache_free(t10_pr_reg_cache, pr_reg); | |
2827 | } | |
2828 | } | |
2829 | ||
de103c93 CH |
2830 | static sense_reason_t |
2831 | core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key, | |
33ce6a87 | 2832 | u64 sa_res_key, enum preempt_type preempt_type) |
c66ac9db | 2833 | { |
5951146d | 2834 | struct se_device *dev = cmd->se_dev; |
c66ac9db | 2835 | struct se_node_acl *pr_reg_nacl; |
e3d6f909 | 2836 | struct se_session *se_sess = cmd->se_sess; |
d0f474e5 | 2837 | LIST_HEAD(preempt_and_abort_list); |
c66ac9db | 2838 | struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; |
0fd97ccf | 2839 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
c66ac9db NB |
2840 | u32 pr_res_mapped_lun = 0; |
2841 | int all_reg = 0, calling_it_nexus = 0, released_regs = 0; | |
de103c93 | 2842 | int prh_type = 0, prh_scope = 0; |
c66ac9db | 2843 | |
de103c93 CH |
2844 | if (!se_sess) |
2845 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
c66ac9db | 2846 | |
5951146d | 2847 | pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, |
c66ac9db | 2848 | se_sess); |
6708bb27 AG |
2849 | if (!pr_reg_n) { |
2850 | pr_err("SPC-3 PR: Unable to locate" | |
c66ac9db | 2851 | " PR_REGISTERED *pr_reg for PREEMPT%s\n", |
33ce6a87 | 2852 | (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : ""); |
de103c93 | 2853 | return TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
2854 | } |
2855 | if (pr_reg_n->pr_res_key != res_key) { | |
2856 | core_scsi3_put_pr_reg(pr_reg_n); | |
de103c93 | 2857 | return TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
2858 | } |
2859 | if (scope != PR_SCOPE_LU_SCOPE) { | |
6708bb27 | 2860 | pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); |
c66ac9db | 2861 | core_scsi3_put_pr_reg(pr_reg_n); |
de103c93 | 2862 | return TCM_INVALID_PARAMETER_LIST; |
c66ac9db | 2863 | } |
c66ac9db NB |
2864 | |
2865 | spin_lock(&dev->dev_reservation_lock); | |
2866 | pr_res_holder = dev->dev_pr_res_holder; | |
2867 | if (pr_res_holder && | |
2868 | ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || | |
2869 | (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) | |
2870 | all_reg = 1; | |
2871 | ||
6708bb27 | 2872 | if (!all_reg && !sa_res_key) { |
c66ac9db NB |
2873 | spin_unlock(&dev->dev_reservation_lock); |
2874 | core_scsi3_put_pr_reg(pr_reg_n); | |
de103c93 | 2875 | return TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
2876 | } |
2877 | /* | |
2878 | * From spc4r17, section 5.7.11.4.4 Removing Registrations: | |
2879 | * | |
2880 | * If the SERVICE ACTION RESERVATION KEY field does not identify a | |
2881 | * persistent reservation holder or there is no persistent reservation | |
2882 | * holder (i.e., there is no persistent reservation), then the device | |
2883 | * server shall perform a preempt by doing the following in an | |
2884 | * uninterrupted series of actions. (See below..) | |
2885 | */ | |
6708bb27 | 2886 | if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) { |
c66ac9db NB |
2887 | /* |
2888 | * No existing or SA Reservation Key matching reservations.. | |
2889 | * | |
2890 | * PROUT SA PREEMPT with All Registrant type reservations are | |
2891 | * allowed to be processed without a matching SA Reservation Key | |
2892 | */ | |
2893 | spin_lock(&pr_tmpl->registration_lock); | |
2894 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, | |
2895 | &pr_tmpl->registration_list, pr_reg_list) { | |
2896 | /* | |
2897 | * Removing of registrations in non all registrants | |
2898 | * type reservations without a matching SA reservation | |
2899 | * key. | |
2900 | * | |
2901 | * a) Remove the registrations for all I_T nexuses | |
2902 | * specified by the SERVICE ACTION RESERVATION KEY | |
2903 | * field; | |
2904 | * b) Ignore the contents of the SCOPE and TYPE fields; | |
2905 | * c) Process tasks as defined in 5.7.1; and | |
2906 | * d) Establish a unit attention condition for the | |
2907 | * initiator port associated with every I_T nexus | |
2908 | * that lost its registration other than the I_T | |
2909 | * nexus on which the PERSISTENT RESERVE OUT command | |
2910 | * was received, with the additional sense code set | |
2911 | * to REGISTRATIONS PREEMPTED. | |
2912 | */ | |
6708bb27 | 2913 | if (!all_reg) { |
c66ac9db NB |
2914 | if (pr_reg->pr_res_key != sa_res_key) |
2915 | continue; | |
2916 | ||
2917 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; | |
2918 | pr_reg_nacl = pr_reg->pr_reg_nacl; | |
2919 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; | |
2920 | __core_scsi3_free_registration(dev, pr_reg, | |
33ce6a87 | 2921 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : |
c66ac9db NB |
2922 | NULL, calling_it_nexus); |
2923 | released_regs++; | |
2924 | } else { | |
2925 | /* | |
2926 | * Case for any existing all registrants type | |
2927 | * reservation, follow logic in spc4r17 section | |
2928 | * 5.7.11.4 Preempting, Table 52 and Figure 7. | |
2929 | * | |
2930 | * For a ZERO SA Reservation key, release | |
2931 | * all other registrations and do an implict | |
2932 | * release of active persistent reservation. | |
2933 | * | |
2934 | * For a non-ZERO SA Reservation key, only | |
2935 | * release the matching reservation key from | |
2936 | * registrations. | |
2937 | */ | |
2938 | if ((sa_res_key) && | |
2939 | (pr_reg->pr_res_key != sa_res_key)) | |
2940 | continue; | |
2941 | ||
2942 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; | |
2943 | if (calling_it_nexus) | |
2944 | continue; | |
2945 | ||
2946 | pr_reg_nacl = pr_reg->pr_reg_nacl; | |
2947 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; | |
2948 | __core_scsi3_free_registration(dev, pr_reg, | |
33ce6a87 | 2949 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : |
c66ac9db NB |
2950 | NULL, 0); |
2951 | released_regs++; | |
2952 | } | |
6708bb27 | 2953 | if (!calling_it_nexus) |
c66ac9db NB |
2954 | core_scsi3_ua_allocate(pr_reg_nacl, |
2955 | pr_res_mapped_lun, 0x2A, | |
9e08e34e | 2956 | ASCQ_2AH_REGISTRATIONS_PREEMPTED); |
c66ac9db NB |
2957 | } |
2958 | spin_unlock(&pr_tmpl->registration_lock); | |
2959 | /* | |
2960 | * If a PERSISTENT RESERVE OUT with a PREEMPT service action or | |
2961 | * a PREEMPT AND ABORT service action sets the SERVICE ACTION | |
2962 | * RESERVATION KEY field to a value that does not match any | |
2963 | * registered reservation key, then the device server shall | |
2964 | * complete the command with RESERVATION CONFLICT status. | |
2965 | */ | |
6708bb27 | 2966 | if (!released_regs) { |
c66ac9db NB |
2967 | spin_unlock(&dev->dev_reservation_lock); |
2968 | core_scsi3_put_pr_reg(pr_reg_n); | |
de103c93 | 2969 | return TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
2970 | } |
2971 | /* | |
2972 | * For an existing all registrants type reservation | |
2973 | * with a zero SA rservation key, preempt the existing | |
2974 | * reservation with the new PR type and scope. | |
2975 | */ | |
2976 | if (pr_res_holder && all_reg && !(sa_res_key)) { | |
2977 | __core_scsi3_complete_pro_preempt(dev, pr_reg_n, | |
33ce6a87 AG |
2978 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, |
2979 | type, scope, preempt_type); | |
c66ac9db | 2980 | |
33ce6a87 | 2981 | if (preempt_type == PREEMPT_AND_ABORT) |
c66ac9db NB |
2982 | core_scsi3_release_preempt_and_abort( |
2983 | &preempt_and_abort_list, pr_reg_n); | |
2984 | } | |
2985 | spin_unlock(&dev->dev_reservation_lock); | |
2986 | ||
2987 | if (pr_tmpl->pr_aptpl_active) { | |
de103c93 | 2988 | if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, |
4e529be2 | 2989 | pr_reg_n->pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
6708bb27 | 2990 | pr_debug("SPC-3 PR: Updated APTPL" |
33ce6a87 | 2991 | " metadata for PREEMPT%s\n", (preempt_type == PREEMPT_AND_ABORT) ? |
c66ac9db | 2992 | "_AND_ABORT" : ""); |
de103c93 | 2993 | } |
c66ac9db NB |
2994 | } |
2995 | ||
2996 | core_scsi3_put_pr_reg(pr_reg_n); | |
5951146d | 2997 | core_scsi3_pr_generation(cmd->se_dev); |
c66ac9db NB |
2998 | return 0; |
2999 | } | |
3000 | /* | |
3001 | * The PREEMPTing SA reservation key matches that of the | |
3002 | * existing persistent reservation, first, we check if | |
3003 | * we are preempting our own reservation. | |
3004 | * From spc4r17, section 5.7.11.4.3 Preempting | |
3005 | * persistent reservations and registration handling | |
3006 | * | |
3007 | * If an all registrants persistent reservation is not | |
3008 | * present, it is not an error for the persistent | |
3009 | * reservation holder to preempt itself (i.e., a | |
3010 | * PERSISTENT RESERVE OUT with a PREEMPT service action | |
3011 | * or a PREEMPT AND ABORT service action with the | |
3012 | * SERVICE ACTION RESERVATION KEY value equal to the | |
3013 | * persistent reservation holder's reservation key that | |
3014 | * is received from the persistent reservation holder). | |
3015 | * In that case, the device server shall establish the | |
3016 | * new persistent reservation and maintain the | |
3017 | * registration. | |
3018 | */ | |
3019 | prh_type = pr_res_holder->pr_res_type; | |
3020 | prh_scope = pr_res_holder->pr_res_scope; | |
3021 | /* | |
3022 | * If the SERVICE ACTION RESERVATION KEY field identifies a | |
3023 | * persistent reservation holder (see 5.7.10), the device | |
3024 | * server shall perform a preempt by doing the following as | |
3025 | * an uninterrupted series of actions: | |
3026 | * | |
3027 | * a) Release the persistent reservation for the holder | |
3028 | * identified by the SERVICE ACTION RESERVATION KEY field; | |
3029 | */ | |
3030 | if (pr_reg_n != pr_res_holder) | |
3031 | __core_scsi3_complete_pro_release(dev, | |
3032 | pr_res_holder->pr_reg_nacl, | |
3033 | dev->dev_pr_res_holder, 0); | |
3034 | /* | |
3035 | * b) Remove the registrations for all I_T nexuses identified | |
3036 | * by the SERVICE ACTION RESERVATION KEY field, except the | |
3037 | * I_T nexus that is being used for the PERSISTENT RESERVE | |
3038 | * OUT command. If an all registrants persistent reservation | |
3039 | * is present and the SERVICE ACTION RESERVATION KEY field | |
3040 | * is set to zero, then all registrations shall be removed | |
3041 | * except for that of the I_T nexus that is being used for | |
3042 | * the PERSISTENT RESERVE OUT command; | |
3043 | */ | |
3044 | spin_lock(&pr_tmpl->registration_lock); | |
3045 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, | |
3046 | &pr_tmpl->registration_list, pr_reg_list) { | |
3047 | ||
3048 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; | |
3049 | if (calling_it_nexus) | |
3050 | continue; | |
3051 | ||
3052 | if (pr_reg->pr_res_key != sa_res_key) | |
3053 | continue; | |
3054 | ||
3055 | pr_reg_nacl = pr_reg->pr_reg_nacl; | |
3056 | pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; | |
3057 | __core_scsi3_free_registration(dev, pr_reg, | |
33ce6a87 | 3058 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, |
c66ac9db NB |
3059 | calling_it_nexus); |
3060 | /* | |
3061 | * e) Establish a unit attention condition for the initiator | |
3062 | * port associated with every I_T nexus that lost its | |
3063 | * persistent reservation and/or registration, with the | |
3064 | * additional sense code set to REGISTRATIONS PREEMPTED; | |
3065 | */ | |
3066 | core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A, | |
9e08e34e | 3067 | ASCQ_2AH_REGISTRATIONS_PREEMPTED); |
c66ac9db NB |
3068 | } |
3069 | spin_unlock(&pr_tmpl->registration_lock); | |
3070 | /* | |
3071 | * c) Establish a persistent reservation for the preempting | |
3072 | * I_T nexus using the contents of the SCOPE and TYPE fields; | |
3073 | */ | |
3074 | __core_scsi3_complete_pro_preempt(dev, pr_reg_n, | |
33ce6a87 AG |
3075 | (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, |
3076 | type, scope, preempt_type); | |
c66ac9db NB |
3077 | /* |
3078 | * d) Process tasks as defined in 5.7.1; | |
3079 | * e) See above.. | |
3080 | * f) If the type or scope has changed, then for every I_T nexus | |
3081 | * whose reservation key was not removed, except for the I_T | |
3082 | * nexus on which the PERSISTENT RESERVE OUT command was | |
3083 | * received, the device server shall establish a unit | |
3084 | * attention condition for the initiator port associated with | |
3085 | * that I_T nexus, with the additional sense code set to | |
3086 | * RESERVATIONS RELEASED. If the type or scope have not | |
3087 | * changed, then no unit attention condition(s) shall be | |
3088 | * established for this reason. | |
3089 | */ | |
3090 | if ((prh_type != type) || (prh_scope != scope)) { | |
3091 | spin_lock(&pr_tmpl->registration_lock); | |
3092 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, | |
3093 | &pr_tmpl->registration_list, pr_reg_list) { | |
3094 | ||
3095 | calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; | |
3096 | if (calling_it_nexus) | |
3097 | continue; | |
3098 | ||
3099 | core_scsi3_ua_allocate(pr_reg->pr_reg_nacl, | |
3100 | pr_reg->pr_res_mapped_lun, 0x2A, | |
3101 | ASCQ_2AH_RESERVATIONS_RELEASED); | |
3102 | } | |
3103 | spin_unlock(&pr_tmpl->registration_lock); | |
3104 | } | |
3105 | spin_unlock(&dev->dev_reservation_lock); | |
3106 | /* | |
3107 | * Call LUN_RESET logic upon list of struct t10_pr_registration, | |
3108 | * All received CDBs for the matching existing reservation and | |
3109 | * registrations undergo ABORT_TASK logic. | |
3110 | * | |
3111 | * From there, core_scsi3_release_preempt_and_abort() will | |
3112 | * release every registration in the list (which have already | |
3113 | * been removed from the primary pr_reg list), except the | |
3114 | * new persistent reservation holder, the calling Initiator Port. | |
3115 | */ | |
33ce6a87 | 3116 | if (preempt_type == PREEMPT_AND_ABORT) { |
c66ac9db NB |
3117 | core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd); |
3118 | core_scsi3_release_preempt_and_abort(&preempt_and_abort_list, | |
3119 | pr_reg_n); | |
3120 | } | |
3121 | ||
3122 | if (pr_tmpl->pr_aptpl_active) { | |
de103c93 | 3123 | if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, |
4e529be2 | 3124 | pr_reg_n->pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
6708bb27 | 3125 | pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT" |
33ce6a87 | 3126 | "%s\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : ""); |
de103c93 | 3127 | } |
c66ac9db NB |
3128 | } |
3129 | ||
3130 | core_scsi3_put_pr_reg(pr_reg_n); | |
5951146d | 3131 | core_scsi3_pr_generation(cmd->se_dev); |
c66ac9db NB |
3132 | return 0; |
3133 | } | |
3134 | ||
de103c93 CH |
3135 | static sense_reason_t |
3136 | core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope, | |
33ce6a87 | 3137 | u64 res_key, u64 sa_res_key, enum preempt_type preempt_type) |
c66ac9db | 3138 | { |
c66ac9db NB |
3139 | switch (type) { |
3140 | case PR_TYPE_WRITE_EXCLUSIVE: | |
3141 | case PR_TYPE_EXCLUSIVE_ACCESS: | |
3142 | case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: | |
3143 | case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: | |
3144 | case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: | |
3145 | case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: | |
de103c93 | 3146 | return core_scsi3_pro_preempt(cmd, type, scope, res_key, |
33ce6a87 | 3147 | sa_res_key, preempt_type); |
c66ac9db | 3148 | default: |
6708bb27 | 3149 | pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s" |
33ce6a87 | 3150 | " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type); |
de103c93 | 3151 | return TCM_INVALID_CDB_FIELD; |
c66ac9db | 3152 | } |
c66ac9db NB |
3153 | } |
3154 | ||
3155 | ||
de103c93 CH |
3156 | static sense_reason_t |
3157 | core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key, | |
3158 | u64 sa_res_key, int aptpl, int unreg) | |
c66ac9db | 3159 | { |
e3d6f909 | 3160 | struct se_session *se_sess = cmd->se_sess; |
5951146d | 3161 | struct se_device *dev = cmd->se_dev; |
28168905 | 3162 | struct se_dev_entry *dest_se_deve = NULL; |
e3d6f909 | 3163 | struct se_lun *se_lun = cmd->se_lun; |
c66ac9db NB |
3164 | struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL; |
3165 | struct se_port *se_port; | |
3166 | struct se_portal_group *se_tpg, *dest_se_tpg = NULL; | |
3167 | struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops; | |
3168 | struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg; | |
0fd97ccf | 3169 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
05d1c7c0 | 3170 | unsigned char *buf; |
c66ac9db NB |
3171 | unsigned char *initiator_str; |
3172 | char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN]; | |
3173 | u32 tid_len, tmp_tid_len; | |
d2843c17 | 3174 | int new_reg = 0, type, scope, matching_iname; |
de103c93 | 3175 | sense_reason_t ret; |
c66ac9db NB |
3176 | unsigned short rtpi; |
3177 | unsigned char proto_ident; | |
3178 | ||
6708bb27 AG |
3179 | if (!se_sess || !se_lun) { |
3180 | pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); | |
de103c93 | 3181 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db | 3182 | } |
de103c93 | 3183 | |
c66ac9db NB |
3184 | memset(dest_iport, 0, 64); |
3185 | memset(i_buf, 0, PR_REG_ISID_ID_LEN); | |
3186 | se_tpg = se_sess->se_tpg; | |
e3d6f909 | 3187 | tf_ops = se_tpg->se_tpg_tfo; |
c66ac9db NB |
3188 | /* |
3189 | * Follow logic from spc4r17 Section 5.7.8, Table 50 -- | |
3190 | * Register behaviors for a REGISTER AND MOVE service action | |
3191 | * | |
3192 | * Locate the existing *pr_reg via struct se_node_acl pointers | |
3193 | */ | |
5951146d | 3194 | pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, |
c66ac9db | 3195 | se_sess); |
6708bb27 AG |
3196 | if (!pr_reg) { |
3197 | pr_err("SPC-3 PR: Unable to locate PR_REGISTERED" | |
c66ac9db | 3198 | " *pr_reg for REGISTER_AND_MOVE\n"); |
de103c93 | 3199 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
3200 | } |
3201 | /* | |
3202 | * The provided reservation key much match the existing reservation key | |
3203 | * provided during this initiator's I_T nexus registration. | |
3204 | */ | |
3205 | if (res_key != pr_reg->pr_res_key) { | |
6708bb27 | 3206 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received" |
c66ac9db NB |
3207 | " res_key: 0x%016Lx does not match existing SA REGISTER" |
3208 | " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key); | |
de103c93 CH |
3209 | ret = TCM_RESERVATION_CONFLICT; |
3210 | goto out_put_pr_reg; | |
c66ac9db NB |
3211 | } |
3212 | /* | |
3213 | * The service active reservation key needs to be non zero | |
3214 | */ | |
6708bb27 AG |
3215 | if (!sa_res_key) { |
3216 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero" | |
c66ac9db | 3217 | " sa_res_key\n"); |
de103c93 CH |
3218 | ret = TCM_INVALID_PARAMETER_LIST; |
3219 | goto out_put_pr_reg; | |
c66ac9db | 3220 | } |
05d1c7c0 | 3221 | |
c66ac9db NB |
3222 | /* |
3223 | * Determine the Relative Target Port Identifier where the reservation | |
3224 | * will be moved to for the TransportID containing SCSI initiator WWN | |
3225 | * information. | |
3226 | */ | |
4949314c | 3227 | buf = transport_kmap_data_sg(cmd); |
de103c93 CH |
3228 | if (!buf) { |
3229 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
3230 | goto out_put_pr_reg; | |
3231 | } | |
3232 | ||
c66ac9db NB |
3233 | rtpi = (buf[18] & 0xff) << 8; |
3234 | rtpi |= buf[19] & 0xff; | |
3235 | tid_len = (buf[20] & 0xff) << 24; | |
3236 | tid_len |= (buf[21] & 0xff) << 16; | |
3237 | tid_len |= (buf[22] & 0xff) << 8; | |
3238 | tid_len |= buf[23] & 0xff; | |
4949314c | 3239 | transport_kunmap_data_sg(cmd); |
05d1c7c0 | 3240 | buf = NULL; |
c66ac9db NB |
3241 | |
3242 | if ((tid_len + 24) != cmd->data_length) { | |
6708bb27 | 3243 | pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header" |
c66ac9db NB |
3244 | " does not equal CDB data_length: %u\n", tid_len, |
3245 | cmd->data_length); | |
de103c93 CH |
3246 | ret = TCM_INVALID_PARAMETER_LIST; |
3247 | goto out_put_pr_reg; | |
c66ac9db NB |
3248 | } |
3249 | ||
3250 | spin_lock(&dev->se_port_lock); | |
3251 | list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) { | |
3252 | if (se_port->sep_rtpi != rtpi) | |
3253 | continue; | |
3254 | dest_se_tpg = se_port->sep_tpg; | |
6708bb27 | 3255 | if (!dest_se_tpg) |
c66ac9db | 3256 | continue; |
e3d6f909 | 3257 | dest_tf_ops = dest_se_tpg->se_tpg_tfo; |
6708bb27 | 3258 | if (!dest_tf_ops) |
c66ac9db NB |
3259 | continue; |
3260 | ||
3261 | atomic_inc(&dest_se_tpg->tpg_pr_ref_count); | |
3262 | smp_mb__after_atomic_inc(); | |
3263 | spin_unlock(&dev->se_port_lock); | |
3264 | ||
de103c93 | 3265 | if (core_scsi3_tpg_depend_item(dest_se_tpg)) { |
6708bb27 | 3266 | pr_err("core_scsi3_tpg_depend_item() failed" |
c66ac9db NB |
3267 | " for dest_se_tpg\n"); |
3268 | atomic_dec(&dest_se_tpg->tpg_pr_ref_count); | |
3269 | smp_mb__after_atomic_dec(); | |
de103c93 CH |
3270 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
3271 | goto out_put_pr_reg; | |
c66ac9db NB |
3272 | } |
3273 | ||
3274 | spin_lock(&dev->se_port_lock); | |
3275 | break; | |
3276 | } | |
3277 | spin_unlock(&dev->se_port_lock); | |
3278 | ||
6708bb27 AG |
3279 | if (!dest_se_tpg || !dest_tf_ops) { |
3280 | pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" | |
c66ac9db NB |
3281 | " fabric ops from Relative Target Port Identifier:" |
3282 | " %hu\n", rtpi); | |
de103c93 CH |
3283 | ret = TCM_INVALID_PARAMETER_LIST; |
3284 | goto out_put_pr_reg; | |
c66ac9db | 3285 | } |
05d1c7c0 | 3286 | |
4949314c | 3287 | buf = transport_kmap_data_sg(cmd); |
de103c93 CH |
3288 | if (!buf) { |
3289 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
3290 | goto out_put_pr_reg; | |
3291 | } | |
c66ac9db | 3292 | proto_ident = (buf[24] & 0x0f); |
8b1e1244 | 3293 | |
6708bb27 | 3294 | pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:" |
c66ac9db | 3295 | " 0x%02x\n", proto_ident); |
8b1e1244 | 3296 | |
c66ac9db | 3297 | if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) { |
6708bb27 | 3298 | pr_err("SPC-3 PR REGISTER_AND_MOVE: Received" |
c66ac9db NB |
3299 | " proto_ident: 0x%02x does not match ident: 0x%02x" |
3300 | " from fabric: %s\n", proto_ident, | |
3301 | dest_tf_ops->get_fabric_proto_ident(dest_se_tpg), | |
3302 | dest_tf_ops->get_fabric_name()); | |
de103c93 | 3303 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3304 | goto out; |
3305 | } | |
3306 | if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) { | |
6708bb27 | 3307 | pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not" |
c66ac9db NB |
3308 | " containg a valid tpg_parse_pr_out_transport_id" |
3309 | " function pointer\n"); | |
de103c93 | 3310 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
3311 | goto out; |
3312 | } | |
3313 | initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg, | |
3314 | (const char *)&buf[24], &tmp_tid_len, &iport_ptr); | |
6708bb27 AG |
3315 | if (!initiator_str) { |
3316 | pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" | |
c66ac9db | 3317 | " initiator_str from Transport ID\n"); |
de103c93 | 3318 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3319 | goto out; |
3320 | } | |
3321 | ||
4949314c | 3322 | transport_kunmap_data_sg(cmd); |
05d1c7c0 AG |
3323 | buf = NULL; |
3324 | ||
6708bb27 | 3325 | pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s" |
c66ac9db NB |
3326 | " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ? |
3327 | "port" : "device", initiator_str, (iport_ptr != NULL) ? | |
3328 | iport_ptr : ""); | |
3329 | /* | |
3330 | * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service | |
3331 | * action specifies a TransportID that is the same as the initiator port | |
3332 | * of the I_T nexus for the command received, then the command shall | |
3333 | * be terminated with CHECK CONDITION status, with the sense key set to | |
3334 | * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD | |
3335 | * IN PARAMETER LIST. | |
3336 | */ | |
3337 | pr_reg_nacl = pr_reg->pr_reg_nacl; | |
3338 | matching_iname = (!strcmp(initiator_str, | |
3339 | pr_reg_nacl->initiatorname)) ? 1 : 0; | |
6708bb27 | 3340 | if (!matching_iname) |
c66ac9db NB |
3341 | goto after_iport_check; |
3342 | ||
6708bb27 AG |
3343 | if (!iport_ptr || !pr_reg->isid_present_at_reg) { |
3344 | pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s" | |
c66ac9db NB |
3345 | " matches: %s on received I_T Nexus\n", initiator_str, |
3346 | pr_reg_nacl->initiatorname); | |
de103c93 | 3347 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3348 | goto out; |
3349 | } | |
6708bb27 AG |
3350 | if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) { |
3351 | pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s" | |
c66ac9db NB |
3352 | " matches: %s %s on received I_T Nexus\n", |
3353 | initiator_str, iport_ptr, pr_reg_nacl->initiatorname, | |
3354 | pr_reg->pr_reg_isid); | |
de103c93 | 3355 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3356 | goto out; |
3357 | } | |
3358 | after_iport_check: | |
3359 | /* | |
3360 | * Locate the destination struct se_node_acl from the received Transport ID | |
3361 | */ | |
28638887 | 3362 | spin_lock_irq(&dest_se_tpg->acl_node_lock); |
c66ac9db NB |
3363 | dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg, |
3364 | initiator_str); | |
3365 | if (dest_node_acl) { | |
3366 | atomic_inc(&dest_node_acl->acl_pr_ref_count); | |
3367 | smp_mb__after_atomic_inc(); | |
3368 | } | |
28638887 | 3369 | spin_unlock_irq(&dest_se_tpg->acl_node_lock); |
c66ac9db | 3370 | |
6708bb27 AG |
3371 | if (!dest_node_acl) { |
3372 | pr_err("Unable to locate %s dest_node_acl for" | |
c66ac9db NB |
3373 | " TransportID%s\n", dest_tf_ops->get_fabric_name(), |
3374 | initiator_str); | |
de103c93 | 3375 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3376 | goto out; |
3377 | } | |
de103c93 CH |
3378 | |
3379 | if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { | |
6708bb27 | 3380 | pr_err("core_scsi3_nodeacl_depend_item() for" |
c66ac9db NB |
3381 | " dest_node_acl\n"); |
3382 | atomic_dec(&dest_node_acl->acl_pr_ref_count); | |
3383 | smp_mb__after_atomic_dec(); | |
3384 | dest_node_acl = NULL; | |
de103c93 | 3385 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3386 | goto out; |
3387 | } | |
8b1e1244 | 3388 | |
6708bb27 | 3389 | pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:" |
c66ac9db NB |
3390 | " %s from TransportID\n", dest_tf_ops->get_fabric_name(), |
3391 | dest_node_acl->initiatorname); | |
8b1e1244 | 3392 | |
c66ac9db NB |
3393 | /* |
3394 | * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET | |
3395 | * PORT IDENTIFIER. | |
3396 | */ | |
3397 | dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi); | |
6708bb27 AG |
3398 | if (!dest_se_deve) { |
3399 | pr_err("Unable to locate %s dest_se_deve from RTPI:" | |
c66ac9db | 3400 | " %hu\n", dest_tf_ops->get_fabric_name(), rtpi); |
de103c93 | 3401 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3402 | goto out; |
3403 | } | |
3404 | ||
de103c93 | 3405 | if (core_scsi3_lunacl_depend_item(dest_se_deve)) { |
6708bb27 | 3406 | pr_err("core_scsi3_lunacl_depend_item() failed\n"); |
c66ac9db NB |
3407 | atomic_dec(&dest_se_deve->pr_ref_count); |
3408 | smp_mb__after_atomic_dec(); | |
3409 | dest_se_deve = NULL; | |
de103c93 | 3410 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
3411 | goto out; |
3412 | } | |
8b1e1244 | 3413 | |
6708bb27 | 3414 | pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN" |
c66ac9db NB |
3415 | " ACL for dest_se_deve->mapped_lun: %u\n", |
3416 | dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname, | |
3417 | dest_se_deve->mapped_lun); | |
8b1e1244 | 3418 | |
c66ac9db NB |
3419 | /* |
3420 | * A persistent reservation needs to already existing in order to | |
3421 | * successfully complete the REGISTER_AND_MOVE service action.. | |
3422 | */ | |
3423 | spin_lock(&dev->dev_reservation_lock); | |
3424 | pr_res_holder = dev->dev_pr_res_holder; | |
6708bb27 AG |
3425 | if (!pr_res_holder) { |
3426 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation" | |
c66ac9db NB |
3427 | " currently held\n"); |
3428 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 | 3429 | ret = TCM_INVALID_CDB_FIELD; |
c66ac9db NB |
3430 | goto out; |
3431 | } | |
3432 | /* | |
3433 | * The received on I_T Nexus must be the reservation holder. | |
3434 | * | |
3435 | * From spc4r17 section 5.7.8 Table 50 -- | |
3436 | * Register behaviors for a REGISTER AND MOVE service action | |
3437 | */ | |
3438 | if (pr_res_holder != pr_reg) { | |
6708bb27 | 3439 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T" |
c66ac9db NB |
3440 | " Nexus is not reservation holder\n"); |
3441 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 | 3442 | ret = TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
3443 | goto out; |
3444 | } | |
3445 | /* | |
3446 | * From spc4r17 section 5.7.8: registering and moving reservation | |
3447 | * | |
3448 | * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service | |
3449 | * action is received and the established persistent reservation is a | |
3450 | * Write Exclusive - All Registrants type or Exclusive Access - | |
3451 | * All Registrants type reservation, then the command shall be completed | |
3452 | * with RESERVATION CONFLICT status. | |
3453 | */ | |
3454 | if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || | |
3455 | (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { | |
6708bb27 | 3456 | pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move" |
c66ac9db NB |
3457 | " reservation for type: %s\n", |
3458 | core_scsi3_pr_dump_type(pr_res_holder->pr_res_type)); | |
3459 | spin_unlock(&dev->dev_reservation_lock); | |
de103c93 | 3460 | ret = TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
3461 | goto out; |
3462 | } | |
3463 | pr_res_nacl = pr_res_holder->pr_reg_nacl; | |
3464 | /* | |
3465 | * b) Ignore the contents of the (received) SCOPE and TYPE fields; | |
3466 | */ | |
3467 | type = pr_res_holder->pr_res_type; | |
3468 | scope = pr_res_holder->pr_res_type; | |
3469 | /* | |
3470 | * c) Associate the reservation key specified in the SERVICE ACTION | |
3471 | * RESERVATION KEY field with the I_T nexus specified as the | |
3472 | * destination of the register and move, where: | |
3473 | * A) The I_T nexus is specified by the TransportID and the | |
3474 | * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and | |
3475 | * B) Regardless of the TransportID format used, the association for | |
3476 | * the initiator port is based on either the initiator port name | |
3477 | * (see 3.1.71) on SCSI transport protocols where port names are | |
3478 | * required or the initiator port identifier (see 3.1.70) on SCSI | |
3479 | * transport protocols where port names are not required; | |
3480 | * d) Register the reservation key specified in the SERVICE ACTION | |
3481 | * RESERVATION KEY field; | |
3482 | * e) Retain the reservation key specified in the SERVICE ACTION | |
3483 | * RESERVATION KEY field and associated information; | |
3484 | * | |
3485 | * Also, It is not an error for a REGISTER AND MOVE service action to | |
3486 | * register an I_T nexus that is already registered with the same | |
3487 | * reservation key or a different reservation key. | |
3488 | */ | |
3489 | dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, | |
3490 | iport_ptr); | |
6708bb27 | 3491 | if (!dest_pr_reg) { |
de103c93 | 3492 | if (core_scsi3_alloc_registration(cmd->se_dev, |
c66ac9db | 3493 | dest_node_acl, dest_se_deve, iport_ptr, |
de103c93 | 3494 | sa_res_key, 0, aptpl, 2, 1)) { |
c66ac9db | 3495 | spin_unlock(&dev->dev_reservation_lock); |
de103c93 | 3496 | ret = TCM_INVALID_PARAMETER_LIST; |
c66ac9db NB |
3497 | goto out; |
3498 | } | |
3499 | dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, | |
3500 | iport_ptr); | |
3501 | new_reg = 1; | |
3502 | } | |
3503 | /* | |
3504 | * f) Release the persistent reservation for the persistent reservation | |
3505 | * holder (i.e., the I_T nexus on which the | |
3506 | */ | |
3507 | __core_scsi3_complete_pro_release(dev, pr_res_nacl, | |
3508 | dev->dev_pr_res_holder, 0); | |
3509 | /* | |
3510 | * g) Move the persistent reservation to the specified I_T nexus using | |
3511 | * the same scope and type as the persistent reservation released in | |
3512 | * item f); and | |
3513 | */ | |
3514 | dev->dev_pr_res_holder = dest_pr_reg; | |
3515 | dest_pr_reg->pr_res_holder = 1; | |
3516 | dest_pr_reg->pr_res_type = type; | |
3517 | pr_reg->pr_res_scope = scope; | |
d2843c17 | 3518 | core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); |
c66ac9db NB |
3519 | /* |
3520 | * Increment PRGeneration for existing registrations.. | |
3521 | */ | |
6708bb27 | 3522 | if (!new_reg) |
c66ac9db NB |
3523 | dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++; |
3524 | spin_unlock(&dev->dev_reservation_lock); | |
3525 | ||
6708bb27 | 3526 | pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE" |
c66ac9db NB |
3527 | " created new reservation holder TYPE: %s on object RTPI:" |
3528 | " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(), | |
3529 | core_scsi3_pr_dump_type(type), rtpi, | |
3530 | dest_pr_reg->pr_res_generation); | |
6708bb27 | 3531 | pr_debug("SPC-3 PR Successfully moved reservation from" |
c66ac9db NB |
3532 | " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n", |
3533 | tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname, | |
d2843c17 | 3534 | i_buf, dest_tf_ops->get_fabric_name(), |
c66ac9db NB |
3535 | dest_node_acl->initiatorname, (iport_ptr != NULL) ? |
3536 | iport_ptr : ""); | |
3537 | /* | |
3538 | * It is now safe to release configfs group dependencies for destination | |
3539 | * of Transport ID Initiator Device/Port Identifier | |
3540 | */ | |
3541 | core_scsi3_lunacl_undepend_item(dest_se_deve); | |
3542 | core_scsi3_nodeacl_undepend_item(dest_node_acl); | |
3543 | core_scsi3_tpg_undepend_item(dest_se_tpg); | |
3544 | /* | |
3545 | * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T | |
3546 | * nexus on which PERSISTENT RESERVE OUT command was received. | |
3547 | */ | |
3548 | if (unreg) { | |
3549 | spin_lock(&pr_tmpl->registration_lock); | |
3550 | __core_scsi3_free_registration(dev, pr_reg, NULL, 1); | |
3551 | spin_unlock(&pr_tmpl->registration_lock); | |
3552 | } else | |
3553 | core_scsi3_put_pr_reg(pr_reg); | |
3554 | ||
3555 | /* | |
3556 | * Clear the APTPL metadata if APTPL has been disabled, otherwise | |
3557 | * write out the updated metadata to struct file for this SCSI device. | |
3558 | */ | |
6708bb27 | 3559 | if (!aptpl) { |
c66ac9db | 3560 | pr_tmpl->pr_aptpl_active = 0; |
5951146d | 3561 | core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0); |
6708bb27 | 3562 | pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for" |
c66ac9db NB |
3563 | " REGISTER_AND_MOVE\n"); |
3564 | } else { | |
3565 | pr_tmpl->pr_aptpl_active = 1; | |
de103c93 | 3566 | if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, |
4e529be2 | 3567 | dest_pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) { |
6708bb27 | 3568 | pr_debug("SPC-3 PR: Set APTPL Bit Activated for" |
c66ac9db | 3569 | " REGISTER_AND_MOVE\n"); |
de103c93 | 3570 | } |
c66ac9db NB |
3571 | } |
3572 | ||
4949314c | 3573 | transport_kunmap_data_sg(cmd); |
05d1c7c0 | 3574 | |
c66ac9db NB |
3575 | core_scsi3_put_pr_reg(dest_pr_reg); |
3576 | return 0; | |
3577 | out: | |
05d1c7c0 | 3578 | if (buf) |
4949314c | 3579 | transport_kunmap_data_sg(cmd); |
c66ac9db NB |
3580 | if (dest_se_deve) |
3581 | core_scsi3_lunacl_undepend_item(dest_se_deve); | |
3582 | if (dest_node_acl) | |
3583 | core_scsi3_nodeacl_undepend_item(dest_node_acl); | |
3584 | core_scsi3_tpg_undepend_item(dest_se_tpg); | |
de103c93 CH |
3585 | |
3586 | out_put_pr_reg: | |
c66ac9db NB |
3587 | core_scsi3_put_pr_reg(pr_reg); |
3588 | return ret; | |
3589 | } | |
3590 | ||
3591 | static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb) | |
3592 | { | |
3593 | unsigned int __v1, __v2; | |
3594 | ||
3595 | __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3]; | |
3596 | __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7]; | |
3597 | ||
3598 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; | |
3599 | } | |
3600 | ||
3601 | /* | |
3602 | * See spc4r17 section 6.14 Table 170 | |
3603 | */ | |
de103c93 CH |
3604 | sense_reason_t |
3605 | target_scsi3_emulate_pr_out(struct se_cmd *cmd) | |
c66ac9db | 3606 | { |
617c0e06 | 3607 | unsigned char *cdb = &cmd->t_task_cdb[0]; |
05d1c7c0 | 3608 | unsigned char *buf; |
c66ac9db NB |
3609 | u64 res_key, sa_res_key; |
3610 | int sa, scope, type, aptpl; | |
3611 | int spec_i_pt = 0, all_tg_pt = 0, unreg = 0; | |
de103c93 | 3612 | sense_reason_t ret; |
617c0e06 CH |
3613 | |
3614 | /* | |
3615 | * Following spc2r20 5.5.1 Reservations overview: | |
3616 | * | |
3617 | * If a logical unit has been reserved by any RESERVE command and is | |
3618 | * still reserved by any initiator, all PERSISTENT RESERVE IN and all | |
3619 | * PERSISTENT RESERVE OUT commands shall conflict regardless of | |
3620 | * initiator or service action and shall terminate with a RESERVATION | |
3621 | * CONFLICT status. | |
3622 | */ | |
0fd97ccf | 3623 | if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { |
617c0e06 CH |
3624 | pr_err("Received PERSISTENT_RESERVE CDB while legacy" |
3625 | " SPC-2 reservation is held, returning" | |
3626 | " RESERVATION_CONFLICT\n"); | |
de103c93 | 3627 | return TCM_RESERVATION_CONFLICT; |
617c0e06 CH |
3628 | } |
3629 | ||
c66ac9db NB |
3630 | /* |
3631 | * FIXME: A NULL struct se_session pointer means an this is not coming from | |
3632 | * a $FABRIC_MOD's nexus, but from internal passthrough ops. | |
3633 | */ | |
de103c93 CH |
3634 | if (!cmd->se_sess) |
3635 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
c66ac9db NB |
3636 | |
3637 | if (cmd->data_length < 24) { | |
6708bb27 | 3638 | pr_warn("SPC-PR: Received PR OUT parameter list" |
c66ac9db | 3639 | " length too small: %u\n", cmd->data_length); |
de103c93 | 3640 | return TCM_INVALID_PARAMETER_LIST; |
c66ac9db | 3641 | } |
de103c93 | 3642 | |
c66ac9db NB |
3643 | /* |
3644 | * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB) | |
3645 | */ | |
3646 | sa = (cdb[1] & 0x1f); | |
3647 | scope = (cdb[2] & 0xf0); | |
3648 | type = (cdb[2] & 0x0f); | |
05d1c7c0 | 3649 | |
4949314c | 3650 | buf = transport_kmap_data_sg(cmd); |
de103c93 CH |
3651 | if (!buf) |
3652 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
3653 | ||
c66ac9db NB |
3654 | /* |
3655 | * From PERSISTENT_RESERVE_OUT parameter list (payload) | |
3656 | */ | |
3657 | res_key = core_scsi3_extract_reservation_key(&buf[0]); | |
3658 | sa_res_key = core_scsi3_extract_reservation_key(&buf[8]); | |
3659 | /* | |
3660 | * REGISTER_AND_MOVE uses a different SA parameter list containing | |
3661 | * SCSI TransportIDs. | |
3662 | */ | |
3663 | if (sa != PRO_REGISTER_AND_MOVE) { | |
3664 | spec_i_pt = (buf[20] & 0x08); | |
3665 | all_tg_pt = (buf[20] & 0x04); | |
3666 | aptpl = (buf[20] & 0x01); | |
3667 | } else { | |
3668 | aptpl = (buf[17] & 0x01); | |
3669 | unreg = (buf[17] & 0x02); | |
3670 | } | |
4949314c | 3671 | transport_kunmap_data_sg(cmd); |
05d1c7c0 AG |
3672 | buf = NULL; |
3673 | ||
c66ac9db NB |
3674 | /* |
3675 | * SPEC_I_PT=1 is only valid for Service action: REGISTER | |
3676 | */ | |
de103c93 CH |
3677 | if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) |
3678 | return TCM_INVALID_PARAMETER_LIST; | |
d29a5b6a | 3679 | |
c66ac9db NB |
3680 | /* |
3681 | * From spc4r17 section 6.14: | |
3682 | * | |
3683 | * If the SPEC_I_PT bit is set to zero, the service action is not | |
3684 | * REGISTER AND MOVE, and the parameter list length is not 24, then | |
3685 | * the command shall be terminated with CHECK CONDITION status, with | |
3686 | * the sense key set to ILLEGAL REQUEST, and the additional sense | |
3687 | * code set to PARAMETER LIST LENGTH ERROR. | |
3688 | */ | |
6708bb27 | 3689 | if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) && |
c66ac9db | 3690 | (cmd->data_length != 24)) { |
6708bb27 | 3691 | pr_warn("SPC-PR: Received PR OUT illegal parameter" |
c66ac9db | 3692 | " list length: %u\n", cmd->data_length); |
de103c93 | 3693 | return TCM_INVALID_PARAMETER_LIST; |
c66ac9db | 3694 | } |
de103c93 | 3695 | |
c66ac9db NB |
3696 | /* |
3697 | * (core_scsi3_emulate_pro_* function parameters | |
3698 | * are defined by spc4r17 Table 174: | |
3699 | * PERSISTENT_RESERVE_OUT service actions and valid parameters. | |
3700 | */ | |
3701 | switch (sa) { | |
3702 | case PRO_REGISTER: | |
d29a5b6a | 3703 | ret = core_scsi3_emulate_pro_register(cmd, |
33ce6a87 | 3704 | res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER); |
d29a5b6a | 3705 | break; |
c66ac9db | 3706 | case PRO_RESERVE: |
d29a5b6a CH |
3707 | ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key); |
3708 | break; | |
c66ac9db | 3709 | case PRO_RELEASE: |
d29a5b6a CH |
3710 | ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key); |
3711 | break; | |
c66ac9db | 3712 | case PRO_CLEAR: |
d29a5b6a CH |
3713 | ret = core_scsi3_emulate_pro_clear(cmd, res_key); |
3714 | break; | |
c66ac9db | 3715 | case PRO_PREEMPT: |
d29a5b6a | 3716 | ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, |
33ce6a87 | 3717 | res_key, sa_res_key, PREEMPT); |
d29a5b6a | 3718 | break; |
c66ac9db | 3719 | case PRO_PREEMPT_AND_ABORT: |
d29a5b6a | 3720 | ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, |
33ce6a87 | 3721 | res_key, sa_res_key, PREEMPT_AND_ABORT); |
d29a5b6a | 3722 | break; |
c66ac9db | 3723 | case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: |
d29a5b6a | 3724 | ret = core_scsi3_emulate_pro_register(cmd, |
33ce6a87 | 3725 | 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY); |
d29a5b6a | 3726 | break; |
c66ac9db | 3727 | case PRO_REGISTER_AND_MOVE: |
d29a5b6a | 3728 | ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key, |
c66ac9db | 3729 | sa_res_key, aptpl, unreg); |
d29a5b6a | 3730 | break; |
c66ac9db | 3731 | default: |
6708bb27 | 3732 | pr_err("Unknown PERSISTENT_RESERVE_OUT service" |
c66ac9db | 3733 | " action: 0x%02x\n", cdb[1] & 0x1f); |
de103c93 | 3734 | return TCM_INVALID_CDB_FIELD; |
c66ac9db NB |
3735 | } |
3736 | ||
6bb35e00 CH |
3737 | if (!ret) |
3738 | target_complete_cmd(cmd, GOOD); | |
d29a5b6a | 3739 | return ret; |
c66ac9db NB |
3740 | } |
3741 | ||
3742 | /* | |
3743 | * PERSISTENT_RESERVE_IN Service Action READ_KEYS | |
3744 | * | |
3745 | * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160 | |
3746 | */ | |
de103c93 CH |
3747 | static sense_reason_t |
3748 | core_scsi3_pri_read_keys(struct se_cmd *cmd) | |
c66ac9db | 3749 | { |
0fd97ccf | 3750 | struct se_device *dev = cmd->se_dev; |
c66ac9db | 3751 | struct t10_pr_registration *pr_reg; |
05d1c7c0 | 3752 | unsigned char *buf; |
c66ac9db NB |
3753 | u32 add_len = 0, off = 8; |
3754 | ||
3755 | if (cmd->data_length < 8) { | |
6708bb27 | 3756 | pr_err("PRIN SA READ_KEYS SCSI Data Length: %u" |
c66ac9db | 3757 | " too small\n", cmd->data_length); |
de103c93 | 3758 | return TCM_INVALID_CDB_FIELD; |
c66ac9db NB |
3759 | } |
3760 | ||
4949314c | 3761 | buf = transport_kmap_data_sg(cmd); |
de103c93 CH |
3762 | if (!buf) |
3763 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
3764 | ||
0fd97ccf CH |
3765 | buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); |
3766 | buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); | |
3767 | buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); | |
3768 | buf[3] = (dev->t10_pr.pr_generation & 0xff); | |
c66ac9db | 3769 | |
0fd97ccf CH |
3770 | spin_lock(&dev->t10_pr.registration_lock); |
3771 | list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, | |
c66ac9db NB |
3772 | pr_reg_list) { |
3773 | /* | |
3774 | * Check for overflow of 8byte PRI READ_KEYS payload and | |
3775 | * next reservation key list descriptor. | |
3776 | */ | |
3777 | if ((add_len + 8) > (cmd->data_length - 8)) | |
3778 | break; | |
3779 | ||
3780 | buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); | |
3781 | buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); | |
3782 | buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); | |
3783 | buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); | |
3784 | buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); | |
3785 | buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); | |
3786 | buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); | |
3787 | buf[off++] = (pr_reg->pr_res_key & 0xff); | |
3788 | ||
3789 | add_len += 8; | |
3790 | } | |
0fd97ccf | 3791 | spin_unlock(&dev->t10_pr.registration_lock); |
c66ac9db NB |
3792 | |
3793 | buf[4] = ((add_len >> 24) & 0xff); | |
3794 | buf[5] = ((add_len >> 16) & 0xff); | |
3795 | buf[6] = ((add_len >> 8) & 0xff); | |
3796 | buf[7] = (add_len & 0xff); | |
3797 | ||
4949314c | 3798 | transport_kunmap_data_sg(cmd); |
05d1c7c0 | 3799 | |
c66ac9db NB |
3800 | return 0; |
3801 | } | |
3802 | ||
3803 | /* | |
3804 | * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION | |
3805 | * | |
3806 | * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162 | |
3807 | */ | |
de103c93 CH |
3808 | static sense_reason_t |
3809 | core_scsi3_pri_read_reservation(struct se_cmd *cmd) | |
c66ac9db | 3810 | { |
0fd97ccf | 3811 | struct se_device *dev = cmd->se_dev; |
c66ac9db | 3812 | struct t10_pr_registration *pr_reg; |
05d1c7c0 | 3813 | unsigned char *buf; |
c66ac9db NB |
3814 | u64 pr_res_key; |
3815 | u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */ | |
3816 | ||
3817 | if (cmd->data_length < 8) { | |
6708bb27 | 3818 | pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u" |
c66ac9db | 3819 | " too small\n", cmd->data_length); |
de103c93 | 3820 | return TCM_INVALID_CDB_FIELD; |
c66ac9db NB |
3821 | } |
3822 | ||
4949314c | 3823 | buf = transport_kmap_data_sg(cmd); |
de103c93 CH |
3824 | if (!buf) |
3825 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
3826 | ||
0fd97ccf CH |
3827 | buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); |
3828 | buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); | |
3829 | buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); | |
3830 | buf[3] = (dev->t10_pr.pr_generation & 0xff); | |
c66ac9db | 3831 | |
0fd97ccf CH |
3832 | spin_lock(&dev->dev_reservation_lock); |
3833 | pr_reg = dev->dev_pr_res_holder; | |
ee1b1b9c | 3834 | if (pr_reg) { |
c66ac9db NB |
3835 | /* |
3836 | * Set the hardcoded Additional Length | |
3837 | */ | |
3838 | buf[4] = ((add_len >> 24) & 0xff); | |
3839 | buf[5] = ((add_len >> 16) & 0xff); | |
3840 | buf[6] = ((add_len >> 8) & 0xff); | |
3841 | buf[7] = (add_len & 0xff); | |
3842 | ||
05d1c7c0 AG |
3843 | if (cmd->data_length < 22) |
3844 | goto err; | |
3845 | ||
c66ac9db NB |
3846 | /* |
3847 | * Set the Reservation key. | |
3848 | * | |
3849 | * From spc4r17, section 5.7.10: | |
3850 | * A persistent reservation holder has its reservation key | |
3851 | * returned in the parameter data from a PERSISTENT | |
3852 | * RESERVE IN command with READ RESERVATION service action as | |
3853 | * follows: | |
3854 | * a) For a persistent reservation of the type Write Exclusive | |
3855 | * - All Registrants or Exclusive Access  All Regitrants, | |
3856 | * the reservation key shall be set to zero; or | |
3857 | * b) For all other persistent reservation types, the | |
3858 | * reservation key shall be set to the registered | |
3859 | * reservation key for the I_T nexus that holds the | |
3860 | * persistent reservation. | |
3861 | */ | |
3862 | if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || | |
3863 | (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) | |
3864 | pr_res_key = 0; | |
3865 | else | |
3866 | pr_res_key = pr_reg->pr_res_key; | |
3867 | ||
3868 | buf[8] = ((pr_res_key >> 56) & 0xff); | |
3869 | buf[9] = ((pr_res_key >> 48) & 0xff); | |
3870 | buf[10] = ((pr_res_key >> 40) & 0xff); | |
3871 | buf[11] = ((pr_res_key >> 32) & 0xff); | |
3872 | buf[12] = ((pr_res_key >> 24) & 0xff); | |
3873 | buf[13] = ((pr_res_key >> 16) & 0xff); | |
3874 | buf[14] = ((pr_res_key >> 8) & 0xff); | |
3875 | buf[15] = (pr_res_key & 0xff); | |
3876 | /* | |
3877 | * Set the SCOPE and TYPE | |
3878 | */ | |
3879 | buf[21] = (pr_reg->pr_res_scope & 0xf0) | | |
3880 | (pr_reg->pr_res_type & 0x0f); | |
3881 | } | |
05d1c7c0 AG |
3882 | |
3883 | err: | |
0fd97ccf | 3884 | spin_unlock(&dev->dev_reservation_lock); |
4949314c | 3885 | transport_kunmap_data_sg(cmd); |
c66ac9db NB |
3886 | |
3887 | return 0; | |
3888 | } | |
3889 | ||
3890 | /* | |
3891 | * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES | |
3892 | * | |
3893 | * See spc4r17 section 6.13.4 Table 165 | |
3894 | */ | |
de103c93 CH |
3895 | static sense_reason_t |
3896 | core_scsi3_pri_report_capabilities(struct se_cmd *cmd) | |
c66ac9db | 3897 | { |
5951146d | 3898 | struct se_device *dev = cmd->se_dev; |
0fd97ccf | 3899 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
05d1c7c0 | 3900 | unsigned char *buf; |
c66ac9db NB |
3901 | u16 add_len = 8; /* Hardcoded to 8. */ |
3902 | ||
3903 | if (cmd->data_length < 6) { | |
6708bb27 | 3904 | pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:" |
c66ac9db | 3905 | " %u too small\n", cmd->data_length); |
de103c93 | 3906 | return TCM_INVALID_CDB_FIELD; |
c66ac9db NB |
3907 | } |
3908 | ||
4949314c | 3909 | buf = transport_kmap_data_sg(cmd); |
de103c93 CH |
3910 | if (!buf) |
3911 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
05d1c7c0 | 3912 | |
c66ac9db NB |
3913 | buf[0] = ((add_len << 8) & 0xff); |
3914 | buf[1] = (add_len & 0xff); | |
3915 | buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */ | |
3916 | buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */ | |
3917 | buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */ | |
3918 | buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */ | |
3919 | /* | |
3920 | * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so | |
3921 | * set the TMV: Task Mask Valid bit. | |
3922 | */ | |
3923 | buf[3] |= 0x80; | |
3924 | /* | |
3925 | * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166 | |
3926 | */ | |
3927 | buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */ | |
3928 | /* | |
3929 | * PTPL_A: Persistence across Target Power Loss Active bit | |
3930 | */ | |
3931 | if (pr_tmpl->pr_aptpl_active) | |
3932 | buf[3] |= 0x01; | |
3933 | /* | |
3934 | * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167 | |
3935 | */ | |
3936 | buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ | |
3937 | buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */ | |
3938 | buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */ | |
3939 | buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */ | |
3940 | buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */ | |
3941 | buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ | |
3942 | ||
4949314c | 3943 | transport_kunmap_data_sg(cmd); |
05d1c7c0 | 3944 | |
c66ac9db NB |
3945 | return 0; |
3946 | } | |
3947 | ||
3948 | /* | |
3949 | * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS | |
3950 | * | |
3951 | * See spc4r17 section 6.13.5 Table 168 and 169 | |
3952 | */ | |
de103c93 CH |
3953 | static sense_reason_t |
3954 | core_scsi3_pri_read_full_status(struct se_cmd *cmd) | |
c66ac9db | 3955 | { |
0fd97ccf | 3956 | struct se_device *dev = cmd->se_dev; |
c66ac9db | 3957 | struct se_node_acl *se_nacl; |
c66ac9db NB |
3958 | struct se_portal_group *se_tpg; |
3959 | struct t10_pr_registration *pr_reg, *pr_reg_tmp; | |
0fd97ccf | 3960 | struct t10_reservation *pr_tmpl = &dev->t10_pr; |
05d1c7c0 | 3961 | unsigned char *buf; |
c66ac9db NB |
3962 | u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len; |
3963 | u32 off = 8; /* off into first Full Status descriptor */ | |
3964 | int format_code = 0; | |
3965 | ||
3966 | if (cmd->data_length < 8) { | |
6708bb27 | 3967 | pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u" |
c66ac9db | 3968 | " too small\n", cmd->data_length); |
de103c93 | 3969 | return TCM_INVALID_CDB_FIELD; |
c66ac9db NB |
3970 | } |
3971 | ||
4949314c | 3972 | buf = transport_kmap_data_sg(cmd); |
de103c93 CH |
3973 | if (!buf) |
3974 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
05d1c7c0 | 3975 | |
0fd97ccf CH |
3976 | buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); |
3977 | buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); | |
3978 | buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); | |
3979 | buf[3] = (dev->t10_pr.pr_generation & 0xff); | |
c66ac9db NB |
3980 | |
3981 | spin_lock(&pr_tmpl->registration_lock); | |
3982 | list_for_each_entry_safe(pr_reg, pr_reg_tmp, | |
3983 | &pr_tmpl->registration_list, pr_reg_list) { | |
3984 | ||
3985 | se_nacl = pr_reg->pr_reg_nacl; | |
3986 | se_tpg = pr_reg->pr_reg_nacl->se_tpg; | |
3987 | add_desc_len = 0; | |
3988 | ||
3989 | atomic_inc(&pr_reg->pr_res_holders); | |
3990 | smp_mb__after_atomic_inc(); | |
3991 | spin_unlock(&pr_tmpl->registration_lock); | |
3992 | /* | |
3993 | * Determine expected length of $FABRIC_MOD specific | |
3994 | * TransportID full status descriptor.. | |
3995 | */ | |
e3d6f909 | 3996 | exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len( |
c66ac9db NB |
3997 | se_tpg, se_nacl, pr_reg, &format_code); |
3998 | ||
3999 | if ((exp_desc_len + add_len) > cmd->data_length) { | |
6708bb27 | 4000 | pr_warn("SPC-3 PRIN READ_FULL_STATUS ran" |
c66ac9db NB |
4001 | " out of buffer: %d\n", cmd->data_length); |
4002 | spin_lock(&pr_tmpl->registration_lock); | |
4003 | atomic_dec(&pr_reg->pr_res_holders); | |
4004 | smp_mb__after_atomic_dec(); | |
4005 | break; | |
4006 | } | |
4007 | /* | |
4008 | * Set RESERVATION KEY | |
4009 | */ | |
4010 | buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); | |
4011 | buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); | |
4012 | buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); | |
4013 | buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); | |
4014 | buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); | |
4015 | buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); | |
4016 | buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); | |
4017 | buf[off++] = (pr_reg->pr_res_key & 0xff); | |
4018 | off += 4; /* Skip Over Reserved area */ | |
4019 | ||
4020 | /* | |
4021 | * Set ALL_TG_PT bit if PROUT SA REGISTER had this set. | |
4022 | */ | |
4023 | if (pr_reg->pr_reg_all_tg_pt) | |
4024 | buf[off] = 0x02; | |
4025 | /* | |
4026 | * The struct se_lun pointer will be present for the | |
4027 | * reservation holder for PR_HOLDER bit. | |
4028 | * | |
4029 | * Also, if this registration is the reservation | |
4030 | * holder, fill in SCOPE and TYPE in the next byte. | |
4031 | */ | |
4032 | if (pr_reg->pr_res_holder) { | |
4033 | buf[off++] |= 0x01; | |
4034 | buf[off++] = (pr_reg->pr_res_scope & 0xf0) | | |
4035 | (pr_reg->pr_res_type & 0x0f); | |
4036 | } else | |
4037 | off += 2; | |
4038 | ||
4039 | off += 4; /* Skip over reserved area */ | |
4040 | /* | |
4041 | * From spc4r17 6.3.15: | |
4042 | * | |
4043 | * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT | |
4044 | * IDENTIFIER field contains the relative port identifier (see | |
4045 | * 3.1.120) of the target port that is part of the I_T nexus | |
4046 | * described by this full status descriptor. If the ALL_TG_PT | |
4047 | * bit is set to one, the contents of the RELATIVE TARGET PORT | |
4048 | * IDENTIFIER field are not defined by this standard. | |
4049 | */ | |
6708bb27 | 4050 | if (!pr_reg->pr_reg_all_tg_pt) { |
c66ac9db NB |
4051 | struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep; |
4052 | ||
4053 | buf[off++] = ((port->sep_rtpi >> 8) & 0xff); | |
4054 | buf[off++] = (port->sep_rtpi & 0xff); | |
4055 | } else | |
35d1efe8 | 4056 | off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */ |
c66ac9db NB |
4057 | |
4058 | /* | |
4059 | * Now, have the $FABRIC_MOD fill in the protocol identifier | |
4060 | */ | |
e3d6f909 | 4061 | desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg, |
c66ac9db NB |
4062 | se_nacl, pr_reg, &format_code, &buf[off+4]); |
4063 | ||
4064 | spin_lock(&pr_tmpl->registration_lock); | |
4065 | atomic_dec(&pr_reg->pr_res_holders); | |
4066 | smp_mb__after_atomic_dec(); | |
4067 | /* | |
4068 | * Set the ADDITIONAL DESCRIPTOR LENGTH | |
4069 | */ | |
4070 | buf[off++] = ((desc_len >> 24) & 0xff); | |
4071 | buf[off++] = ((desc_len >> 16) & 0xff); | |
4072 | buf[off++] = ((desc_len >> 8) & 0xff); | |
4073 | buf[off++] = (desc_len & 0xff); | |
4074 | /* | |
4075 | * Size of full desctipor header minus TransportID | |
4076 | * containing $FABRIC_MOD specific) initiator device/port | |
4077 | * WWN information. | |
4078 | * | |
4079 | * See spc4r17 Section 6.13.5 Table 169 | |
4080 | */ | |
4081 | add_desc_len = (24 + desc_len); | |
4082 | ||
4083 | off += desc_len; | |
4084 | add_len += add_desc_len; | |
4085 | } | |
4086 | spin_unlock(&pr_tmpl->registration_lock); | |
4087 | /* | |
4088 | * Set ADDITIONAL_LENGTH | |
4089 | */ | |
4090 | buf[4] = ((add_len >> 24) & 0xff); | |
4091 | buf[5] = ((add_len >> 16) & 0xff); | |
4092 | buf[6] = ((add_len >> 8) & 0xff); | |
4093 | buf[7] = (add_len & 0xff); | |
4094 | ||
4949314c | 4095 | transport_kunmap_data_sg(cmd); |
05d1c7c0 | 4096 | |
c66ac9db NB |
4097 | return 0; |
4098 | } | |
4099 | ||
de103c93 CH |
4100 | sense_reason_t |
4101 | target_scsi3_emulate_pr_in(struct se_cmd *cmd) | |
c66ac9db | 4102 | { |
de103c93 | 4103 | sense_reason_t ret; |
e76a35d6 | 4104 | |
c66ac9db NB |
4105 | /* |
4106 | * Following spc2r20 5.5.1 Reservations overview: | |
4107 | * | |
4108 | * If a logical unit has been reserved by any RESERVE command and is | |
4109 | * still reserved by any initiator, all PERSISTENT RESERVE IN and all | |
4110 | * PERSISTENT RESERVE OUT commands shall conflict regardless of | |
4111 | * initiator or service action and shall terminate with a RESERVATION | |
4112 | * CONFLICT status. | |
4113 | */ | |
0fd97ccf | 4114 | if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { |
6708bb27 | 4115 | pr_err("Received PERSISTENT_RESERVE CDB while legacy" |
c66ac9db NB |
4116 | " SPC-2 reservation is held, returning" |
4117 | " RESERVATION_CONFLICT\n"); | |
de103c93 | 4118 | return TCM_RESERVATION_CONFLICT; |
c66ac9db NB |
4119 | } |
4120 | ||
617c0e06 CH |
4121 | switch (cmd->t_task_cdb[1] & 0x1f) { |
4122 | case PRI_READ_KEYS: | |
d29a5b6a CH |
4123 | ret = core_scsi3_pri_read_keys(cmd); |
4124 | break; | |
617c0e06 | 4125 | case PRI_READ_RESERVATION: |
d29a5b6a CH |
4126 | ret = core_scsi3_pri_read_reservation(cmd); |
4127 | break; | |
617c0e06 | 4128 | case PRI_REPORT_CAPABILITIES: |
d29a5b6a CH |
4129 | ret = core_scsi3_pri_report_capabilities(cmd); |
4130 | break; | |
617c0e06 | 4131 | case PRI_READ_FULL_STATUS: |
d29a5b6a CH |
4132 | ret = core_scsi3_pri_read_full_status(cmd); |
4133 | break; | |
617c0e06 CH |
4134 | default: |
4135 | pr_err("Unknown PERSISTENT_RESERVE_IN service" | |
4136 | " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f); | |
de103c93 | 4137 | return TCM_INVALID_CDB_FIELD; |
d29a5b6a CH |
4138 | } |
4139 | ||
6bb35e00 CH |
4140 | if (!ret) |
4141 | target_complete_cmd(cmd, GOOD); | |
d29a5b6a | 4142 | return ret; |
c66ac9db NB |
4143 | } |
4144 | ||
de103c93 CH |
4145 | sense_reason_t |
4146 | target_check_reservation(struct se_cmd *cmd) | |
c66ac9db | 4147 | { |
d977f437 | 4148 | struct se_device *dev = cmd->se_dev; |
de103c93 | 4149 | sense_reason_t ret; |
c66ac9db | 4150 | |
d977f437 CH |
4151 | if (!cmd->se_sess) |
4152 | return 0; | |
4153 | if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) | |
4154 | return 0; | |
4155 | if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) | |
4156 | return 0; | |
c66ac9db | 4157 | |
d977f437 CH |
4158 | spin_lock(&dev->dev_reservation_lock); |
4159 | if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) | |
4160 | ret = target_scsi2_reservation_check(cmd); | |
4161 | else | |
4162 | ret = target_scsi3_pr_reservation_check(cmd); | |
4163 | spin_unlock(&dev->dev_reservation_lock); | |
0fd97ccf | 4164 | |
d977f437 | 4165 | return ret; |
c66ac9db | 4166 | } |