Avoid reading past buffer when calling GETACL
[zen-stable.git] / drivers / target / target_core_pr.c
blob5371d10db3a897fbd757503386e74dd113c58b2b
1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
26 ******************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_pr.h"
42 #include "target_core_ua.h"
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 struct pr_transport_id_holder {
48 int dest_local_nexus;
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
56 int core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
58 char *buf,
59 u32 size)
61 if (!pr_reg->isid_present_at_reg)
62 return 0;
64 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
65 return 1;
68 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69 struct t10_pr_registration *, int);
71 static int core_scsi2_reservation_seq_non_holder(
72 struct se_cmd *cmd,
73 unsigned char *cdb,
74 u32 pr_reg_type)
76 switch (cdb[0]) {
77 case INQUIRY:
78 case RELEASE:
79 case RELEASE_10:
80 return 0;
81 default:
82 return 1;
85 return 1;
88 static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
90 struct se_device *dev = cmd->se_dev;
91 struct se_session *sess = cmd->se_sess;
92 int ret;
94 if (!sess)
95 return 0;
97 spin_lock(&dev->dev_reservation_lock);
98 if (!dev->dev_reserved_node_acl || !sess) {
99 spin_unlock(&dev->dev_reservation_lock);
100 return 0;
102 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103 spin_unlock(&dev->dev_reservation_lock);
104 return -EINVAL;
106 if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
107 spin_unlock(&dev->dev_reservation_lock);
108 return 0;
110 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
111 spin_unlock(&dev->dev_reservation_lock);
113 return ret;
116 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117 struct se_node_acl *, struct se_session *);
118 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
120 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
122 struct se_session *se_sess = cmd->se_sess;
123 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
124 struct t10_pr_registration *pr_reg;
125 struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
126 int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
127 int conflict = 0;
129 if (!crh)
130 return -EINVAL;
132 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
133 se_sess);
134 if (pr_reg) {
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
137 * behavior
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
156 if (pr_reg->pr_res_holder) {
157 core_scsi3_put_pr_reg(pr_reg);
158 return 1;
160 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
161 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
162 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
163 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
164 core_scsi3_put_pr_reg(pr_reg);
165 return 1;
167 core_scsi3_put_pr_reg(pr_reg);
168 conflict = 1;
169 } else {
171 * Following spc2r20 5.5.1 Reservations overview:
173 * If a logical unit has executed a PERSISTENT RESERVE OUT
174 * command with the REGISTER or the REGISTER AND IGNORE
175 * EXISTING KEY service action and is still registered by any
176 * initiator, all RESERVE commands and all RELEASE commands
177 * regardless of initiator shall conflict and shall terminate
178 * with a RESERVATION CONFLICT status.
180 spin_lock(&pr_tmpl->registration_lock);
181 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
182 spin_unlock(&pr_tmpl->registration_lock);
185 if (conflict) {
186 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
187 " while active SPC-3 registrations exist,"
188 " returning RESERVATION_CONFLICT\n");
189 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
190 return -EBUSY;
193 return 0;
196 int target_scsi2_reservation_release(struct se_task *task)
198 struct se_cmd *cmd = task->task_se_cmd;
199 struct se_device *dev = cmd->se_dev;
200 struct se_session *sess = cmd->se_sess;
201 struct se_portal_group *tpg = sess->se_tpg;
202 int ret = 0, rc;
204 if (!sess || !tpg)
205 goto out;
206 rc = target_check_scsi2_reservation_conflict(cmd);
207 if (rc == 1)
208 goto out;
209 else if (rc < 0) {
210 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
211 ret = -EINVAL;
212 goto out;
215 ret = 0;
216 spin_lock(&dev->dev_reservation_lock);
217 if (!dev->dev_reserved_node_acl || !sess)
218 goto out_unlock;
220 if (dev->dev_reserved_node_acl != sess->se_node_acl)
221 goto out_unlock;
223 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
224 goto out_unlock;
226 dev->dev_reserved_node_acl = NULL;
227 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
228 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
229 dev->dev_res_bin_isid = 0;
230 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
232 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
233 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
234 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
235 sess->se_node_acl->initiatorname);
237 out_unlock:
238 spin_unlock(&dev->dev_reservation_lock);
239 out:
240 if (!ret) {
241 task->task_scsi_status = GOOD;
242 transport_complete_task(task, 1);
244 return ret;
247 int target_scsi2_reservation_reserve(struct se_task *task)
249 struct se_cmd *cmd = task->task_se_cmd;
250 struct se_device *dev = cmd->se_dev;
251 struct se_session *sess = cmd->se_sess;
252 struct se_portal_group *tpg = sess->se_tpg;
253 int ret = 0, rc;
255 if ((cmd->t_task_cdb[1] & 0x01) &&
256 (cmd->t_task_cdb[1] & 0x02)) {
257 pr_err("LongIO and Obselete Bits set, returning"
258 " ILLEGAL_REQUEST\n");
259 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
260 ret = -EINVAL;
261 goto out;
264 * This is currently the case for target_core_mod passthrough struct se_cmd
265 * ops
267 if (!sess || !tpg)
268 goto out;
269 rc = target_check_scsi2_reservation_conflict(cmd);
270 if (rc == 1)
271 goto out;
272 else if (rc < 0) {
273 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
274 ret = -EINVAL;
275 goto out;
278 ret = 0;
279 spin_lock(&dev->dev_reservation_lock);
280 if (dev->dev_reserved_node_acl &&
281 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
282 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
283 tpg->se_tpg_tfo->get_fabric_name());
284 pr_err("Original reserver LUN: %u %s\n",
285 cmd->se_lun->unpacked_lun,
286 dev->dev_reserved_node_acl->initiatorname);
287 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
288 " from %s \n", cmd->se_lun->unpacked_lun,
289 cmd->se_deve->mapped_lun,
290 sess->se_node_acl->initiatorname);
291 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
292 ret = -EINVAL;
293 goto out_unlock;
296 dev->dev_reserved_node_acl = sess->se_node_acl;
297 dev->dev_flags |= DF_SPC2_RESERVATIONS;
298 if (sess->sess_bin_isid != 0) {
299 dev->dev_res_bin_isid = sess->sess_bin_isid;
300 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
302 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
303 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
304 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
305 sess->se_node_acl->initiatorname);
307 out_unlock:
308 spin_unlock(&dev->dev_reservation_lock);
309 out:
310 if (!ret) {
311 task->task_scsi_status = GOOD;
312 transport_complete_task(task, 1);
314 return ret;
319 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
321 * This function is called by those initiator ports who are *NOT*
322 * the active PR reservation holder when a reservation is present.
324 static int core_scsi3_pr_seq_non_holder(
325 struct se_cmd *cmd,
326 unsigned char *cdb,
327 u32 pr_reg_type)
329 struct se_dev_entry *se_deve;
330 struct se_session *se_sess = cmd->se_sess;
331 int other_cdb = 0, ignore_reg;
332 int registered_nexus = 0, ret = 1; /* Conflict by default */
333 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
334 int we = 0; /* Write Exclusive */
335 int legacy = 0; /* Act like a legacy device and return
336 * RESERVATION CONFLICT on some CDBs */
338 * A legacy SPC-2 reservation is being held.
340 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
341 return core_scsi2_reservation_seq_non_holder(cmd,
342 cdb, pr_reg_type);
344 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
346 * Determine if the registration should be ignored due to
347 * non-matching ISIDs in core_scsi3_pr_reservation_check().
349 ignore_reg = (pr_reg_type & 0x80000000);
350 if (ignore_reg)
351 pr_reg_type &= ~0x80000000;
353 switch (pr_reg_type) {
354 case PR_TYPE_WRITE_EXCLUSIVE:
355 we = 1;
356 case PR_TYPE_EXCLUSIVE_ACCESS:
358 * Some commands are only allowed for the persistent reservation
359 * holder.
361 if ((se_deve->def_pr_registered) && !(ignore_reg))
362 registered_nexus = 1;
363 break;
364 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
365 we = 1;
366 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
368 * Some commands are only allowed for registered I_T Nexuses.
370 reg_only = 1;
371 if ((se_deve->def_pr_registered) && !(ignore_reg))
372 registered_nexus = 1;
373 break;
374 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
375 we = 1;
376 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
378 * Each registered I_T Nexus is a reservation holder.
380 all_reg = 1;
381 if ((se_deve->def_pr_registered) && !(ignore_reg))
382 registered_nexus = 1;
383 break;
384 default:
385 return -EINVAL;
388 * Referenced from spc4r17 table 45 for *NON* PR holder access
390 switch (cdb[0]) {
391 case SECURITY_PROTOCOL_IN:
392 if (registered_nexus)
393 return 0;
394 ret = (we) ? 0 : 1;
395 break;
396 case MODE_SENSE:
397 case MODE_SENSE_10:
398 case READ_ATTRIBUTE:
399 case READ_BUFFER:
400 case RECEIVE_DIAGNOSTIC:
401 if (legacy) {
402 ret = 1;
403 break;
405 if (registered_nexus) {
406 ret = 0;
407 break;
409 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
410 break;
411 case PERSISTENT_RESERVE_OUT:
413 * This follows PERSISTENT_RESERVE_OUT service actions that
414 * are allowed in the presence of various reservations.
415 * See spc4r17, table 46
417 switch (cdb[1] & 0x1f) {
418 case PRO_CLEAR:
419 case PRO_PREEMPT:
420 case PRO_PREEMPT_AND_ABORT:
421 ret = (registered_nexus) ? 0 : 1;
422 break;
423 case PRO_REGISTER:
424 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
425 ret = 0;
426 break;
427 case PRO_REGISTER_AND_MOVE:
428 case PRO_RESERVE:
429 ret = 1;
430 break;
431 case PRO_RELEASE:
432 ret = (registered_nexus) ? 0 : 1;
433 break;
434 default:
435 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
436 " action: 0x%02x\n", cdb[1] & 0x1f);
437 return -EINVAL;
439 break;
440 case RELEASE:
441 case RELEASE_10:
442 /* Handled by CRH=1 in target_scsi2_reservation_release() */
443 ret = 0;
444 break;
445 case RESERVE:
446 case RESERVE_10:
447 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
448 ret = 0;
449 break;
450 case TEST_UNIT_READY:
451 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
452 break;
453 case MAINTENANCE_IN:
454 switch (cdb[1] & 0x1f) {
455 case MI_MANAGEMENT_PROTOCOL_IN:
456 if (registered_nexus) {
457 ret = 0;
458 break;
460 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
461 break;
462 case MI_REPORT_SUPPORTED_OPERATION_CODES:
463 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
464 if (legacy) {
465 ret = 1;
466 break;
468 if (registered_nexus) {
469 ret = 0;
470 break;
472 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
473 break;
474 case MI_REPORT_ALIASES:
475 case MI_REPORT_IDENTIFYING_INFORMATION:
476 case MI_REPORT_PRIORITY:
477 case MI_REPORT_TARGET_PGS:
478 case MI_REPORT_TIMESTAMP:
479 ret = 0; /* Allowed */
480 break;
481 default:
482 pr_err("Unknown MI Service Action: 0x%02x\n",
483 (cdb[1] & 0x1f));
484 return -EINVAL;
486 break;
487 case ACCESS_CONTROL_IN:
488 case ACCESS_CONTROL_OUT:
489 case INQUIRY:
490 case LOG_SENSE:
491 case READ_MEDIA_SERIAL_NUMBER:
492 case REPORT_LUNS:
493 case REQUEST_SENSE:
494 case PERSISTENT_RESERVE_IN:
495 ret = 0; /*/ Allowed CDBs */
496 break;
497 default:
498 other_cdb = 1;
499 break;
502 * Case where the CDB is explicitly allowed in the above switch
503 * statement.
505 if (!ret && !other_cdb) {
506 #if 0
507 pr_debug("Allowing explict CDB: 0x%02x for %s"
508 " reservation holder\n", cdb[0],
509 core_scsi3_pr_dump_type(pr_reg_type));
510 #endif
511 return ret;
514 * Check if write exclusive initiator ports *NOT* holding the
515 * WRITE_EXCLUSIVE_* reservation.
517 if ((we) && !(registered_nexus)) {
518 if (cmd->data_direction == DMA_TO_DEVICE) {
520 * Conflict for write exclusive
522 pr_debug("%s Conflict for unregistered nexus"
523 " %s CDB: 0x%02x to %s reservation\n",
524 transport_dump_cmd_direction(cmd),
525 se_sess->se_node_acl->initiatorname, cdb[0],
526 core_scsi3_pr_dump_type(pr_reg_type));
527 return 1;
528 } else {
530 * Allow non WRITE CDBs for all Write Exclusive
531 * PR TYPEs to pass for registered and
532 * non-registered_nexuxes NOT holding the reservation.
534 * We only make noise for the unregisterd nexuses,
535 * as we expect registered non-reservation holding
536 * nexuses to issue CDBs.
538 #if 0
539 if (!registered_nexus) {
540 pr_debug("Allowing implict CDB: 0x%02x"
541 " for %s reservation on unregistered"
542 " nexus\n", cdb[0],
543 core_scsi3_pr_dump_type(pr_reg_type));
545 #endif
546 return 0;
548 } else if ((reg_only) || (all_reg)) {
549 if (registered_nexus) {
551 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
552 * allow commands from registered nexuses.
554 #if 0
555 pr_debug("Allowing implict CDB: 0x%02x for %s"
556 " reservation\n", cdb[0],
557 core_scsi3_pr_dump_type(pr_reg_type));
558 #endif
559 return 0;
562 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
563 " for %s reservation\n", transport_dump_cmd_direction(cmd),
564 (registered_nexus) ? "" : "un",
565 se_sess->se_node_acl->initiatorname, cdb[0],
566 core_scsi3_pr_dump_type(pr_reg_type));
568 return 1; /* Conflict by default */
571 static u32 core_scsi3_pr_generation(struct se_device *dev)
573 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
574 u32 prg;
576 * PRGeneration field shall contain the value of a 32-bit wrapping
577 * counter mainted by the device server.
579 * Note that this is done regardless of Active Persist across
580 * Target PowerLoss (APTPL)
582 * See spc4r17 section 6.3.12 READ_KEYS service action
584 spin_lock(&dev->dev_reservation_lock);
585 prg = su_dev->t10_pr.pr_generation++;
586 spin_unlock(&dev->dev_reservation_lock);
588 return prg;
591 static int core_scsi3_pr_reservation_check(
592 struct se_cmd *cmd,
593 u32 *pr_reg_type)
595 struct se_device *dev = cmd->se_dev;
596 struct se_session *sess = cmd->se_sess;
597 int ret;
599 if (!sess)
600 return 0;
602 * A legacy SPC-2 reservation is being held.
604 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
605 return core_scsi2_reservation_check(cmd, pr_reg_type);
607 spin_lock(&dev->dev_reservation_lock);
608 if (!dev->dev_pr_res_holder) {
609 spin_unlock(&dev->dev_reservation_lock);
610 return 0;
612 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
613 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
614 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
615 spin_unlock(&dev->dev_reservation_lock);
616 return -EINVAL;
618 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
619 spin_unlock(&dev->dev_reservation_lock);
620 return 0;
622 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
623 sess->sess_bin_isid) ? 0 : -EINVAL;
625 * Use bit in *pr_reg_type to notify ISID mismatch in
626 * core_scsi3_pr_seq_non_holder().
628 if (ret != 0)
629 *pr_reg_type |= 0x80000000;
630 spin_unlock(&dev->dev_reservation_lock);
632 return ret;
635 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
636 struct se_device *dev,
637 struct se_node_acl *nacl,
638 struct se_dev_entry *deve,
639 unsigned char *isid,
640 u64 sa_res_key,
641 int all_tg_pt,
642 int aptpl)
644 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
645 struct t10_pr_registration *pr_reg;
647 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
648 if (!pr_reg) {
649 pr_err("Unable to allocate struct t10_pr_registration\n");
650 return NULL;
653 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
654 GFP_ATOMIC);
655 if (!pr_reg->pr_aptpl_buf) {
656 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
657 kmem_cache_free(t10_pr_reg_cache, pr_reg);
658 return NULL;
661 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
662 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
663 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
664 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
665 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
666 atomic_set(&pr_reg->pr_res_holders, 0);
667 pr_reg->pr_reg_nacl = nacl;
668 pr_reg->pr_reg_deve = deve;
669 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
670 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
671 pr_reg->pr_res_key = sa_res_key;
672 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
673 pr_reg->pr_reg_aptpl = aptpl;
674 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
676 * If an ISID value for this SCSI Initiator Port exists,
677 * save it to the registration now.
679 if (isid != NULL) {
680 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
681 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
682 pr_reg->isid_present_at_reg = 1;
685 return pr_reg;
688 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
689 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
692 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
693 * modes.
695 static struct t10_pr_registration *__core_scsi3_alloc_registration(
696 struct se_device *dev,
697 struct se_node_acl *nacl,
698 struct se_dev_entry *deve,
699 unsigned char *isid,
700 u64 sa_res_key,
701 int all_tg_pt,
702 int aptpl)
704 struct se_dev_entry *deve_tmp;
705 struct se_node_acl *nacl_tmp;
706 struct se_port *port, *port_tmp;
707 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
708 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
709 int ret;
711 * Create a registration for the I_T Nexus upon which the
712 * PROUT REGISTER was received.
714 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
715 sa_res_key, all_tg_pt, aptpl);
716 if (!pr_reg)
717 return NULL;
719 * Return pointer to pr_reg for ALL_TG_PT=0
721 if (!all_tg_pt)
722 return pr_reg;
724 * Create list of matching SCSI Initiator Port registrations
725 * for ALL_TG_PT=1
727 spin_lock(&dev->se_port_lock);
728 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
729 atomic_inc(&port->sep_tg_pt_ref_cnt);
730 smp_mb__after_atomic_inc();
731 spin_unlock(&dev->se_port_lock);
733 spin_lock_bh(&port->sep_alua_lock);
734 list_for_each_entry(deve_tmp, &port->sep_alua_list,
735 alua_port_list) {
737 * This pointer will be NULL for demo mode MappedLUNs
738 * that have not been make explict via a ConfigFS
739 * MappedLUN group for the SCSI Initiator Node ACL.
741 if (!deve_tmp->se_lun_acl)
742 continue;
744 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
746 * Skip the matching struct se_node_acl that is allocated
747 * above..
749 if (nacl == nacl_tmp)
750 continue;
752 * Only perform PR registrations for target ports on
753 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
754 * arrived.
756 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
757 continue;
759 * Look for a matching Initiator Node ACL in ASCII format
761 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
762 continue;
764 atomic_inc(&deve_tmp->pr_ref_count);
765 smp_mb__after_atomic_inc();
766 spin_unlock_bh(&port->sep_alua_lock);
768 * Grab a configfs group dependency that is released
769 * for the exception path at label out: below, or upon
770 * completion of adding ALL_TG_PT=1 registrations in
771 * __core_scsi3_add_registration()
773 ret = core_scsi3_lunacl_depend_item(deve_tmp);
774 if (ret < 0) {
775 pr_err("core_scsi3_lunacl_depend"
776 "_item() failed\n");
777 atomic_dec(&port->sep_tg_pt_ref_cnt);
778 smp_mb__after_atomic_dec();
779 atomic_dec(&deve_tmp->pr_ref_count);
780 smp_mb__after_atomic_dec();
781 goto out;
784 * Located a matching SCSI Initiator Port on a different
785 * port, allocate the pr_reg_atp and attach it to the
786 * pr_reg->pr_reg_atp_list that will be processed once
787 * the original *pr_reg is processed in
788 * __core_scsi3_add_registration()
790 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
791 nacl_tmp, deve_tmp, NULL,
792 sa_res_key, all_tg_pt, aptpl);
793 if (!pr_reg_atp) {
794 atomic_dec(&port->sep_tg_pt_ref_cnt);
795 smp_mb__after_atomic_dec();
796 atomic_dec(&deve_tmp->pr_ref_count);
797 smp_mb__after_atomic_dec();
798 core_scsi3_lunacl_undepend_item(deve_tmp);
799 goto out;
802 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
803 &pr_reg->pr_reg_atp_list);
804 spin_lock_bh(&port->sep_alua_lock);
806 spin_unlock_bh(&port->sep_alua_lock);
808 spin_lock(&dev->se_port_lock);
809 atomic_dec(&port->sep_tg_pt_ref_cnt);
810 smp_mb__after_atomic_dec();
812 spin_unlock(&dev->se_port_lock);
814 return pr_reg;
815 out:
816 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
817 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
818 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
819 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
820 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
822 kmem_cache_free(t10_pr_reg_cache, pr_reg);
823 return NULL;
826 int core_scsi3_alloc_aptpl_registration(
827 struct t10_reservation *pr_tmpl,
828 u64 sa_res_key,
829 unsigned char *i_port,
830 unsigned char *isid,
831 u32 mapped_lun,
832 unsigned char *t_port,
833 u16 tpgt,
834 u32 target_lun,
835 int res_holder,
836 int all_tg_pt,
837 u8 type)
839 struct t10_pr_registration *pr_reg;
841 if (!i_port || !t_port || !sa_res_key) {
842 pr_err("Illegal parameters for APTPL registration\n");
843 return -EINVAL;
846 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
847 if (!pr_reg) {
848 pr_err("Unable to allocate struct t10_pr_registration\n");
849 return -ENOMEM;
851 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
853 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
854 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
855 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
856 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
857 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
858 atomic_set(&pr_reg->pr_res_holders, 0);
859 pr_reg->pr_reg_nacl = NULL;
860 pr_reg->pr_reg_deve = NULL;
861 pr_reg->pr_res_mapped_lun = mapped_lun;
862 pr_reg->pr_aptpl_target_lun = target_lun;
863 pr_reg->pr_res_key = sa_res_key;
864 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
865 pr_reg->pr_reg_aptpl = 1;
866 pr_reg->pr_reg_tg_pt_lun = NULL;
867 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
868 pr_reg->pr_res_type = type;
870 * If an ISID value had been saved in APTPL metadata for this
871 * SCSI Initiator Port, restore it now.
873 if (isid != NULL) {
874 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
875 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
876 pr_reg->isid_present_at_reg = 1;
879 * Copy the i_port and t_port information from caller.
881 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
882 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
883 pr_reg->pr_reg_tpgt = tpgt;
885 * Set pr_res_holder from caller, the pr_reg who is the reservation
886 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
887 * the Initiator Node LUN ACL from the fabric module is created for
888 * this registration.
890 pr_reg->pr_res_holder = res_holder;
892 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
893 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
894 " metadata\n", (res_holder) ? "+reservation" : "");
895 return 0;
898 static void core_scsi3_aptpl_reserve(
899 struct se_device *dev,
900 struct se_portal_group *tpg,
901 struct se_node_acl *node_acl,
902 struct t10_pr_registration *pr_reg)
904 char i_buf[PR_REG_ISID_ID_LEN];
905 int prf_isid;
907 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
908 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
909 PR_REG_ISID_ID_LEN);
911 spin_lock(&dev->dev_reservation_lock);
912 dev->dev_pr_res_holder = pr_reg;
913 spin_unlock(&dev->dev_reservation_lock);
915 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
916 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
917 tpg->se_tpg_tfo->get_fabric_name(),
918 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
919 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
920 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
921 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
922 (prf_isid) ? &i_buf[0] : "");
925 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
926 struct t10_pr_registration *, int, int);
928 static int __core_scsi3_check_aptpl_registration(
929 struct se_device *dev,
930 struct se_portal_group *tpg,
931 struct se_lun *lun,
932 u32 target_lun,
933 struct se_node_acl *nacl,
934 struct se_dev_entry *deve)
936 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
937 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
938 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
939 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
940 u16 tpgt;
942 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
943 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
945 * Copy Initiator Port information from struct se_node_acl
947 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
948 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
949 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
950 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
952 * Look for the matching registrations+reservation from those
953 * created from APTPL metadata. Note that multiple registrations
954 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
955 * TransportIDs.
957 spin_lock(&pr_tmpl->aptpl_reg_lock);
958 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
959 pr_reg_aptpl_list) {
960 if (!strcmp(pr_reg->pr_iport, i_port) &&
961 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
962 !(strcmp(pr_reg->pr_tport, t_port)) &&
963 (pr_reg->pr_reg_tpgt == tpgt) &&
964 (pr_reg->pr_aptpl_target_lun == target_lun)) {
966 pr_reg->pr_reg_nacl = nacl;
967 pr_reg->pr_reg_deve = deve;
968 pr_reg->pr_reg_tg_pt_lun = lun;
970 list_del(&pr_reg->pr_reg_aptpl_list);
971 spin_unlock(&pr_tmpl->aptpl_reg_lock);
973 * At this point all of the pointers in *pr_reg will
974 * be setup, so go ahead and add the registration.
977 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
979 * If this registration is the reservation holder,
980 * make that happen now..
982 if (pr_reg->pr_res_holder)
983 core_scsi3_aptpl_reserve(dev, tpg,
984 nacl, pr_reg);
986 * Reenable pr_aptpl_active to accept new metadata
987 * updates once the SCSI device is active again..
989 spin_lock(&pr_tmpl->aptpl_reg_lock);
990 pr_tmpl->pr_aptpl_active = 1;
993 spin_unlock(&pr_tmpl->aptpl_reg_lock);
995 return 0;
998 int core_scsi3_check_aptpl_registration(
999 struct se_device *dev,
1000 struct se_portal_group *tpg,
1001 struct se_lun *lun,
1002 struct se_lun_acl *lun_acl)
1004 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1005 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
1006 struct se_dev_entry *deve = &nacl->device_list[lun_acl->mapped_lun];
1008 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1009 return 0;
1011 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1012 lun->unpacked_lun, nacl, deve);
1015 static void __core_scsi3_dump_registration(
1016 struct target_core_fabric_ops *tfo,
1017 struct se_device *dev,
1018 struct se_node_acl *nacl,
1019 struct t10_pr_registration *pr_reg,
1020 int register_type)
1022 struct se_portal_group *se_tpg = nacl->se_tpg;
1023 char i_buf[PR_REG_ISID_ID_LEN];
1024 int prf_isid;
1026 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1027 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1028 PR_REG_ISID_ID_LEN);
1030 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1031 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1032 "_AND_MOVE" : (register_type == 1) ?
1033 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1034 (prf_isid) ? i_buf : "");
1035 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1036 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1037 tfo->tpg_get_tag(se_tpg));
1038 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1039 " Port(s)\n", tfo->get_fabric_name(),
1040 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1041 dev->transport->name);
1042 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1043 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1044 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1045 pr_reg->pr_reg_aptpl);
1049 * this function can be called with struct se_device->dev_reservation_lock
1050 * when register_move = 1
1052 static void __core_scsi3_add_registration(
1053 struct se_device *dev,
1054 struct se_node_acl *nacl,
1055 struct t10_pr_registration *pr_reg,
1056 int register_type,
1057 int register_move)
1059 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1060 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1061 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1062 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1065 * Increment PRgeneration counter for struct se_device upon a successful
1066 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1068 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1069 * action, the struct se_device->dev_reservation_lock will already be held,
1070 * so we do not call core_scsi3_pr_generation() which grabs the lock
1071 * for the REGISTER.
1073 pr_reg->pr_res_generation = (register_move) ?
1074 su_dev->t10_pr.pr_generation++ :
1075 core_scsi3_pr_generation(dev);
1077 spin_lock(&pr_tmpl->registration_lock);
1078 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1079 pr_reg->pr_reg_deve->def_pr_registered = 1;
1081 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1082 spin_unlock(&pr_tmpl->registration_lock);
1084 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1086 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1087 return;
1089 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1090 * allocated in __core_scsi3_alloc_registration()
1092 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1093 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1094 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1096 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1098 spin_lock(&pr_tmpl->registration_lock);
1099 list_add_tail(&pr_reg_tmp->pr_reg_list,
1100 &pr_tmpl->registration_list);
1101 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1103 __core_scsi3_dump_registration(tfo, dev,
1104 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1105 register_type);
1106 spin_unlock(&pr_tmpl->registration_lock);
1108 * Drop configfs group dependency reference from
1109 * __core_scsi3_alloc_registration()
1111 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1115 static int core_scsi3_alloc_registration(
1116 struct se_device *dev,
1117 struct se_node_acl *nacl,
1118 struct se_dev_entry *deve,
1119 unsigned char *isid,
1120 u64 sa_res_key,
1121 int all_tg_pt,
1122 int aptpl,
1123 int register_type,
1124 int register_move)
1126 struct t10_pr_registration *pr_reg;
1128 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1129 sa_res_key, all_tg_pt, aptpl);
1130 if (!pr_reg)
1131 return -EPERM;
1133 __core_scsi3_add_registration(dev, nacl, pr_reg,
1134 register_type, register_move);
1135 return 0;
1138 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1139 struct se_device *dev,
1140 struct se_node_acl *nacl,
1141 unsigned char *isid)
1143 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1144 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1145 struct se_portal_group *tpg;
1147 spin_lock(&pr_tmpl->registration_lock);
1148 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1149 &pr_tmpl->registration_list, pr_reg_list) {
1151 * First look for a matching struct se_node_acl
1153 if (pr_reg->pr_reg_nacl != nacl)
1154 continue;
1156 tpg = pr_reg->pr_reg_nacl->se_tpg;
1158 * If this registration does NOT contain a fabric provided
1159 * ISID, then we have found a match.
1161 if (!pr_reg->isid_present_at_reg) {
1163 * Determine if this SCSI device server requires that
1164 * SCSI Intiatior TransportID w/ ISIDs is enforced
1165 * for fabric modules (iSCSI) requiring them.
1167 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1168 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
1169 continue;
1171 atomic_inc(&pr_reg->pr_res_holders);
1172 smp_mb__after_atomic_inc();
1173 spin_unlock(&pr_tmpl->registration_lock);
1174 return pr_reg;
1177 * If the *pr_reg contains a fabric defined ISID for multi-value
1178 * SCSI Initiator Port TransportIDs, then we expect a valid
1179 * matching ISID to be provided by the local SCSI Initiator Port.
1181 if (!isid)
1182 continue;
1183 if (strcmp(isid, pr_reg->pr_reg_isid))
1184 continue;
1186 atomic_inc(&pr_reg->pr_res_holders);
1187 smp_mb__after_atomic_inc();
1188 spin_unlock(&pr_tmpl->registration_lock);
1189 return pr_reg;
1191 spin_unlock(&pr_tmpl->registration_lock);
1193 return NULL;
1196 static 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)
1201 struct se_portal_group *tpg = nacl->se_tpg;
1202 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1204 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1205 memset(&buf[0], 0, PR_REG_ISID_LEN);
1206 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1207 PR_REG_ISID_LEN);
1208 isid_ptr = &buf[0];
1211 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1214 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1216 atomic_dec(&pr_reg->pr_res_holders);
1217 smp_mb__after_atomic_dec();
1220 static int core_scsi3_check_implict_release(
1221 struct se_device *dev,
1222 struct t10_pr_registration *pr_reg)
1224 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1225 struct t10_pr_registration *pr_res_holder;
1226 int ret = 0;
1228 spin_lock(&dev->dev_reservation_lock);
1229 pr_res_holder = dev->dev_pr_res_holder;
1230 if (!pr_res_holder) {
1231 spin_unlock(&dev->dev_reservation_lock);
1232 return ret;
1234 if (pr_res_holder == pr_reg) {
1236 * Perform an implict RELEASE if the registration that
1237 * is being released is holding the reservation.
1239 * From spc4r17, section 5.7.11.1:
1241 * e) If the I_T nexus is the persistent reservation holder
1242 * and the persistent reservation is not an all registrants
1243 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1244 * service action or REGISTER AND IGNORE EXISTING KEY
1245 * service action with the SERVICE ACTION RESERVATION KEY
1246 * field set to zero (see 5.7.11.3).
1248 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1249 ret = 1;
1251 * For 'All Registrants' reservation types, all existing
1252 * registrations are still processed as reservation holders
1253 * in core_scsi3_pr_seq_non_holder() after the initial
1254 * reservation holder is implictly released here.
1256 } else if (pr_reg->pr_reg_all_tg_pt &&
1257 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1258 pr_reg->pr_reg_nacl->initiatorname)) &&
1259 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1260 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1261 " UNREGISTER while existing reservation with matching"
1262 " key 0x%016Lx is present from another SCSI Initiator"
1263 " Port\n", pr_reg->pr_res_key);
1264 ret = -EPERM;
1266 spin_unlock(&dev->dev_reservation_lock);
1268 return ret;
1272 * Called with struct t10_reservation->registration_lock held.
1274 static void __core_scsi3_free_registration(
1275 struct se_device *dev,
1276 struct t10_pr_registration *pr_reg,
1277 struct list_head *preempt_and_abort_list,
1278 int dec_holders)
1280 struct target_core_fabric_ops *tfo =
1281 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1282 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1283 char i_buf[PR_REG_ISID_ID_LEN];
1284 int prf_isid;
1286 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1287 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1288 PR_REG_ISID_ID_LEN);
1290 pr_reg->pr_reg_deve->def_pr_registered = 0;
1291 pr_reg->pr_reg_deve->pr_res_key = 0;
1292 list_del(&pr_reg->pr_reg_list);
1294 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1295 * so call core_scsi3_put_pr_reg() to decrement our reference.
1297 if (dec_holders)
1298 core_scsi3_put_pr_reg(pr_reg);
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.
1305 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1306 spin_unlock(&pr_tmpl->registration_lock);
1307 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1308 tfo->get_fabric_name());
1309 cpu_relax();
1310 spin_lock(&pr_tmpl->registration_lock);
1313 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1314 " Node: %s%s\n", tfo->get_fabric_name(),
1315 pr_reg->pr_reg_nacl->initiatorname,
1316 (prf_isid) ? &i_buf[0] : "");
1317 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1318 " Port(s)\n", tfo->get_fabric_name(),
1319 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1320 dev->transport->name);
1321 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1322 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1323 pr_reg->pr_res_generation);
1325 if (!preempt_and_abort_list) {
1326 pr_reg->pr_reg_deve = NULL;
1327 pr_reg->pr_reg_nacl = NULL;
1328 kfree(pr_reg->pr_aptpl_buf);
1329 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1330 return;
1333 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1334 * are released once the ABORT_TASK_SET has completed..
1336 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1339 void core_scsi3_free_pr_reg_from_nacl(
1340 struct se_device *dev,
1341 struct se_node_acl *nacl)
1343 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1344 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1346 * If the passed se_node_acl matches the reservation holder,
1347 * release the reservation.
1349 spin_lock(&dev->dev_reservation_lock);
1350 pr_res_holder = dev->dev_pr_res_holder;
1351 if ((pr_res_holder != NULL) &&
1352 (pr_res_holder->pr_reg_nacl == nacl))
1353 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1354 spin_unlock(&dev->dev_reservation_lock);
1356 * Release any registration associated with the struct se_node_acl.
1358 spin_lock(&pr_tmpl->registration_lock);
1359 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1360 &pr_tmpl->registration_list, pr_reg_list) {
1362 if (pr_reg->pr_reg_nacl != nacl)
1363 continue;
1365 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1367 spin_unlock(&pr_tmpl->registration_lock);
1370 void core_scsi3_free_all_registrations(
1371 struct se_device *dev)
1373 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
1374 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1376 spin_lock(&dev->dev_reservation_lock);
1377 pr_res_holder = dev->dev_pr_res_holder;
1378 if (pr_res_holder != NULL) {
1379 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1380 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1381 pr_res_holder, 0);
1383 spin_unlock(&dev->dev_reservation_lock);
1385 spin_lock(&pr_tmpl->registration_lock);
1386 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1387 &pr_tmpl->registration_list, pr_reg_list) {
1389 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1391 spin_unlock(&pr_tmpl->registration_lock);
1393 spin_lock(&pr_tmpl->aptpl_reg_lock);
1394 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1395 pr_reg_aptpl_list) {
1396 list_del(&pr_reg->pr_reg_aptpl_list);
1397 kfree(pr_reg->pr_aptpl_buf);
1398 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1400 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1403 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1405 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1406 &tpg->tpg_group.cg_item);
1409 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1411 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1412 &tpg->tpg_group.cg_item);
1414 atomic_dec(&tpg->tpg_pr_ref_count);
1415 smp_mb__after_atomic_dec();
1418 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1420 struct se_portal_group *tpg = nacl->se_tpg;
1422 if (nacl->dynamic_node_acl)
1423 return 0;
1425 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1426 &nacl->acl_group.cg_item);
1429 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1431 struct se_portal_group *tpg = nacl->se_tpg;
1433 if (nacl->dynamic_node_acl) {
1434 atomic_dec(&nacl->acl_pr_ref_count);
1435 smp_mb__after_atomic_dec();
1436 return;
1439 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1440 &nacl->acl_group.cg_item);
1442 atomic_dec(&nacl->acl_pr_ref_count);
1443 smp_mb__after_atomic_dec();
1446 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1448 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1449 struct se_node_acl *nacl;
1450 struct se_portal_group *tpg;
1452 * For nacl->dynamic_node_acl=1
1454 if (!lun_acl)
1455 return 0;
1457 nacl = lun_acl->se_lun_nacl;
1458 tpg = nacl->se_tpg;
1460 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1461 &lun_acl->se_lun_group.cg_item);
1464 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1466 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1467 struct se_node_acl *nacl;
1468 struct se_portal_group *tpg;
1470 * For nacl->dynamic_node_acl=1
1472 if (!lun_acl) {
1473 atomic_dec(&se_deve->pr_ref_count);
1474 smp_mb__after_atomic_dec();
1475 return;
1477 nacl = lun_acl->se_lun_nacl;
1478 tpg = nacl->se_tpg;
1480 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1481 &lun_acl->se_lun_group.cg_item);
1483 atomic_dec(&se_deve->pr_ref_count);
1484 smp_mb__after_atomic_dec();
1487 static int core_scsi3_decode_spec_i_port(
1488 struct se_cmd *cmd,
1489 struct se_portal_group *tpg,
1490 unsigned char *l_isid,
1491 u64 sa_res_key,
1492 int all_tg_pt,
1493 int aptpl)
1495 struct se_device *dev = cmd->se_dev;
1496 struct se_port *tmp_port;
1497 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1498 struct se_session *se_sess = cmd->se_sess;
1499 struct se_node_acl *dest_node_acl = NULL;
1500 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1501 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1502 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1503 struct list_head tid_dest_list;
1504 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1505 struct target_core_fabric_ops *tmp_tf_ops;
1506 unsigned char *buf;
1507 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1508 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1509 u32 tpdl, tid_len = 0;
1510 int ret, dest_local_nexus, prf_isid;
1511 u32 dest_rtpi = 0;
1513 memset(dest_iport, 0, 64);
1514 INIT_LIST_HEAD(&tid_dest_list);
1516 local_se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1518 * Allocate a struct pr_transport_id_holder and setup the
1519 * local_node_acl and local_se_deve pointers and add to
1520 * struct list_head tid_dest_list for add registration
1521 * processing in the loop of tid_dest_list below.
1523 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1524 if (!tidh_new) {
1525 pr_err("Unable to allocate tidh_new\n");
1526 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1527 return -EINVAL;
1529 INIT_LIST_HEAD(&tidh_new->dest_list);
1530 tidh_new->dest_tpg = tpg;
1531 tidh_new->dest_node_acl = se_sess->se_node_acl;
1532 tidh_new->dest_se_deve = local_se_deve;
1534 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1535 se_sess->se_node_acl, local_se_deve, l_isid,
1536 sa_res_key, all_tg_pt, aptpl);
1537 if (!local_pr_reg) {
1538 kfree(tidh_new);
1539 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1540 return -ENOMEM;
1542 tidh_new->dest_pr_reg = local_pr_reg;
1544 * The local I_T nexus does not hold any configfs dependances,
1545 * so we set tid_h->dest_local_nexus=1 to prevent the
1546 * configfs_undepend_item() calls in the tid_dest_list loops below.
1548 tidh_new->dest_local_nexus = 1;
1549 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1551 buf = transport_kmap_data_sg(cmd);
1553 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1554 * first extract TransportID Parameter Data Length, and make sure
1555 * the value matches up to the SCSI expected data transfer length.
1557 tpdl = (buf[24] & 0xff) << 24;
1558 tpdl |= (buf[25] & 0xff) << 16;
1559 tpdl |= (buf[26] & 0xff) << 8;
1560 tpdl |= buf[27] & 0xff;
1562 if ((tpdl + 28) != cmd->data_length) {
1563 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1564 " does not equal CDB data_length: %u\n", tpdl,
1565 cmd->data_length);
1566 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1567 ret = -EINVAL;
1568 goto out;
1571 * Start processing the received transport IDs using the
1572 * receiving I_T Nexus portal's fabric dependent methods to
1573 * obtain the SCSI Initiator Port/Device Identifiers.
1575 ptr = &buf[28];
1577 while (tpdl > 0) {
1578 proto_ident = (ptr[0] & 0x0f);
1579 dest_tpg = NULL;
1581 spin_lock(&dev->se_port_lock);
1582 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1583 tmp_tpg = tmp_port->sep_tpg;
1584 if (!tmp_tpg)
1585 continue;
1586 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1587 if (!tmp_tf_ops)
1588 continue;
1589 if (!tmp_tf_ops->get_fabric_proto_ident ||
1590 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1591 continue;
1593 * Look for the matching proto_ident provided by
1594 * the received TransportID
1596 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1597 if (tmp_proto_ident != proto_ident)
1598 continue;
1599 dest_rtpi = tmp_port->sep_rtpi;
1601 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1602 tmp_tpg, (const char *)ptr, &tid_len,
1603 &iport_ptr);
1604 if (!i_str)
1605 continue;
1607 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1608 smp_mb__after_atomic_inc();
1609 spin_unlock(&dev->se_port_lock);
1611 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1612 if (ret != 0) {
1613 pr_err(" core_scsi3_tpg_depend_item()"
1614 " for tmp_tpg\n");
1615 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1616 smp_mb__after_atomic_dec();
1617 cmd->scsi_sense_reason =
1618 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1619 ret = -EINVAL;
1620 goto out;
1623 * Locate the desination initiator ACL to be registered
1624 * from the decoded fabric module specific TransportID
1625 * at *i_str.
1627 spin_lock_irq(&tmp_tpg->acl_node_lock);
1628 dest_node_acl = __core_tpg_get_initiator_node_acl(
1629 tmp_tpg, i_str);
1630 if (dest_node_acl) {
1631 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1632 smp_mb__after_atomic_inc();
1634 spin_unlock_irq(&tmp_tpg->acl_node_lock);
1636 if (!dest_node_acl) {
1637 core_scsi3_tpg_undepend_item(tmp_tpg);
1638 spin_lock(&dev->se_port_lock);
1639 continue;
1642 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1643 if (ret != 0) {
1644 pr_err("configfs_depend_item() failed"
1645 " for dest_node_acl->acl_group\n");
1646 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1647 smp_mb__after_atomic_dec();
1648 core_scsi3_tpg_undepend_item(tmp_tpg);
1649 cmd->scsi_sense_reason =
1650 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1651 ret = -EINVAL;
1652 goto out;
1655 dest_tpg = tmp_tpg;
1656 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1657 " %s Port RTPI: %hu\n",
1658 dest_tpg->se_tpg_tfo->get_fabric_name(),
1659 dest_node_acl->initiatorname, dest_rtpi);
1661 spin_lock(&dev->se_port_lock);
1662 break;
1664 spin_unlock(&dev->se_port_lock);
1666 if (!dest_tpg) {
1667 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1668 " dest_tpg\n");
1669 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1670 ret = -EINVAL;
1671 goto out;
1673 #if 0
1674 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1675 " tid_len: %d for %s + %s\n",
1676 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1677 tpdl, tid_len, i_str, iport_ptr);
1678 #endif
1679 if (tid_len > tpdl) {
1680 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1681 " %u for Transport ID: %s\n", tid_len, ptr);
1682 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1683 core_scsi3_tpg_undepend_item(dest_tpg);
1684 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1685 ret = -EINVAL;
1686 goto out;
1689 * Locate the desintation struct se_dev_entry pointer for matching
1690 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1691 * Target Port.
1693 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1694 dest_rtpi);
1695 if (!dest_se_deve) {
1696 pr_err("Unable to locate %s dest_se_deve"
1697 " from destination RTPI: %hu\n",
1698 dest_tpg->se_tpg_tfo->get_fabric_name(),
1699 dest_rtpi);
1701 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1702 core_scsi3_tpg_undepend_item(dest_tpg);
1703 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1704 ret = -EINVAL;
1705 goto out;
1708 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1709 if (ret < 0) {
1710 pr_err("core_scsi3_lunacl_depend_item()"
1711 " failed\n");
1712 atomic_dec(&dest_se_deve->pr_ref_count);
1713 smp_mb__after_atomic_dec();
1714 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1715 core_scsi3_tpg_undepend_item(dest_tpg);
1716 cmd->scsi_sense_reason =
1717 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1718 ret = -EINVAL;
1719 goto out;
1721 #if 0
1722 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1723 " dest_se_deve mapped_lun: %u\n",
1724 dest_tpg->se_tpg_tfo->get_fabric_name(),
1725 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1726 #endif
1728 * Skip any TransportIDs that already have a registration for
1729 * this target port.
1731 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1732 iport_ptr);
1733 if (pr_reg_e) {
1734 core_scsi3_put_pr_reg(pr_reg_e);
1735 core_scsi3_lunacl_undepend_item(dest_se_deve);
1736 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1737 core_scsi3_tpg_undepend_item(dest_tpg);
1738 ptr += tid_len;
1739 tpdl -= tid_len;
1740 tid_len = 0;
1741 continue;
1744 * Allocate a struct pr_transport_id_holder and setup
1745 * the dest_node_acl and dest_se_deve pointers for the
1746 * loop below.
1748 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1749 GFP_KERNEL);
1750 if (!tidh_new) {
1751 pr_err("Unable to allocate tidh_new\n");
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 cmd->scsi_sense_reason =
1756 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1757 ret = -ENOMEM;
1758 goto out;
1760 INIT_LIST_HEAD(&tidh_new->dest_list);
1761 tidh_new->dest_tpg = dest_tpg;
1762 tidh_new->dest_node_acl = dest_node_acl;
1763 tidh_new->dest_se_deve = dest_se_deve;
1766 * Allocate, but do NOT add the registration for the
1767 * TransportID referenced SCSI Initiator port. This
1768 * done because of the following from spc4r17 in section
1769 * 6.14.3 wrt SPEC_I_PT:
1771 * "If a registration fails for any initiator port (e.g., if th
1772 * logical unit does not have enough resources available to
1773 * hold the registration information), no registrations shall be
1774 * made, and the command shall be terminated with
1775 * CHECK CONDITION status."
1777 * That means we call __core_scsi3_alloc_registration() here,
1778 * and then call __core_scsi3_add_registration() in the
1779 * 2nd loop which will never fail.
1781 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1782 dest_node_acl, dest_se_deve, iport_ptr,
1783 sa_res_key, all_tg_pt, aptpl);
1784 if (!dest_pr_reg) {
1785 core_scsi3_lunacl_undepend_item(dest_se_deve);
1786 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1787 core_scsi3_tpg_undepend_item(dest_tpg);
1788 kfree(tidh_new);
1789 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1790 ret = -EINVAL;
1791 goto out;
1793 tidh_new->dest_pr_reg = dest_pr_reg;
1794 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1796 ptr += tid_len;
1797 tpdl -= tid_len;
1798 tid_len = 0;
1802 transport_kunmap_data_sg(cmd);
1805 * Go ahead and create a registrations from tid_dest_list for the
1806 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1807 * and dest_se_deve.
1809 * The SA Reservation Key from the PROUT is set for the
1810 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1811 * means that the TransportID Initiator port will be
1812 * registered on all of the target ports in the SCSI target device
1813 * ALL_TG_PT=0 means the registration will only be for the
1814 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1815 * was received.
1817 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1818 dest_tpg = tidh->dest_tpg;
1819 dest_node_acl = tidh->dest_node_acl;
1820 dest_se_deve = tidh->dest_se_deve;
1821 dest_pr_reg = tidh->dest_pr_reg;
1822 dest_local_nexus = tidh->dest_local_nexus;
1824 list_del(&tidh->dest_list);
1825 kfree(tidh);
1827 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1828 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1829 PR_REG_ISID_ID_LEN);
1831 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1832 dest_pr_reg, 0, 0);
1834 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1835 " registered Transport ID for Node: %s%s Mapped LUN:"
1836 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1837 dest_node_acl->initiatorname, (prf_isid) ?
1838 &i_buf[0] : "", dest_se_deve->mapped_lun);
1840 if (dest_local_nexus)
1841 continue;
1843 core_scsi3_lunacl_undepend_item(dest_se_deve);
1844 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1845 core_scsi3_tpg_undepend_item(dest_tpg);
1848 return 0;
1849 out:
1850 transport_kunmap_data_sg(cmd);
1852 * For the failure case, release everything from tid_dest_list
1853 * including *dest_pr_reg and the configfs dependances..
1855 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1856 dest_tpg = tidh->dest_tpg;
1857 dest_node_acl = tidh->dest_node_acl;
1858 dest_se_deve = tidh->dest_se_deve;
1859 dest_pr_reg = tidh->dest_pr_reg;
1860 dest_local_nexus = tidh->dest_local_nexus;
1862 list_del(&tidh->dest_list);
1863 kfree(tidh);
1865 * Release any extra ALL_TG_PT=1 registrations for
1866 * the SPEC_I_PT=1 case.
1868 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1869 &dest_pr_reg->pr_reg_atp_list,
1870 pr_reg_atp_mem_list) {
1871 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1872 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1873 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1876 kfree(dest_pr_reg->pr_aptpl_buf);
1877 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1879 if (dest_local_nexus)
1880 continue;
1882 core_scsi3_lunacl_undepend_item(dest_se_deve);
1883 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1884 core_scsi3_tpg_undepend_item(dest_tpg);
1886 return ret;
1890 * Called with struct se_device->dev_reservation_lock held
1892 static int __core_scsi3_update_aptpl_buf(
1893 struct se_device *dev,
1894 unsigned char *buf,
1895 u32 pr_aptpl_buf_len,
1896 int clear_aptpl_metadata)
1898 struct se_lun *lun;
1899 struct se_portal_group *tpg;
1900 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1901 struct t10_pr_registration *pr_reg;
1902 unsigned char tmp[512], isid_buf[32];
1903 ssize_t len = 0;
1904 int reg_count = 0;
1906 memset(buf, 0, pr_aptpl_buf_len);
1908 * Called to clear metadata once APTPL has been deactivated.
1910 if (clear_aptpl_metadata) {
1911 snprintf(buf, pr_aptpl_buf_len,
1912 "No Registrations or Reservations\n");
1913 return 0;
1916 * Walk the registration list..
1918 spin_lock(&su_dev->t10_pr.registration_lock);
1919 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1920 pr_reg_list) {
1922 tmp[0] = '\0';
1923 isid_buf[0] = '\0';
1924 tpg = pr_reg->pr_reg_nacl->se_tpg;
1925 lun = pr_reg->pr_reg_tg_pt_lun;
1927 * Write out any ISID value to APTPL metadata that was included
1928 * in the original registration.
1930 if (pr_reg->isid_present_at_reg)
1931 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1932 pr_reg->pr_reg_isid);
1934 * Include special metadata if the pr_reg matches the
1935 * reservation holder.
1937 if (dev->dev_pr_res_holder == pr_reg) {
1938 snprintf(tmp, 512, "PR_REG_START: %d"
1939 "\ninitiator_fabric=%s\n"
1940 "initiator_node=%s\n%s"
1941 "sa_res_key=%llu\n"
1942 "res_holder=1\nres_type=%02x\n"
1943 "res_scope=%02x\nres_all_tg_pt=%d\n"
1944 "mapped_lun=%u\n", reg_count,
1945 tpg->se_tpg_tfo->get_fabric_name(),
1946 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1947 pr_reg->pr_res_key, pr_reg->pr_res_type,
1948 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1949 pr_reg->pr_res_mapped_lun);
1950 } else {
1951 snprintf(tmp, 512, "PR_REG_START: %d\n"
1952 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1953 "sa_res_key=%llu\nres_holder=0\n"
1954 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1955 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1956 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1957 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1958 pr_reg->pr_res_mapped_lun);
1961 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1962 pr_err("Unable to update renaming"
1963 " APTPL metadata\n");
1964 spin_unlock(&su_dev->t10_pr.registration_lock);
1965 return -EMSGSIZE;
1967 len += sprintf(buf+len, "%s", tmp);
1970 * Include information about the associated SCSI target port.
1972 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1973 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1974 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1975 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1976 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1977 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1979 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1980 pr_err("Unable to update renaming"
1981 " APTPL metadata\n");
1982 spin_unlock(&su_dev->t10_pr.registration_lock);
1983 return -EMSGSIZE;
1985 len += sprintf(buf+len, "%s", tmp);
1986 reg_count++;
1988 spin_unlock(&su_dev->t10_pr.registration_lock);
1990 if (!reg_count)
1991 len += sprintf(buf+len, "No Registrations or Reservations");
1993 return 0;
1996 static int core_scsi3_update_aptpl_buf(
1997 struct se_device *dev,
1998 unsigned char *buf,
1999 u32 pr_aptpl_buf_len,
2000 int clear_aptpl_metadata)
2002 int ret;
2004 spin_lock(&dev->dev_reservation_lock);
2005 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2006 clear_aptpl_metadata);
2007 spin_unlock(&dev->dev_reservation_lock);
2009 return ret;
2013 * Called with struct se_device->aptpl_file_mutex held
2015 static int __core_scsi3_write_aptpl_to_file(
2016 struct se_device *dev,
2017 unsigned char *buf,
2018 u32 pr_aptpl_buf_len)
2020 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
2021 struct file *file;
2022 struct iovec iov[1];
2023 mm_segment_t old_fs;
2024 int flags = O_RDWR | O_CREAT | O_TRUNC;
2025 char path[512];
2026 int ret;
2028 memset(iov, 0, sizeof(struct iovec));
2029 memset(path, 0, 512);
2031 if (strlen(&wwn->unit_serial[0]) >= 512) {
2032 pr_err("WWN value for struct se_device does not fit"
2033 " into path buffer\n");
2034 return -EMSGSIZE;
2037 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2038 file = filp_open(path, flags, 0600);
2039 if (IS_ERR(file) || !file || !file->f_dentry) {
2040 pr_err("filp_open(%s) for APTPL metadata"
2041 " failed\n", path);
2042 return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
2045 iov[0].iov_base = &buf[0];
2046 if (!pr_aptpl_buf_len)
2047 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2048 else
2049 iov[0].iov_len = pr_aptpl_buf_len;
2051 old_fs = get_fs();
2052 set_fs(get_ds());
2053 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2054 set_fs(old_fs);
2056 if (ret < 0) {
2057 pr_debug("Error writing APTPL metadata file: %s\n", path);
2058 filp_close(file, NULL);
2059 return -EIO;
2061 filp_close(file, NULL);
2063 return 0;
2066 static int core_scsi3_update_and_write_aptpl(
2067 struct se_device *dev,
2068 unsigned char *in_buf,
2069 u32 in_pr_aptpl_buf_len)
2071 unsigned char null_buf[64], *buf;
2072 u32 pr_aptpl_buf_len;
2073 int ret, clear_aptpl_metadata = 0;
2075 * Can be called with a NULL pointer from PROUT service action CLEAR
2077 if (!in_buf) {
2078 memset(null_buf, 0, 64);
2079 buf = &null_buf[0];
2081 * This will clear the APTPL metadata to:
2082 * "No Registrations or Reservations" status
2084 pr_aptpl_buf_len = 64;
2085 clear_aptpl_metadata = 1;
2086 } else {
2087 buf = in_buf;
2088 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2091 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2092 clear_aptpl_metadata);
2093 if (ret != 0)
2094 return ret;
2096 * __core_scsi3_write_aptpl_to_file() will call strlen()
2097 * on the passed buf to determine pr_aptpl_buf_len.
2099 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2100 if (ret != 0)
2101 return ret;
2103 return ret;
2106 static int core_scsi3_emulate_pro_register(
2107 struct se_cmd *cmd,
2108 u64 res_key,
2109 u64 sa_res_key,
2110 int aptpl,
2111 int all_tg_pt,
2112 int spec_i_pt,
2113 int ignore_key)
2115 struct se_session *se_sess = cmd->se_sess;
2116 struct se_device *dev = cmd->se_dev;
2117 struct se_dev_entry *se_deve;
2118 struct se_lun *se_lun = cmd->se_lun;
2119 struct se_portal_group *se_tpg;
2120 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2121 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2122 /* Used for APTPL metadata w/ UNREGISTER */
2123 unsigned char *pr_aptpl_buf = NULL;
2124 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2125 int pr_holder = 0, ret = 0, type;
2127 if (!se_sess || !se_lun) {
2128 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2129 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2130 return -EINVAL;
2132 se_tpg = se_sess->se_tpg;
2133 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2135 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2136 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2137 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2138 PR_REG_ISID_LEN);
2139 isid_ptr = &isid_buf[0];
2142 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2144 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2145 if (!pr_reg_e) {
2146 if (res_key) {
2147 pr_warn("SPC-3 PR: Reservation Key non-zero"
2148 " for SA REGISTER, returning CONFLICT\n");
2149 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2150 return -EINVAL;
2153 * Do nothing but return GOOD status.
2155 if (!sa_res_key)
2156 return 0;
2158 if (!spec_i_pt) {
2160 * Perform the Service Action REGISTER on the Initiator
2161 * Port Endpoint that the PRO was received from on the
2162 * Logical Unit of the SCSI device server.
2164 ret = core_scsi3_alloc_registration(cmd->se_dev,
2165 se_sess->se_node_acl, se_deve, isid_ptr,
2166 sa_res_key, all_tg_pt, aptpl,
2167 ignore_key, 0);
2168 if (ret != 0) {
2169 pr_err("Unable to allocate"
2170 " struct t10_pr_registration\n");
2171 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2172 return -EINVAL;
2174 } else {
2176 * Register both the Initiator port that received
2177 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2178 * TransportID from Parameter list and loop through
2179 * fabric dependent parameter list while calling
2180 * logic from of core_scsi3_alloc_registration() for
2181 * each TransportID provided SCSI Initiator Port/Device
2183 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2184 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2185 if (ret != 0)
2186 return ret;
2189 * Nothing left to do for the APTPL=0 case.
2191 if (!aptpl) {
2192 pr_tmpl->pr_aptpl_active = 0;
2193 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2194 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2195 " REGISTER\n");
2196 return 0;
2199 * Locate the newly allocated local I_T Nexus *pr_reg, and
2200 * update the APTPL metadata information using its
2201 * preallocated *pr_reg->pr_aptpl_buf.
2203 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2204 se_sess->se_node_acl, se_sess);
2206 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2207 &pr_reg->pr_aptpl_buf[0],
2208 pr_tmpl->pr_aptpl_buf_len);
2209 if (!ret) {
2210 pr_tmpl->pr_aptpl_active = 1;
2211 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2214 core_scsi3_put_pr_reg(pr_reg);
2215 return ret;
2216 } else {
2218 * Locate the existing *pr_reg via struct se_node_acl pointers
2220 pr_reg = pr_reg_e;
2221 type = pr_reg->pr_res_type;
2223 if (!ignore_key) {
2224 if (res_key != pr_reg->pr_res_key) {
2225 pr_err("SPC-3 PR REGISTER: Received"
2226 " res_key: 0x%016Lx does not match"
2227 " existing SA REGISTER res_key:"
2228 " 0x%016Lx\n", res_key,
2229 pr_reg->pr_res_key);
2230 core_scsi3_put_pr_reg(pr_reg);
2231 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2232 return -EINVAL;
2235 if (spec_i_pt) {
2236 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2237 " set while sa_res_key=0\n");
2238 core_scsi3_put_pr_reg(pr_reg);
2239 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2240 return -EINVAL;
2243 * An existing ALL_TG_PT=1 registration being released
2244 * must also set ALL_TG_PT=1 in the incoming PROUT.
2246 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2247 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2248 " registration exists, but ALL_TG_PT=1 bit not"
2249 " present in received PROUT\n");
2250 core_scsi3_put_pr_reg(pr_reg);
2251 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2252 return -EINVAL;
2255 * Allocate APTPL metadata buffer used for UNREGISTER ops
2257 if (aptpl) {
2258 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2259 GFP_KERNEL);
2260 if (!pr_aptpl_buf) {
2261 pr_err("Unable to allocate"
2262 " pr_aptpl_buf\n");
2263 core_scsi3_put_pr_reg(pr_reg);
2264 cmd->scsi_sense_reason =
2265 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2266 return -EINVAL;
2270 * sa_res_key=0 Unregister Reservation Key for registered I_T
2271 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2272 * Nexus.
2274 if (!sa_res_key) {
2275 pr_holder = core_scsi3_check_implict_release(
2276 cmd->se_dev, pr_reg);
2277 if (pr_holder < 0) {
2278 kfree(pr_aptpl_buf);
2279 core_scsi3_put_pr_reg(pr_reg);
2280 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2281 return -EINVAL;
2284 spin_lock(&pr_tmpl->registration_lock);
2286 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2287 * and matching pr_res_key.
2289 if (pr_reg->pr_reg_all_tg_pt) {
2290 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2291 &pr_tmpl->registration_list,
2292 pr_reg_list) {
2294 if (!pr_reg_p->pr_reg_all_tg_pt)
2295 continue;
2297 if (pr_reg_p->pr_res_key != res_key)
2298 continue;
2300 if (pr_reg == pr_reg_p)
2301 continue;
2303 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2304 pr_reg_p->pr_reg_nacl->initiatorname))
2305 continue;
2307 __core_scsi3_free_registration(dev,
2308 pr_reg_p, NULL, 0);
2312 * Release the calling I_T Nexus registration now..
2314 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
2315 NULL, 1);
2317 * From spc4r17, section 5.7.11.3 Unregistering
2319 * If the persistent reservation is a registrants only
2320 * type, the device server shall establish a unit
2321 * attention condition for the initiator port associated
2322 * with every registered I_T nexus except for the I_T
2323 * nexus on which the PERSISTENT RESERVE OUT command was
2324 * received, with the additional sense code set to
2325 * RESERVATIONS RELEASED.
2327 if (pr_holder &&
2328 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2329 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2330 list_for_each_entry(pr_reg_p,
2331 &pr_tmpl->registration_list,
2332 pr_reg_list) {
2334 core_scsi3_ua_allocate(
2335 pr_reg_p->pr_reg_nacl,
2336 pr_reg_p->pr_res_mapped_lun,
2337 0x2A,
2338 ASCQ_2AH_RESERVATIONS_RELEASED);
2341 spin_unlock(&pr_tmpl->registration_lock);
2343 if (!aptpl) {
2344 pr_tmpl->pr_aptpl_active = 0;
2345 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2346 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2347 " for UNREGISTER\n");
2348 return 0;
2351 ret = core_scsi3_update_and_write_aptpl(dev,
2352 &pr_aptpl_buf[0],
2353 pr_tmpl->pr_aptpl_buf_len);
2354 if (!ret) {
2355 pr_tmpl->pr_aptpl_active = 1;
2356 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2357 " for UNREGISTER\n");
2360 kfree(pr_aptpl_buf);
2361 return ret;
2362 } else {
2364 * Increment PRgeneration counter for struct se_device"
2365 * upon a successful REGISTER, see spc4r17 section 6.3.2
2366 * READ_KEYS service action.
2368 pr_reg->pr_res_generation = core_scsi3_pr_generation(
2369 cmd->se_dev);
2370 pr_reg->pr_res_key = sa_res_key;
2371 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2372 " Key for %s to: 0x%016Lx PRgeneration:"
2373 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2374 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2375 pr_reg->pr_reg_nacl->initiatorname,
2376 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2378 if (!aptpl) {
2379 pr_tmpl->pr_aptpl_active = 0;
2380 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2381 core_scsi3_put_pr_reg(pr_reg);
2382 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2383 " for REGISTER\n");
2384 return 0;
2387 ret = core_scsi3_update_and_write_aptpl(dev,
2388 &pr_aptpl_buf[0],
2389 pr_tmpl->pr_aptpl_buf_len);
2390 if (!ret) {
2391 pr_tmpl->pr_aptpl_active = 1;
2392 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2393 " for REGISTER\n");
2396 kfree(pr_aptpl_buf);
2397 core_scsi3_put_pr_reg(pr_reg);
2400 return 0;
2403 unsigned char *core_scsi3_pr_dump_type(int type)
2405 switch (type) {
2406 case PR_TYPE_WRITE_EXCLUSIVE:
2407 return "Write Exclusive Access";
2408 case PR_TYPE_EXCLUSIVE_ACCESS:
2409 return "Exclusive Access";
2410 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2411 return "Write Exclusive Access, Registrants Only";
2412 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2413 return "Exclusive Access, Registrants Only";
2414 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2415 return "Write Exclusive Access, All Registrants";
2416 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2417 return "Exclusive Access, All Registrants";
2418 default:
2419 break;
2422 return "Unknown SPC-3 PR Type";
2425 static int core_scsi3_pro_reserve(
2426 struct se_cmd *cmd,
2427 struct se_device *dev,
2428 int type,
2429 int scope,
2430 u64 res_key)
2432 struct se_session *se_sess = cmd->se_sess;
2433 struct se_dev_entry *se_deve;
2434 struct se_lun *se_lun = cmd->se_lun;
2435 struct se_portal_group *se_tpg;
2436 struct t10_pr_registration *pr_reg, *pr_res_holder;
2437 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2438 char i_buf[PR_REG_ISID_ID_LEN];
2439 int ret, prf_isid;
2441 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2443 if (!se_sess || !se_lun) {
2444 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2445 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2446 return -EINVAL;
2448 se_tpg = se_sess->se_tpg;
2449 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2451 * Locate the existing *pr_reg via struct se_node_acl pointers
2453 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2454 se_sess);
2455 if (!pr_reg) {
2456 pr_err("SPC-3 PR: Unable to locate"
2457 " PR_REGISTERED *pr_reg for RESERVE\n");
2458 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2459 return -EINVAL;
2462 * From spc4r17 Section 5.7.9: Reserving:
2464 * An application client creates a persistent reservation by issuing
2465 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2466 * a registered I_T nexus with the following parameters:
2467 * a) RESERVATION KEY set to the value of the reservation key that is
2468 * registered with the logical unit for the I_T nexus; and
2470 if (res_key != pr_reg->pr_res_key) {
2471 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2472 " does not match existing SA REGISTER res_key:"
2473 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2474 core_scsi3_put_pr_reg(pr_reg);
2475 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2476 return -EINVAL;
2479 * From spc4r17 Section 5.7.9: Reserving:
2481 * From above:
2482 * b) TYPE field and SCOPE field set to the persistent reservation
2483 * being created.
2485 * Only one persistent reservation is allowed at a time per logical unit
2486 * and that persistent reservation has a scope of LU_SCOPE.
2488 if (scope != PR_SCOPE_LU_SCOPE) {
2489 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2490 core_scsi3_put_pr_reg(pr_reg);
2491 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2492 return -EINVAL;
2495 * See if we have an existing PR reservation holder pointer at
2496 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2497 * *pr_res_holder.
2499 spin_lock(&dev->dev_reservation_lock);
2500 pr_res_holder = dev->dev_pr_res_holder;
2501 if ((pr_res_holder)) {
2503 * From spc4r17 Section 5.7.9: Reserving:
2505 * If the device server receives a PERSISTENT RESERVE OUT
2506 * command from an I_T nexus other than a persistent reservation
2507 * holder (see 5.7.10) that attempts to create a persistent
2508 * reservation when a persistent reservation already exists for
2509 * the logical unit, then the command shall be completed with
2510 * RESERVATION CONFLICT status.
2512 if (pr_res_holder != pr_reg) {
2513 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2514 pr_err("SPC-3 PR: Attempted RESERVE from"
2515 " [%s]: %s while reservation already held by"
2516 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2517 cmd->se_tfo->get_fabric_name(),
2518 se_sess->se_node_acl->initiatorname,
2519 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2520 pr_res_holder->pr_reg_nacl->initiatorname);
2522 spin_unlock(&dev->dev_reservation_lock);
2523 core_scsi3_put_pr_reg(pr_reg);
2524 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2525 return -EINVAL;
2528 * From spc4r17 Section 5.7.9: Reserving:
2530 * If a persistent reservation holder attempts to modify the
2531 * type or scope of an existing persistent reservation, the
2532 * command shall be completed with RESERVATION CONFLICT status.
2534 if ((pr_res_holder->pr_res_type != type) ||
2535 (pr_res_holder->pr_res_scope != scope)) {
2536 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2537 pr_err("SPC-3 PR: Attempted RESERVE from"
2538 " [%s]: %s trying to change TYPE and/or SCOPE,"
2539 " while reservation already held by [%s]: %s,"
2540 " returning RESERVATION_CONFLICT\n",
2541 cmd->se_tfo->get_fabric_name(),
2542 se_sess->se_node_acl->initiatorname,
2543 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2544 pr_res_holder->pr_reg_nacl->initiatorname);
2546 spin_unlock(&dev->dev_reservation_lock);
2547 core_scsi3_put_pr_reg(pr_reg);
2548 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2549 return -EINVAL;
2552 * From spc4r17 Section 5.7.9: Reserving:
2554 * If the device server receives a PERSISTENT RESERVE OUT
2555 * command with RESERVE service action where the TYPE field and
2556 * the SCOPE field contain the same values as the existing type
2557 * and scope from a persistent reservation holder, it shall not
2558 * make any change to the existing persistent reservation and
2559 * shall completethe command with GOOD status.
2561 spin_unlock(&dev->dev_reservation_lock);
2562 core_scsi3_put_pr_reg(pr_reg);
2563 return 0;
2566 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2567 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2569 pr_reg->pr_res_scope = scope;
2570 pr_reg->pr_res_type = type;
2571 pr_reg->pr_res_holder = 1;
2572 dev->dev_pr_res_holder = pr_reg;
2573 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2574 PR_REG_ISID_ID_LEN);
2576 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2577 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2578 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2579 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2580 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2581 cmd->se_tfo->get_fabric_name(),
2582 se_sess->se_node_acl->initiatorname,
2583 (prf_isid) ? &i_buf[0] : "");
2584 spin_unlock(&dev->dev_reservation_lock);
2586 if (pr_tmpl->pr_aptpl_active) {
2587 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2588 &pr_reg->pr_aptpl_buf[0],
2589 pr_tmpl->pr_aptpl_buf_len);
2590 if (!ret)
2591 pr_debug("SPC-3 PR: Updated APTPL metadata"
2592 " for RESERVE\n");
2595 core_scsi3_put_pr_reg(pr_reg);
2596 return 0;
2599 static int core_scsi3_emulate_pro_reserve(
2600 struct se_cmd *cmd,
2601 int type,
2602 int scope,
2603 u64 res_key)
2605 struct se_device *dev = cmd->se_dev;
2606 int ret = 0;
2608 switch (type) {
2609 case PR_TYPE_WRITE_EXCLUSIVE:
2610 case PR_TYPE_EXCLUSIVE_ACCESS:
2611 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2612 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2613 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2614 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2615 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2616 break;
2617 default:
2618 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2619 " 0x%02x\n", type);
2620 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2621 return -EINVAL;
2624 return ret;
2628 * Called with struct se_device->dev_reservation_lock held.
2630 static void __core_scsi3_complete_pro_release(
2631 struct se_device *dev,
2632 struct se_node_acl *se_nacl,
2633 struct t10_pr_registration *pr_reg,
2634 int explict)
2636 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2637 char i_buf[PR_REG_ISID_ID_LEN];
2638 int prf_isid;
2640 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2641 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2642 PR_REG_ISID_ID_LEN);
2644 * Go ahead and release the current PR reservation holder.
2646 dev->dev_pr_res_holder = NULL;
2648 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2649 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2650 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2651 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2652 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2653 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2654 tfo->get_fabric_name(), se_nacl->initiatorname,
2655 (prf_isid) ? &i_buf[0] : "");
2657 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2659 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2662 static int core_scsi3_emulate_pro_release(
2663 struct se_cmd *cmd,
2664 int type,
2665 int scope,
2666 u64 res_key)
2668 struct se_device *dev = cmd->se_dev;
2669 struct se_session *se_sess = cmd->se_sess;
2670 struct se_lun *se_lun = cmd->se_lun;
2671 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2672 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2673 int ret, all_reg = 0;
2675 if (!se_sess || !se_lun) {
2676 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2677 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2678 return -EINVAL;
2681 * Locate the existing *pr_reg via struct se_node_acl pointers
2683 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2684 if (!pr_reg) {
2685 pr_err("SPC-3 PR: Unable to locate"
2686 " PR_REGISTERED *pr_reg for RELEASE\n");
2687 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2688 return -EINVAL;
2691 * From spc4r17 Section 5.7.11.2 Releasing:
2693 * If there is no persistent reservation or in response to a persistent
2694 * reservation release request from a registered I_T nexus that is not a
2695 * persistent reservation holder (see 5.7.10), the device server shall
2696 * do the following:
2698 * a) Not release the persistent reservation, if any;
2699 * b) Not remove any registrations; and
2700 * c) Complete the command with GOOD status.
2702 spin_lock(&dev->dev_reservation_lock);
2703 pr_res_holder = dev->dev_pr_res_holder;
2704 if (!pr_res_holder) {
2706 * No persistent reservation, return GOOD status.
2708 spin_unlock(&dev->dev_reservation_lock);
2709 core_scsi3_put_pr_reg(pr_reg);
2710 return 0;
2712 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2713 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2714 all_reg = 1;
2716 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2718 * Non 'All Registrants' PR Type cases..
2719 * Release request from a registered I_T nexus that is not a
2720 * persistent reservation holder. return GOOD status.
2722 spin_unlock(&dev->dev_reservation_lock);
2723 core_scsi3_put_pr_reg(pr_reg);
2724 return 0;
2727 * From spc4r17 Section 5.7.11.2 Releasing:
2729 * Only the persistent reservation holder (see 5.7.10) is allowed to
2730 * release a persistent reservation.
2732 * An application client releases the persistent reservation by issuing
2733 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2734 * an I_T nexus that is a persistent reservation holder with the
2735 * following parameters:
2737 * a) RESERVATION KEY field set to the value of the reservation key
2738 * that is registered with the logical unit for the I_T nexus;
2740 if (res_key != pr_reg->pr_res_key) {
2741 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2742 " does not match existing SA REGISTER res_key:"
2743 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2744 spin_unlock(&dev->dev_reservation_lock);
2745 core_scsi3_put_pr_reg(pr_reg);
2746 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2747 return -EINVAL;
2750 * From spc4r17 Section 5.7.11.2 Releasing and above:
2752 * b) TYPE field and SCOPE field set to match the persistent
2753 * reservation being released.
2755 if ((pr_res_holder->pr_res_type != type) ||
2756 (pr_res_holder->pr_res_scope != scope)) {
2757 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2758 pr_err("SPC-3 PR RELEASE: Attempted to release"
2759 " reservation from [%s]: %s with different TYPE "
2760 "and/or SCOPE while reservation already held by"
2761 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2762 cmd->se_tfo->get_fabric_name(),
2763 se_sess->se_node_acl->initiatorname,
2764 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2765 pr_res_holder->pr_reg_nacl->initiatorname);
2767 spin_unlock(&dev->dev_reservation_lock);
2768 core_scsi3_put_pr_reg(pr_reg);
2769 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2770 return -EINVAL;
2773 * In response to a persistent reservation release request from the
2774 * persistent reservation holder the device server shall perform a
2775 * release by doing the following as an uninterrupted series of actions:
2776 * a) Release the persistent reservation;
2777 * b) Not remove any registration(s);
2778 * c) If the released persistent reservation is a registrants only type
2779 * or all registrants type persistent reservation,
2780 * the device server shall establish a unit attention condition for
2781 * the initiator port associated with every regis-
2782 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2783 * RESERVE OUT command with RELEASE service action was received,
2784 * with the additional sense code set to RESERVATIONS RELEASED; and
2785 * d) If the persistent reservation is of any other type, the device
2786 * server shall not establish a unit attention condition.
2788 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2789 pr_reg, 1);
2791 spin_unlock(&dev->dev_reservation_lock);
2793 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2794 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2795 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2796 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2798 * If no UNIT ATTENTION conditions will be established for
2799 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2800 * go ahead and check for APTPL=1 update+write below
2802 goto write_aptpl;
2805 spin_lock(&pr_tmpl->registration_lock);
2806 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2807 pr_reg_list) {
2809 * Do not establish a UNIT ATTENTION condition
2810 * for the calling I_T Nexus
2812 if (pr_reg_p == pr_reg)
2813 continue;
2815 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2816 pr_reg_p->pr_res_mapped_lun,
2817 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2819 spin_unlock(&pr_tmpl->registration_lock);
2821 write_aptpl:
2822 if (pr_tmpl->pr_aptpl_active) {
2823 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2824 &pr_reg->pr_aptpl_buf[0],
2825 pr_tmpl->pr_aptpl_buf_len);
2826 if (!ret)
2827 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2830 core_scsi3_put_pr_reg(pr_reg);
2831 return 0;
2834 static int core_scsi3_emulate_pro_clear(
2835 struct se_cmd *cmd,
2836 u64 res_key)
2838 struct se_device *dev = cmd->se_dev;
2839 struct se_node_acl *pr_reg_nacl;
2840 struct se_session *se_sess = cmd->se_sess;
2841 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
2842 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2843 u32 pr_res_mapped_lun = 0;
2844 int calling_it_nexus = 0;
2846 * Locate the existing *pr_reg via struct se_node_acl pointers
2848 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2849 se_sess->se_node_acl, se_sess);
2850 if (!pr_reg_n) {
2851 pr_err("SPC-3 PR: Unable to locate"
2852 " PR_REGISTERED *pr_reg for CLEAR\n");
2853 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2854 return -EINVAL;
2857 * From spc4r17 section 5.7.11.6, Clearing:
2859 * Any application client may release the persistent reservation and
2860 * remove all registrations from a device server by issuing a
2861 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2862 * registered I_T nexus with the following parameter:
2864 * a) RESERVATION KEY field set to the value of the reservation key
2865 * that is registered with the logical unit for the I_T nexus.
2867 if (res_key != pr_reg_n->pr_res_key) {
2868 pr_err("SPC-3 PR REGISTER: Received"
2869 " res_key: 0x%016Lx does not match"
2870 " existing SA REGISTER res_key:"
2871 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2872 core_scsi3_put_pr_reg(pr_reg_n);
2873 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2874 return -EINVAL;
2877 * a) Release the persistent reservation, if any;
2879 spin_lock(&dev->dev_reservation_lock);
2880 pr_res_holder = dev->dev_pr_res_holder;
2881 if (pr_res_holder) {
2882 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2883 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2884 pr_res_holder, 0);
2886 spin_unlock(&dev->dev_reservation_lock);
2888 * b) Remove all registration(s) (see spc4r17 5.7.7);
2890 spin_lock(&pr_tmpl->registration_lock);
2891 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2892 &pr_tmpl->registration_list, pr_reg_list) {
2894 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2895 pr_reg_nacl = pr_reg->pr_reg_nacl;
2896 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2897 __core_scsi3_free_registration(dev, pr_reg, NULL,
2898 calling_it_nexus);
2900 * e) Establish a unit attention condition for the initiator
2901 * port associated with every registered I_T nexus other
2902 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2903 * command with CLEAR service action was received, with the
2904 * additional sense code set to RESERVATIONS PREEMPTED.
2906 if (!calling_it_nexus)
2907 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2908 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2910 spin_unlock(&pr_tmpl->registration_lock);
2912 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2913 cmd->se_tfo->get_fabric_name());
2915 if (pr_tmpl->pr_aptpl_active) {
2916 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2917 pr_debug("SPC-3 PR: Updated APTPL metadata"
2918 " for CLEAR\n");
2921 core_scsi3_pr_generation(dev);
2922 return 0;
2926 * Called with struct se_device->dev_reservation_lock held.
2928 static void __core_scsi3_complete_pro_preempt(
2929 struct se_device *dev,
2930 struct t10_pr_registration *pr_reg,
2931 struct list_head *preempt_and_abort_list,
2932 int type,
2933 int scope,
2934 int abort)
2936 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2937 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2938 char i_buf[PR_REG_ISID_ID_LEN];
2939 int prf_isid;
2941 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2942 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2943 PR_REG_ISID_ID_LEN);
2945 * Do an implict RELEASE of the existing reservation.
2947 if (dev->dev_pr_res_holder)
2948 __core_scsi3_complete_pro_release(dev, nacl,
2949 dev->dev_pr_res_holder, 0);
2951 dev->dev_pr_res_holder = pr_reg;
2952 pr_reg->pr_res_holder = 1;
2953 pr_reg->pr_res_type = type;
2954 pr_reg->pr_res_scope = scope;
2956 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2957 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2958 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2959 core_scsi3_pr_dump_type(type),
2960 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2961 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2962 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2963 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2965 * For PREEMPT_AND_ABORT, add the preempting reservation's
2966 * struct t10_pr_registration to the list that will be compared
2967 * against received CDBs..
2969 if (preempt_and_abort_list)
2970 list_add_tail(&pr_reg->pr_reg_abort_list,
2971 preempt_and_abort_list);
2974 static void core_scsi3_release_preempt_and_abort(
2975 struct list_head *preempt_and_abort_list,
2976 struct t10_pr_registration *pr_reg_holder)
2978 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2980 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2981 pr_reg_abort_list) {
2983 list_del(&pr_reg->pr_reg_abort_list);
2984 if (pr_reg_holder == pr_reg)
2985 continue;
2986 if (pr_reg->pr_res_holder) {
2987 pr_warn("pr_reg->pr_res_holder still set\n");
2988 continue;
2991 pr_reg->pr_reg_deve = NULL;
2992 pr_reg->pr_reg_nacl = NULL;
2993 kfree(pr_reg->pr_aptpl_buf);
2994 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2998 static int core_scsi3_pro_preempt(
2999 struct se_cmd *cmd,
3000 int type,
3001 int scope,
3002 u64 res_key,
3003 u64 sa_res_key,
3004 int abort)
3006 struct se_device *dev = cmd->se_dev;
3007 struct se_dev_entry *se_deve;
3008 struct se_node_acl *pr_reg_nacl;
3009 struct se_session *se_sess = cmd->se_sess;
3010 struct list_head preempt_and_abort_list;
3011 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
3012 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3013 u32 pr_res_mapped_lun = 0;
3014 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3015 int prh_type = 0, prh_scope = 0, ret;
3017 if (!se_sess) {
3018 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3019 return -EINVAL;
3022 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3023 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3024 se_sess);
3025 if (!pr_reg_n) {
3026 pr_err("SPC-3 PR: Unable to locate"
3027 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3028 (abort) ? "_AND_ABORT" : "");
3029 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3030 return -EINVAL;
3032 if (pr_reg_n->pr_res_key != res_key) {
3033 core_scsi3_put_pr_reg(pr_reg_n);
3034 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3035 return -EINVAL;
3037 if (scope != PR_SCOPE_LU_SCOPE) {
3038 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3039 core_scsi3_put_pr_reg(pr_reg_n);
3040 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3041 return -EINVAL;
3043 INIT_LIST_HEAD(&preempt_and_abort_list);
3045 spin_lock(&dev->dev_reservation_lock);
3046 pr_res_holder = dev->dev_pr_res_holder;
3047 if (pr_res_holder &&
3048 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3049 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3050 all_reg = 1;
3052 if (!all_reg && !sa_res_key) {
3053 spin_unlock(&dev->dev_reservation_lock);
3054 core_scsi3_put_pr_reg(pr_reg_n);
3055 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3056 return -EINVAL;
3059 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3061 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3062 * persistent reservation holder or there is no persistent reservation
3063 * holder (i.e., there is no persistent reservation), then the device
3064 * server shall perform a preempt by doing the following in an
3065 * uninterrupted series of actions. (See below..)
3067 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3069 * No existing or SA Reservation Key matching reservations..
3071 * PROUT SA PREEMPT with All Registrant type reservations are
3072 * allowed to be processed without a matching SA Reservation Key
3074 spin_lock(&pr_tmpl->registration_lock);
3075 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3076 &pr_tmpl->registration_list, pr_reg_list) {
3078 * Removing of registrations in non all registrants
3079 * type reservations without a matching SA reservation
3080 * key.
3082 * a) Remove the registrations for all I_T nexuses
3083 * specified by the SERVICE ACTION RESERVATION KEY
3084 * field;
3085 * b) Ignore the contents of the SCOPE and TYPE fields;
3086 * c) Process tasks as defined in 5.7.1; and
3087 * d) Establish a unit attention condition for the
3088 * initiator port associated with every I_T nexus
3089 * that lost its registration other than the I_T
3090 * nexus on which the PERSISTENT RESERVE OUT command
3091 * was received, with the additional sense code set
3092 * to REGISTRATIONS PREEMPTED.
3094 if (!all_reg) {
3095 if (pr_reg->pr_res_key != sa_res_key)
3096 continue;
3098 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3099 pr_reg_nacl = pr_reg->pr_reg_nacl;
3100 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3101 __core_scsi3_free_registration(dev, pr_reg,
3102 (abort) ? &preempt_and_abort_list :
3103 NULL, calling_it_nexus);
3104 released_regs++;
3105 } else {
3107 * Case for any existing all registrants type
3108 * reservation, follow logic in spc4r17 section
3109 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3111 * For a ZERO SA Reservation key, release
3112 * all other registrations and do an implict
3113 * release of active persistent reservation.
3115 * For a non-ZERO SA Reservation key, only
3116 * release the matching reservation key from
3117 * registrations.
3119 if ((sa_res_key) &&
3120 (pr_reg->pr_res_key != sa_res_key))
3121 continue;
3123 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3124 if (calling_it_nexus)
3125 continue;
3127 pr_reg_nacl = pr_reg->pr_reg_nacl;
3128 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3129 __core_scsi3_free_registration(dev, pr_reg,
3130 (abort) ? &preempt_and_abort_list :
3131 NULL, 0);
3132 released_regs++;
3134 if (!calling_it_nexus)
3135 core_scsi3_ua_allocate(pr_reg_nacl,
3136 pr_res_mapped_lun, 0x2A,
3137 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3139 spin_unlock(&pr_tmpl->registration_lock);
3141 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3142 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3143 * RESERVATION KEY field to a value that does not match any
3144 * registered reservation key, then the device server shall
3145 * complete the command with RESERVATION CONFLICT status.
3147 if (!released_regs) {
3148 spin_unlock(&dev->dev_reservation_lock);
3149 core_scsi3_put_pr_reg(pr_reg_n);
3150 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3151 return -EINVAL;
3154 * For an existing all registrants type reservation
3155 * with a zero SA rservation key, preempt the existing
3156 * reservation with the new PR type and scope.
3158 if (pr_res_holder && all_reg && !(sa_res_key)) {
3159 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3160 (abort) ? &preempt_and_abort_list : NULL,
3161 type, scope, abort);
3163 if (abort)
3164 core_scsi3_release_preempt_and_abort(
3165 &preempt_and_abort_list, pr_reg_n);
3167 spin_unlock(&dev->dev_reservation_lock);
3169 if (pr_tmpl->pr_aptpl_active) {
3170 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3171 &pr_reg_n->pr_aptpl_buf[0],
3172 pr_tmpl->pr_aptpl_buf_len);
3173 if (!ret)
3174 pr_debug("SPC-3 PR: Updated APTPL"
3175 " metadata for PREEMPT%s\n", (abort) ?
3176 "_AND_ABORT" : "");
3179 core_scsi3_put_pr_reg(pr_reg_n);
3180 core_scsi3_pr_generation(cmd->se_dev);
3181 return 0;
3184 * The PREEMPTing SA reservation key matches that of the
3185 * existing persistent reservation, first, we check if
3186 * we are preempting our own reservation.
3187 * From spc4r17, section 5.7.11.4.3 Preempting
3188 * persistent reservations and registration handling
3190 * If an all registrants persistent reservation is not
3191 * present, it is not an error for the persistent
3192 * reservation holder to preempt itself (i.e., a
3193 * PERSISTENT RESERVE OUT with a PREEMPT service action
3194 * or a PREEMPT AND ABORT service action with the
3195 * SERVICE ACTION RESERVATION KEY value equal to the
3196 * persistent reservation holder's reservation key that
3197 * is received from the persistent reservation holder).
3198 * In that case, the device server shall establish the
3199 * new persistent reservation and maintain the
3200 * registration.
3202 prh_type = pr_res_holder->pr_res_type;
3203 prh_scope = pr_res_holder->pr_res_scope;
3205 * If the SERVICE ACTION RESERVATION KEY field identifies a
3206 * persistent reservation holder (see 5.7.10), the device
3207 * server shall perform a preempt by doing the following as
3208 * an uninterrupted series of actions:
3210 * a) Release the persistent reservation for the holder
3211 * identified by the SERVICE ACTION RESERVATION KEY field;
3213 if (pr_reg_n != pr_res_holder)
3214 __core_scsi3_complete_pro_release(dev,
3215 pr_res_holder->pr_reg_nacl,
3216 dev->dev_pr_res_holder, 0);
3218 * b) Remove the registrations for all I_T nexuses identified
3219 * by the SERVICE ACTION RESERVATION KEY field, except the
3220 * I_T nexus that is being used for the PERSISTENT RESERVE
3221 * OUT command. If an all registrants persistent reservation
3222 * is present and the SERVICE ACTION RESERVATION KEY field
3223 * is set to zero, then all registrations shall be removed
3224 * except for that of the I_T nexus that is being used for
3225 * the PERSISTENT RESERVE OUT command;
3227 spin_lock(&pr_tmpl->registration_lock);
3228 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3229 &pr_tmpl->registration_list, pr_reg_list) {
3231 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3232 if (calling_it_nexus)
3233 continue;
3235 if (pr_reg->pr_res_key != sa_res_key)
3236 continue;
3238 pr_reg_nacl = pr_reg->pr_reg_nacl;
3239 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3240 __core_scsi3_free_registration(dev, pr_reg,
3241 (abort) ? &preempt_and_abort_list : NULL,
3242 calling_it_nexus);
3244 * e) Establish a unit attention condition for the initiator
3245 * port associated with every I_T nexus that lost its
3246 * persistent reservation and/or registration, with the
3247 * additional sense code set to REGISTRATIONS PREEMPTED;
3249 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3250 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3252 spin_unlock(&pr_tmpl->registration_lock);
3254 * c) Establish a persistent reservation for the preempting
3255 * I_T nexus using the contents of the SCOPE and TYPE fields;
3257 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3258 (abort) ? &preempt_and_abort_list : NULL,
3259 type, scope, abort);
3261 * d) Process tasks as defined in 5.7.1;
3262 * e) See above..
3263 * f) If the type or scope has changed, then for every I_T nexus
3264 * whose reservation key was not removed, except for the I_T
3265 * nexus on which the PERSISTENT RESERVE OUT command was
3266 * received, the device server shall establish a unit
3267 * attention condition for the initiator port associated with
3268 * that I_T nexus, with the additional sense code set to
3269 * RESERVATIONS RELEASED. If the type or scope have not
3270 * changed, then no unit attention condition(s) shall be
3271 * established for this reason.
3273 if ((prh_type != type) || (prh_scope != scope)) {
3274 spin_lock(&pr_tmpl->registration_lock);
3275 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3276 &pr_tmpl->registration_list, pr_reg_list) {
3278 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3279 if (calling_it_nexus)
3280 continue;
3282 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3283 pr_reg->pr_res_mapped_lun, 0x2A,
3284 ASCQ_2AH_RESERVATIONS_RELEASED);
3286 spin_unlock(&pr_tmpl->registration_lock);
3288 spin_unlock(&dev->dev_reservation_lock);
3290 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3291 * All received CDBs for the matching existing reservation and
3292 * registrations undergo ABORT_TASK logic.
3294 * From there, core_scsi3_release_preempt_and_abort() will
3295 * release every registration in the list (which have already
3296 * been removed from the primary pr_reg list), except the
3297 * new persistent reservation holder, the calling Initiator Port.
3299 if (abort) {
3300 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3301 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3302 pr_reg_n);
3305 if (pr_tmpl->pr_aptpl_active) {
3306 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3307 &pr_reg_n->pr_aptpl_buf[0],
3308 pr_tmpl->pr_aptpl_buf_len);
3309 if (!ret)
3310 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3311 "%s\n", (abort) ? "_AND_ABORT" : "");
3314 core_scsi3_put_pr_reg(pr_reg_n);
3315 core_scsi3_pr_generation(cmd->se_dev);
3316 return 0;
3319 static int core_scsi3_emulate_pro_preempt(
3320 struct se_cmd *cmd,
3321 int type,
3322 int scope,
3323 u64 res_key,
3324 u64 sa_res_key,
3325 int abort)
3327 int ret = 0;
3329 switch (type) {
3330 case PR_TYPE_WRITE_EXCLUSIVE:
3331 case PR_TYPE_EXCLUSIVE_ACCESS:
3332 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3333 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3334 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3335 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3336 ret = core_scsi3_pro_preempt(cmd, type, scope,
3337 res_key, sa_res_key, abort);
3338 break;
3339 default:
3340 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3341 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3342 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3343 return -EINVAL;
3346 return ret;
3350 static int core_scsi3_emulate_pro_register_and_move(
3351 struct se_cmd *cmd,
3352 u64 res_key,
3353 u64 sa_res_key,
3354 int aptpl,
3355 int unreg)
3357 struct se_session *se_sess = cmd->se_sess;
3358 struct se_device *dev = cmd->se_dev;
3359 struct se_dev_entry *se_deve, *dest_se_deve = NULL;
3360 struct se_lun *se_lun = cmd->se_lun;
3361 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3362 struct se_port *se_port;
3363 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3364 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3365 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3366 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
3367 unsigned char *buf;
3368 unsigned char *initiator_str;
3369 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3370 u32 tid_len, tmp_tid_len;
3371 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3372 unsigned short rtpi;
3373 unsigned char proto_ident;
3375 if (!se_sess || !se_lun) {
3376 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3377 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3378 return -EINVAL;
3380 memset(dest_iport, 0, 64);
3381 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3382 se_tpg = se_sess->se_tpg;
3383 tf_ops = se_tpg->se_tpg_tfo;
3384 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3386 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3387 * Register behaviors for a REGISTER AND MOVE service action
3389 * Locate the existing *pr_reg via struct se_node_acl pointers
3391 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3392 se_sess);
3393 if (!pr_reg) {
3394 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3395 " *pr_reg for REGISTER_AND_MOVE\n");
3396 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3397 return -EINVAL;
3400 * The provided reservation key much match the existing reservation key
3401 * provided during this initiator's I_T nexus registration.
3403 if (res_key != pr_reg->pr_res_key) {
3404 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3405 " res_key: 0x%016Lx does not match existing SA REGISTER"
3406 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3407 core_scsi3_put_pr_reg(pr_reg);
3408 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3409 return -EINVAL;
3412 * The service active reservation key needs to be non zero
3414 if (!sa_res_key) {
3415 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3416 " sa_res_key\n");
3417 core_scsi3_put_pr_reg(pr_reg);
3418 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3419 return -EINVAL;
3423 * Determine the Relative Target Port Identifier where the reservation
3424 * will be moved to for the TransportID containing SCSI initiator WWN
3425 * information.
3427 buf = transport_kmap_data_sg(cmd);
3428 rtpi = (buf[18] & 0xff) << 8;
3429 rtpi |= buf[19] & 0xff;
3430 tid_len = (buf[20] & 0xff) << 24;
3431 tid_len |= (buf[21] & 0xff) << 16;
3432 tid_len |= (buf[22] & 0xff) << 8;
3433 tid_len |= buf[23] & 0xff;
3434 transport_kunmap_data_sg(cmd);
3435 buf = NULL;
3437 if ((tid_len + 24) != cmd->data_length) {
3438 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3439 " does not equal CDB data_length: %u\n", tid_len,
3440 cmd->data_length);
3441 core_scsi3_put_pr_reg(pr_reg);
3442 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3443 return -EINVAL;
3446 spin_lock(&dev->se_port_lock);
3447 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3448 if (se_port->sep_rtpi != rtpi)
3449 continue;
3450 dest_se_tpg = se_port->sep_tpg;
3451 if (!dest_se_tpg)
3452 continue;
3453 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3454 if (!dest_tf_ops)
3455 continue;
3457 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3458 smp_mb__after_atomic_inc();
3459 spin_unlock(&dev->se_port_lock);
3461 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3462 if (ret != 0) {
3463 pr_err("core_scsi3_tpg_depend_item() failed"
3464 " for dest_se_tpg\n");
3465 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3466 smp_mb__after_atomic_dec();
3467 core_scsi3_put_pr_reg(pr_reg);
3468 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3469 return -EINVAL;
3472 spin_lock(&dev->se_port_lock);
3473 break;
3475 spin_unlock(&dev->se_port_lock);
3477 if (!dest_se_tpg || !dest_tf_ops) {
3478 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3479 " fabric ops from Relative Target Port Identifier:"
3480 " %hu\n", rtpi);
3481 core_scsi3_put_pr_reg(pr_reg);
3482 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3483 return -EINVAL;
3486 buf = transport_kmap_data_sg(cmd);
3487 proto_ident = (buf[24] & 0x0f);
3488 #if 0
3489 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3490 " 0x%02x\n", proto_ident);
3491 #endif
3492 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3493 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3494 " proto_ident: 0x%02x does not match ident: 0x%02x"
3495 " from fabric: %s\n", proto_ident,
3496 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3497 dest_tf_ops->get_fabric_name());
3498 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3499 ret = -EINVAL;
3500 goto out;
3502 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3503 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3504 " containg a valid tpg_parse_pr_out_transport_id"
3505 " function pointer\n");
3506 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3507 ret = -EINVAL;
3508 goto out;
3510 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3511 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3512 if (!initiator_str) {
3513 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3514 " initiator_str from Transport ID\n");
3515 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3516 ret = -EINVAL;
3517 goto out;
3520 transport_kunmap_data_sg(cmd);
3521 buf = NULL;
3523 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3524 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3525 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3526 iport_ptr : "");
3528 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3529 * action specifies a TransportID that is the same as the initiator port
3530 * of the I_T nexus for the command received, then the command shall
3531 * be terminated with CHECK CONDITION status, with the sense key set to
3532 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3533 * IN PARAMETER LIST.
3535 pr_reg_nacl = pr_reg->pr_reg_nacl;
3536 matching_iname = (!strcmp(initiator_str,
3537 pr_reg_nacl->initiatorname)) ? 1 : 0;
3538 if (!matching_iname)
3539 goto after_iport_check;
3541 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3542 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3543 " matches: %s on received I_T Nexus\n", initiator_str,
3544 pr_reg_nacl->initiatorname);
3545 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3546 ret = -EINVAL;
3547 goto out;
3549 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3550 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3551 " matches: %s %s on received I_T Nexus\n",
3552 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3553 pr_reg->pr_reg_isid);
3554 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3555 ret = -EINVAL;
3556 goto out;
3558 after_iport_check:
3560 * Locate the destination struct se_node_acl from the received Transport ID
3562 spin_lock_irq(&dest_se_tpg->acl_node_lock);
3563 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3564 initiator_str);
3565 if (dest_node_acl) {
3566 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3567 smp_mb__after_atomic_inc();
3569 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3571 if (!dest_node_acl) {
3572 pr_err("Unable to locate %s dest_node_acl for"
3573 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3574 initiator_str);
3575 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3576 ret = -EINVAL;
3577 goto out;
3579 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3580 if (ret != 0) {
3581 pr_err("core_scsi3_nodeacl_depend_item() for"
3582 " dest_node_acl\n");
3583 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3584 smp_mb__after_atomic_dec();
3585 dest_node_acl = NULL;
3586 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3587 ret = -EINVAL;
3588 goto out;
3590 #if 0
3591 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3592 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3593 dest_node_acl->initiatorname);
3594 #endif
3596 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3597 * PORT IDENTIFIER.
3599 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3600 if (!dest_se_deve) {
3601 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3602 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3603 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3604 ret = -EINVAL;
3605 goto out;
3608 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3609 if (ret < 0) {
3610 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3611 atomic_dec(&dest_se_deve->pr_ref_count);
3612 smp_mb__after_atomic_dec();
3613 dest_se_deve = NULL;
3614 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3615 ret = -EINVAL;
3616 goto out;
3618 #if 0
3619 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3620 " ACL for dest_se_deve->mapped_lun: %u\n",
3621 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3622 dest_se_deve->mapped_lun);
3623 #endif
3625 * A persistent reservation needs to already existing in order to
3626 * successfully complete the REGISTER_AND_MOVE service action..
3628 spin_lock(&dev->dev_reservation_lock);
3629 pr_res_holder = dev->dev_pr_res_holder;
3630 if (!pr_res_holder) {
3631 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3632 " currently held\n");
3633 spin_unlock(&dev->dev_reservation_lock);
3634 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3635 ret = -EINVAL;
3636 goto out;
3639 * The received on I_T Nexus must be the reservation holder.
3641 * From spc4r17 section 5.7.8 Table 50 --
3642 * Register behaviors for a REGISTER AND MOVE service action
3644 if (pr_res_holder != pr_reg) {
3645 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3646 " Nexus is not reservation holder\n");
3647 spin_unlock(&dev->dev_reservation_lock);
3648 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3649 ret = -EINVAL;
3650 goto out;
3653 * From spc4r17 section 5.7.8: registering and moving reservation
3655 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3656 * action is received and the established persistent reservation is a
3657 * Write Exclusive - All Registrants type or Exclusive Access -
3658 * All Registrants type reservation, then the command shall be completed
3659 * with RESERVATION CONFLICT status.
3661 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3662 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3663 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3664 " reservation for type: %s\n",
3665 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3666 spin_unlock(&dev->dev_reservation_lock);
3667 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3668 ret = -EINVAL;
3669 goto out;
3671 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3673 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3675 type = pr_res_holder->pr_res_type;
3676 scope = pr_res_holder->pr_res_type;
3678 * c) Associate the reservation key specified in the SERVICE ACTION
3679 * RESERVATION KEY field with the I_T nexus specified as the
3680 * destination of the register and move, where:
3681 * A) The I_T nexus is specified by the TransportID and the
3682 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3683 * B) Regardless of the TransportID format used, the association for
3684 * the initiator port is based on either the initiator port name
3685 * (see 3.1.71) on SCSI transport protocols where port names are
3686 * required or the initiator port identifier (see 3.1.70) on SCSI
3687 * transport protocols where port names are not required;
3688 * d) Register the reservation key specified in the SERVICE ACTION
3689 * RESERVATION KEY field;
3690 * e) Retain the reservation key specified in the SERVICE ACTION
3691 * RESERVATION KEY field and associated information;
3693 * Also, It is not an error for a REGISTER AND MOVE service action to
3694 * register an I_T nexus that is already registered with the same
3695 * reservation key or a different reservation key.
3697 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3698 iport_ptr);
3699 if (!dest_pr_reg) {
3700 ret = core_scsi3_alloc_registration(cmd->se_dev,
3701 dest_node_acl, dest_se_deve, iport_ptr,
3702 sa_res_key, 0, aptpl, 2, 1);
3703 if (ret != 0) {
3704 spin_unlock(&dev->dev_reservation_lock);
3705 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3706 ret = -EINVAL;
3707 goto out;
3709 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3710 iport_ptr);
3711 new_reg = 1;
3714 * f) Release the persistent reservation for the persistent reservation
3715 * holder (i.e., the I_T nexus on which the
3717 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3718 dev->dev_pr_res_holder, 0);
3720 * g) Move the persistent reservation to the specified I_T nexus using
3721 * the same scope and type as the persistent reservation released in
3722 * item f); and
3724 dev->dev_pr_res_holder = dest_pr_reg;
3725 dest_pr_reg->pr_res_holder = 1;
3726 dest_pr_reg->pr_res_type = type;
3727 pr_reg->pr_res_scope = scope;
3728 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3729 PR_REG_ISID_ID_LEN);
3731 * Increment PRGeneration for existing registrations..
3733 if (!new_reg)
3734 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3735 spin_unlock(&dev->dev_reservation_lock);
3737 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3738 " created new reservation holder TYPE: %s on object RTPI:"
3739 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3740 core_scsi3_pr_dump_type(type), rtpi,
3741 dest_pr_reg->pr_res_generation);
3742 pr_debug("SPC-3 PR Successfully moved reservation from"
3743 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3744 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3745 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3746 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3747 iport_ptr : "");
3749 * It is now safe to release configfs group dependencies for destination
3750 * of Transport ID Initiator Device/Port Identifier
3752 core_scsi3_lunacl_undepend_item(dest_se_deve);
3753 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3754 core_scsi3_tpg_undepend_item(dest_se_tpg);
3756 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3757 * nexus on which PERSISTENT RESERVE OUT command was received.
3759 if (unreg) {
3760 spin_lock(&pr_tmpl->registration_lock);
3761 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3762 spin_unlock(&pr_tmpl->registration_lock);
3763 } else
3764 core_scsi3_put_pr_reg(pr_reg);
3767 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3768 * write out the updated metadata to struct file for this SCSI device.
3770 if (!aptpl) {
3771 pr_tmpl->pr_aptpl_active = 0;
3772 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3773 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3774 " REGISTER_AND_MOVE\n");
3775 } else {
3776 pr_tmpl->pr_aptpl_active = 1;
3777 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3778 &dest_pr_reg->pr_aptpl_buf[0],
3779 pr_tmpl->pr_aptpl_buf_len);
3780 if (!ret)
3781 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3782 " REGISTER_AND_MOVE\n");
3785 transport_kunmap_data_sg(cmd);
3787 core_scsi3_put_pr_reg(dest_pr_reg);
3788 return 0;
3789 out:
3790 if (buf)
3791 transport_kunmap_data_sg(cmd);
3792 if (dest_se_deve)
3793 core_scsi3_lunacl_undepend_item(dest_se_deve);
3794 if (dest_node_acl)
3795 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3796 core_scsi3_tpg_undepend_item(dest_se_tpg);
3797 core_scsi3_put_pr_reg(pr_reg);
3798 return ret;
3801 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3803 unsigned int __v1, __v2;
3805 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3806 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3808 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3812 * See spc4r17 section 6.14 Table 170
3814 int target_scsi3_emulate_pr_out(struct se_task *task)
3816 struct se_cmd *cmd = task->task_se_cmd;
3817 unsigned char *cdb = &cmd->t_task_cdb[0];
3818 unsigned char *buf;
3819 u64 res_key, sa_res_key;
3820 int sa, scope, type, aptpl;
3821 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3822 int ret;
3825 * Following spc2r20 5.5.1 Reservations overview:
3827 * If a logical unit has been reserved by any RESERVE command and is
3828 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3829 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3830 * initiator or service action and shall terminate with a RESERVATION
3831 * CONFLICT status.
3833 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3834 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3835 " SPC-2 reservation is held, returning"
3836 " RESERVATION_CONFLICT\n");
3837 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3838 ret = EINVAL;
3839 goto out;
3843 * FIXME: A NULL struct se_session pointer means an this is not coming from
3844 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3846 if (!cmd->se_sess) {
3847 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3848 return -EINVAL;
3851 if (cmd->data_length < 24) {
3852 pr_warn("SPC-PR: Received PR OUT parameter list"
3853 " length too small: %u\n", cmd->data_length);
3854 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3855 ret = -EINVAL;
3856 goto out;
3859 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3861 sa = (cdb[1] & 0x1f);
3862 scope = (cdb[2] & 0xf0);
3863 type = (cdb[2] & 0x0f);
3865 buf = transport_kmap_data_sg(cmd);
3867 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3869 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3870 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3872 * REGISTER_AND_MOVE uses a different SA parameter list containing
3873 * SCSI TransportIDs.
3875 if (sa != PRO_REGISTER_AND_MOVE) {
3876 spec_i_pt = (buf[20] & 0x08);
3877 all_tg_pt = (buf[20] & 0x04);
3878 aptpl = (buf[20] & 0x01);
3879 } else {
3880 aptpl = (buf[17] & 0x01);
3881 unreg = (buf[17] & 0x02);
3883 transport_kunmap_data_sg(cmd);
3884 buf = NULL;
3887 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3889 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
3890 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3891 ret = -EINVAL;
3892 goto out;
3896 * From spc4r17 section 6.14:
3898 * If the SPEC_I_PT bit is set to zero, the service action is not
3899 * REGISTER AND MOVE, and the parameter list length is not 24, then
3900 * the command shall be terminated with CHECK CONDITION status, with
3901 * the sense key set to ILLEGAL REQUEST, and the additional sense
3902 * code set to PARAMETER LIST LENGTH ERROR.
3904 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3905 (cmd->data_length != 24)) {
3906 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3907 " list length: %u\n", cmd->data_length);
3908 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3909 ret = -EINVAL;
3910 goto out;
3913 * (core_scsi3_emulate_pro_* function parameters
3914 * are defined by spc4r17 Table 174:
3915 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3917 switch (sa) {
3918 case PRO_REGISTER:
3919 ret = core_scsi3_emulate_pro_register(cmd,
3920 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3921 break;
3922 case PRO_RESERVE:
3923 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3924 break;
3925 case PRO_RELEASE:
3926 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3927 break;
3928 case PRO_CLEAR:
3929 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3930 break;
3931 case PRO_PREEMPT:
3932 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3933 res_key, sa_res_key, 0);
3934 break;
3935 case PRO_PREEMPT_AND_ABORT:
3936 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3937 res_key, sa_res_key, 1);
3938 break;
3939 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3940 ret = core_scsi3_emulate_pro_register(cmd,
3941 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3942 break;
3943 case PRO_REGISTER_AND_MOVE:
3944 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3945 sa_res_key, aptpl, unreg);
3946 break;
3947 default:
3948 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3949 " action: 0x%02x\n", cdb[1] & 0x1f);
3950 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3951 ret = -EINVAL;
3952 break;
3955 out:
3956 if (!ret) {
3957 task->task_scsi_status = GOOD;
3958 transport_complete_task(task, 1);
3960 return ret;
3964 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3966 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3968 static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3970 struct se_device *se_dev = cmd->se_dev;
3971 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
3972 struct t10_pr_registration *pr_reg;
3973 unsigned char *buf;
3974 u32 add_len = 0, off = 8;
3976 if (cmd->data_length < 8) {
3977 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3978 " too small\n", cmd->data_length);
3979 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3980 return -EINVAL;
3983 buf = transport_kmap_data_sg(cmd);
3984 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3985 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3986 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3987 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
3989 spin_lock(&su_dev->t10_pr.registration_lock);
3990 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
3991 pr_reg_list) {
3993 * Check for overflow of 8byte PRI READ_KEYS payload and
3994 * next reservation key list descriptor.
3996 if ((add_len + 8) > (cmd->data_length - 8))
3997 break;
3999 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4000 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4001 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4002 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4003 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4004 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4005 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4006 buf[off++] = (pr_reg->pr_res_key & 0xff);
4008 add_len += 8;
4010 spin_unlock(&su_dev->t10_pr.registration_lock);
4012 buf[4] = ((add_len >> 24) & 0xff);
4013 buf[5] = ((add_len >> 16) & 0xff);
4014 buf[6] = ((add_len >> 8) & 0xff);
4015 buf[7] = (add_len & 0xff);
4017 transport_kunmap_data_sg(cmd);
4019 return 0;
4023 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4025 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4027 static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4029 struct se_device *se_dev = cmd->se_dev;
4030 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4031 struct t10_pr_registration *pr_reg;
4032 unsigned char *buf;
4033 u64 pr_res_key;
4034 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4036 if (cmd->data_length < 8) {
4037 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4038 " too small\n", cmd->data_length);
4039 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4040 return -EINVAL;
4043 buf = transport_kmap_data_sg(cmd);
4044 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4045 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4046 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4047 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4049 spin_lock(&se_dev->dev_reservation_lock);
4050 pr_reg = se_dev->dev_pr_res_holder;
4051 if ((pr_reg)) {
4053 * Set the hardcoded Additional Length
4055 buf[4] = ((add_len >> 24) & 0xff);
4056 buf[5] = ((add_len >> 16) & 0xff);
4057 buf[6] = ((add_len >> 8) & 0xff);
4058 buf[7] = (add_len & 0xff);
4060 if (cmd->data_length < 22)
4061 goto err;
4064 * Set the Reservation key.
4066 * From spc4r17, section 5.7.10:
4067 * A persistent reservation holder has its reservation key
4068 * returned in the parameter data from a PERSISTENT
4069 * RESERVE IN command with READ RESERVATION service action as
4070 * follows:
4071 * a) For a persistent reservation of the type Write Exclusive
4072 * - All Registrants or Exclusive Access ­ All Regitrants,
4073 * the reservation key shall be set to zero; or
4074 * b) For all other persistent reservation types, the
4075 * reservation key shall be set to the registered
4076 * reservation key for the I_T nexus that holds the
4077 * persistent reservation.
4079 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4080 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4081 pr_res_key = 0;
4082 else
4083 pr_res_key = pr_reg->pr_res_key;
4085 buf[8] = ((pr_res_key >> 56) & 0xff);
4086 buf[9] = ((pr_res_key >> 48) & 0xff);
4087 buf[10] = ((pr_res_key >> 40) & 0xff);
4088 buf[11] = ((pr_res_key >> 32) & 0xff);
4089 buf[12] = ((pr_res_key >> 24) & 0xff);
4090 buf[13] = ((pr_res_key >> 16) & 0xff);
4091 buf[14] = ((pr_res_key >> 8) & 0xff);
4092 buf[15] = (pr_res_key & 0xff);
4094 * Set the SCOPE and TYPE
4096 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4097 (pr_reg->pr_res_type & 0x0f);
4100 err:
4101 spin_unlock(&se_dev->dev_reservation_lock);
4102 transport_kunmap_data_sg(cmd);
4104 return 0;
4108 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4110 * See spc4r17 section 6.13.4 Table 165
4112 static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4114 struct se_device *dev = cmd->se_dev;
4115 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
4116 unsigned char *buf;
4117 u16 add_len = 8; /* Hardcoded to 8. */
4119 if (cmd->data_length < 6) {
4120 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4121 " %u too small\n", cmd->data_length);
4122 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4123 return -EINVAL;
4126 buf = transport_kmap_data_sg(cmd);
4128 buf[0] = ((add_len << 8) & 0xff);
4129 buf[1] = (add_len & 0xff);
4130 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4131 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4132 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4133 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4135 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4136 * set the TMV: Task Mask Valid bit.
4138 buf[3] |= 0x80;
4140 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4142 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4144 * PTPL_A: Persistence across Target Power Loss Active bit
4146 if (pr_tmpl->pr_aptpl_active)
4147 buf[3] |= 0x01;
4149 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4151 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4152 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4153 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4154 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4155 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4156 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4158 transport_kunmap_data_sg(cmd);
4160 return 0;
4164 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4166 * See spc4r17 section 6.13.5 Table 168 and 169
4168 static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4170 struct se_device *se_dev = cmd->se_dev;
4171 struct se_node_acl *se_nacl;
4172 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
4173 struct se_portal_group *se_tpg;
4174 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4175 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
4176 unsigned char *buf;
4177 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4178 u32 off = 8; /* off into first Full Status descriptor */
4179 int format_code = 0;
4181 if (cmd->data_length < 8) {
4182 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4183 " too small\n", cmd->data_length);
4184 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4185 return -EINVAL;
4188 buf = transport_kmap_data_sg(cmd);
4190 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4191 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4192 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4193 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
4195 spin_lock(&pr_tmpl->registration_lock);
4196 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4197 &pr_tmpl->registration_list, pr_reg_list) {
4199 se_nacl = pr_reg->pr_reg_nacl;
4200 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4201 add_desc_len = 0;
4203 atomic_inc(&pr_reg->pr_res_holders);
4204 smp_mb__after_atomic_inc();
4205 spin_unlock(&pr_tmpl->registration_lock);
4207 * Determine expected length of $FABRIC_MOD specific
4208 * TransportID full status descriptor..
4210 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4211 se_tpg, se_nacl, pr_reg, &format_code);
4213 if ((exp_desc_len + add_len) > cmd->data_length) {
4214 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4215 " out of buffer: %d\n", cmd->data_length);
4216 spin_lock(&pr_tmpl->registration_lock);
4217 atomic_dec(&pr_reg->pr_res_holders);
4218 smp_mb__after_atomic_dec();
4219 break;
4222 * Set RESERVATION KEY
4224 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4225 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4226 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4227 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4228 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4229 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4230 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4231 buf[off++] = (pr_reg->pr_res_key & 0xff);
4232 off += 4; /* Skip Over Reserved area */
4235 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4237 if (pr_reg->pr_reg_all_tg_pt)
4238 buf[off] = 0x02;
4240 * The struct se_lun pointer will be present for the
4241 * reservation holder for PR_HOLDER bit.
4243 * Also, if this registration is the reservation
4244 * holder, fill in SCOPE and TYPE in the next byte.
4246 if (pr_reg->pr_res_holder) {
4247 buf[off++] |= 0x01;
4248 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4249 (pr_reg->pr_res_type & 0x0f);
4250 } else
4251 off += 2;
4253 off += 4; /* Skip over reserved area */
4255 * From spc4r17 6.3.15:
4257 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4258 * IDENTIFIER field contains the relative port identifier (see
4259 * 3.1.120) of the target port that is part of the I_T nexus
4260 * described by this full status descriptor. If the ALL_TG_PT
4261 * bit is set to one, the contents of the RELATIVE TARGET PORT
4262 * IDENTIFIER field are not defined by this standard.
4264 if (!pr_reg->pr_reg_all_tg_pt) {
4265 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4267 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4268 buf[off++] = (port->sep_rtpi & 0xff);
4269 } else
4270 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4273 * Now, have the $FABRIC_MOD fill in the protocol identifier
4275 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4276 se_nacl, pr_reg, &format_code, &buf[off+4]);
4278 spin_lock(&pr_tmpl->registration_lock);
4279 atomic_dec(&pr_reg->pr_res_holders);
4280 smp_mb__after_atomic_dec();
4282 * Set the ADDITIONAL DESCRIPTOR LENGTH
4284 buf[off++] = ((desc_len >> 24) & 0xff);
4285 buf[off++] = ((desc_len >> 16) & 0xff);
4286 buf[off++] = ((desc_len >> 8) & 0xff);
4287 buf[off++] = (desc_len & 0xff);
4289 * Size of full desctipor header minus TransportID
4290 * containing $FABRIC_MOD specific) initiator device/port
4291 * WWN information.
4293 * See spc4r17 Section 6.13.5 Table 169
4295 add_desc_len = (24 + desc_len);
4297 off += desc_len;
4298 add_len += add_desc_len;
4300 spin_unlock(&pr_tmpl->registration_lock);
4302 * Set ADDITIONAL_LENGTH
4304 buf[4] = ((add_len >> 24) & 0xff);
4305 buf[5] = ((add_len >> 16) & 0xff);
4306 buf[6] = ((add_len >> 8) & 0xff);
4307 buf[7] = (add_len & 0xff);
4309 transport_kunmap_data_sg(cmd);
4311 return 0;
4314 int target_scsi3_emulate_pr_in(struct se_task *task)
4316 struct se_cmd *cmd = task->task_se_cmd;
4317 int ret;
4320 * Following spc2r20 5.5.1 Reservations overview:
4322 * If a logical unit has been reserved by any RESERVE command and is
4323 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4324 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4325 * initiator or service action and shall terminate with a RESERVATION
4326 * CONFLICT status.
4328 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4329 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4330 " SPC-2 reservation is held, returning"
4331 " RESERVATION_CONFLICT\n");
4332 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4333 return -EINVAL;
4336 switch (cmd->t_task_cdb[1] & 0x1f) {
4337 case PRI_READ_KEYS:
4338 ret = core_scsi3_pri_read_keys(cmd);
4339 break;
4340 case PRI_READ_RESERVATION:
4341 ret = core_scsi3_pri_read_reservation(cmd);
4342 break;
4343 case PRI_REPORT_CAPABILITIES:
4344 ret = core_scsi3_pri_report_capabilities(cmd);
4345 break;
4346 case PRI_READ_FULL_STATUS:
4347 ret = core_scsi3_pri_read_full_status(cmd);
4348 break;
4349 default:
4350 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4351 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4352 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4353 ret = -EINVAL;
4354 break;
4357 if (!ret) {
4358 task->task_scsi_status = GOOD;
4359 transport_complete_task(task, 1);
4361 return ret;
4364 static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4366 return 0;
4369 static int core_pt_seq_non_holder(
4370 struct se_cmd *cmd,
4371 unsigned char *cdb,
4372 u32 pr_reg_type)
4374 return 0;
4377 int core_setup_reservations(struct se_device *dev, int force_pt)
4379 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
4380 struct t10_reservation *rest = &su_dev->t10_pr;
4382 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4383 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4384 * cause a problem because libata and some SATA RAID HBAs appear
4385 * under Linux/SCSI, but to emulate reservations themselves.
4387 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4388 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
4389 rest->res_type = SPC_PASSTHROUGH;
4390 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4391 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
4392 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4393 " emulation\n", dev->transport->name);
4394 return 0;
4397 * If SPC-3 or above is reported by real or emulated struct se_device,
4398 * use emulated Persistent Reservations.
4400 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4401 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4402 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4403 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
4404 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4405 " emulation\n", dev->transport->name);
4406 } else {
4407 rest->res_type = SPC2_RESERVATIONS;
4408 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4409 rest->pr_ops.t10_seq_non_holder =
4410 &core_scsi2_reservation_seq_non_holder;
4411 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4412 dev->transport->name);
4415 return 0;