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