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