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