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