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