sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / target / target_core_pr.c
blobd761025144f9dc178cc43d4803b4c79b0147815b
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 * (c) Copyright 2009-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/vmalloc.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/fs.h>
34 #include <scsi/scsi_proto.h>
35 #include <asm/unaligned.h>
37 #include <target/target_core_base.h>
38 #include <target/target_core_backend.h>
39 #include <target/target_core_fabric.h>
41 #include "target_core_internal.h"
42 #include "target_core_pr.h"
43 #include "target_core_ua.h"
46 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
48 struct pr_transport_id_holder {
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 void 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 buf[0] = '\0';
64 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
67 enum register_type {
68 REGISTER,
69 REGISTER_AND_IGNORE_EXISTING_KEY,
70 REGISTER_AND_MOVE,
73 enum preempt_type {
74 PREEMPT,
75 PREEMPT_AND_ABORT,
78 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
79 struct t10_pr_registration *, int, int);
81 static int is_reservation_holder(
82 struct t10_pr_registration *pr_res_holder,
83 struct t10_pr_registration *pr_reg)
85 int pr_res_type;
87 if (pr_res_holder) {
88 pr_res_type = pr_res_holder->pr_res_type;
90 return pr_res_holder == pr_reg ||
91 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
92 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
94 return 0;
97 static sense_reason_t
98 target_scsi2_reservation_check(struct se_cmd *cmd)
100 struct se_device *dev = cmd->se_dev;
101 struct se_session *sess = cmd->se_sess;
103 switch (cmd->t_task_cdb[0]) {
104 case INQUIRY:
105 case RELEASE:
106 case RELEASE_10:
107 return 0;
108 default:
109 break;
112 if (!dev->dev_reserved_node_acl || !sess)
113 return 0;
115 if (dev->dev_reserved_node_acl != sess->se_node_acl)
116 return TCM_RESERVATION_CONFLICT;
118 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
119 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
120 return TCM_RESERVATION_CONFLICT;
123 return 0;
126 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
127 struct se_node_acl *, struct se_session *);
128 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
130 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
132 struct se_session *se_sess = cmd->se_sess;
133 struct se_device *dev = cmd->se_dev;
134 struct t10_pr_registration *pr_reg;
135 struct t10_reservation *pr_tmpl = &dev->t10_pr;
136 int conflict = 0;
138 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
139 se_sess);
140 if (pr_reg) {
142 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
143 * behavior
145 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
146 * status, but no reservation shall be established and the
147 * persistent reservation shall not be changed, if the command
148 * is received from a) and b) below.
150 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
151 * status, but the persistent reservation shall not be released,
152 * if the command is received from a) and b)
154 * a) An I_T nexus that is a persistent reservation holder; or
155 * b) An I_T nexus that is registered if a registrants only or
156 * all registrants type persistent reservation is present.
158 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
159 * RELEASE(6) command, or RELEASE(10) command shall be processed
160 * as defined in SPC-2.
162 if (pr_reg->pr_res_holder) {
163 core_scsi3_put_pr_reg(pr_reg);
164 return 1;
166 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
167 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
168 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
169 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
170 core_scsi3_put_pr_reg(pr_reg);
171 return 1;
173 core_scsi3_put_pr_reg(pr_reg);
174 conflict = 1;
175 } else {
177 * Following spc2r20 5.5.1 Reservations overview:
179 * If a logical unit has executed a PERSISTENT RESERVE OUT
180 * command with the REGISTER or the REGISTER AND IGNORE
181 * EXISTING KEY service action and is still registered by any
182 * initiator, all RESERVE commands and all RELEASE commands
183 * regardless of initiator shall conflict and shall terminate
184 * with a RESERVATION CONFLICT status.
186 spin_lock(&pr_tmpl->registration_lock);
187 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
188 spin_unlock(&pr_tmpl->registration_lock);
191 if (conflict) {
192 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
193 " while active SPC-3 registrations exist,"
194 " returning RESERVATION_CONFLICT\n");
195 return -EBUSY;
198 return 0;
201 sense_reason_t
202 target_scsi2_reservation_release(struct se_cmd *cmd)
204 struct se_device *dev = cmd->se_dev;
205 struct se_session *sess = cmd->se_sess;
206 struct se_portal_group *tpg;
207 int rc;
209 if (!sess || !sess->se_tpg)
210 goto out;
211 rc = target_check_scsi2_reservation_conflict(cmd);
212 if (rc == 1)
213 goto out;
214 if (rc < 0)
215 return TCM_RESERVATION_CONFLICT;
217 spin_lock(&dev->dev_reservation_lock);
218 if (!dev->dev_reserved_node_acl || !sess)
219 goto out_unlock;
221 if (dev->dev_reserved_node_acl != sess->se_node_acl)
222 goto out_unlock;
224 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
225 goto out_unlock;
227 dev->dev_reserved_node_acl = NULL;
228 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
229 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
230 dev->dev_res_bin_isid = 0;
231 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
233 tpg = sess->se_tpg;
234 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
235 " MAPPED LUN: %llu for %s\n",
236 tpg->se_tpg_tfo->get_fabric_name(),
237 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
238 sess->se_node_acl->initiatorname);
240 out_unlock:
241 spin_unlock(&dev->dev_reservation_lock);
242 out:
243 target_complete_cmd(cmd, GOOD);
244 return 0;
247 sense_reason_t
248 target_scsi2_reservation_reserve(struct se_cmd *cmd)
250 struct se_device *dev = cmd->se_dev;
251 struct se_session *sess = cmd->se_sess;
252 struct se_portal_group *tpg;
253 sense_reason_t ret = 0;
254 int rc;
256 if ((cmd->t_task_cdb[1] & 0x01) &&
257 (cmd->t_task_cdb[1] & 0x02)) {
258 pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
259 return TCM_UNSUPPORTED_SCSI_OPCODE;
262 * This is currently the case for target_core_mod passthrough struct se_cmd
263 * ops
265 if (!sess || !sess->se_tpg)
266 goto out;
267 rc = target_check_scsi2_reservation_conflict(cmd);
268 if (rc == 1)
269 goto out;
271 if (rc < 0)
272 return TCM_RESERVATION_CONFLICT;
274 tpg = sess->se_tpg;
275 spin_lock(&dev->dev_reservation_lock);
276 if (dev->dev_reserved_node_acl &&
277 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
278 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
279 tpg->se_tpg_tfo->get_fabric_name());
280 pr_err("Original reserver LUN: %llu %s\n",
281 cmd->se_lun->unpacked_lun,
282 dev->dev_reserved_node_acl->initiatorname);
283 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
284 " from %s \n", cmd->se_lun->unpacked_lun,
285 cmd->orig_fe_lun,
286 sess->se_node_acl->initiatorname);
287 ret = TCM_RESERVATION_CONFLICT;
288 goto out_unlock;
291 dev->dev_reserved_node_acl = sess->se_node_acl;
292 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
293 if (sess->sess_bin_isid != 0) {
294 dev->dev_res_bin_isid = sess->sess_bin_isid;
295 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
297 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
298 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
299 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
300 sess->se_node_acl->initiatorname);
302 out_unlock:
303 spin_unlock(&dev->dev_reservation_lock);
304 out:
305 if (!ret)
306 target_complete_cmd(cmd, GOOD);
307 return ret;
312 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
314 * This function is called by those initiator ports who are *NOT*
315 * the active PR reservation holder when a reservation is present.
317 static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
318 bool isid_mismatch)
320 unsigned char *cdb = cmd->t_task_cdb;
321 struct se_session *se_sess = cmd->se_sess;
322 struct se_node_acl *nacl = se_sess->se_node_acl;
323 int other_cdb = 0;
324 int registered_nexus = 0, ret = 1; /* Conflict by default */
325 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
326 int we = 0; /* Write Exclusive */
327 int legacy = 0; /* Act like a legacy device and return
328 * RESERVATION CONFLICT on some CDBs */
330 if (isid_mismatch) {
331 registered_nexus = 0;
332 } else {
333 struct se_dev_entry *se_deve;
335 rcu_read_lock();
336 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
337 if (se_deve)
338 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
339 &se_deve->deve_flags);
340 rcu_read_unlock();
343 switch (pr_reg_type) {
344 case PR_TYPE_WRITE_EXCLUSIVE:
345 we = 1;
346 case PR_TYPE_EXCLUSIVE_ACCESS:
348 * Some commands are only allowed for the persistent reservation
349 * holder.
351 break;
352 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
353 we = 1;
354 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
356 * Some commands are only allowed for registered I_T Nexuses.
358 reg_only = 1;
359 break;
360 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
361 we = 1;
362 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
364 * Each registered I_T Nexus is a reservation holder.
366 all_reg = 1;
367 break;
368 default:
369 return -EINVAL;
372 * Referenced from spc4r17 table 45 for *NON* PR holder access
374 switch (cdb[0]) {
375 case SECURITY_PROTOCOL_IN:
376 if (registered_nexus)
377 return 0;
378 ret = (we) ? 0 : 1;
379 break;
380 case MODE_SENSE:
381 case MODE_SENSE_10:
382 case READ_ATTRIBUTE:
383 case READ_BUFFER:
384 case RECEIVE_DIAGNOSTIC:
385 if (legacy) {
386 ret = 1;
387 break;
389 if (registered_nexus) {
390 ret = 0;
391 break;
393 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
394 break;
395 case PERSISTENT_RESERVE_OUT:
397 * This follows PERSISTENT_RESERVE_OUT service actions that
398 * are allowed in the presence of various reservations.
399 * See spc4r17, table 46
401 switch (cdb[1] & 0x1f) {
402 case PRO_CLEAR:
403 case PRO_PREEMPT:
404 case PRO_PREEMPT_AND_ABORT:
405 ret = (registered_nexus) ? 0 : 1;
406 break;
407 case PRO_REGISTER:
408 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
409 ret = 0;
410 break;
411 case PRO_REGISTER_AND_MOVE:
412 case PRO_RESERVE:
413 ret = 1;
414 break;
415 case PRO_RELEASE:
416 ret = (registered_nexus) ? 0 : 1;
417 break;
418 default:
419 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
420 " action: 0x%02x\n", cdb[1] & 0x1f);
421 return -EINVAL;
423 break;
424 case RELEASE:
425 case RELEASE_10:
426 /* Handled by CRH=1 in target_scsi2_reservation_release() */
427 ret = 0;
428 break;
429 case RESERVE:
430 case RESERVE_10:
431 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
432 ret = 0;
433 break;
434 case TEST_UNIT_READY:
435 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
436 break;
437 case MAINTENANCE_IN:
438 switch (cdb[1] & 0x1f) {
439 case MI_MANAGEMENT_PROTOCOL_IN:
440 if (registered_nexus) {
441 ret = 0;
442 break;
444 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
445 break;
446 case MI_REPORT_SUPPORTED_OPERATION_CODES:
447 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
448 if (legacy) {
449 ret = 1;
450 break;
452 if (registered_nexus) {
453 ret = 0;
454 break;
456 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
457 break;
458 case MI_REPORT_ALIASES:
459 case MI_REPORT_IDENTIFYING_INFORMATION:
460 case MI_REPORT_PRIORITY:
461 case MI_REPORT_TARGET_PGS:
462 case MI_REPORT_TIMESTAMP:
463 ret = 0; /* Allowed */
464 break;
465 default:
466 pr_err("Unknown MI Service Action: 0x%02x\n",
467 (cdb[1] & 0x1f));
468 return -EINVAL;
470 break;
471 case ACCESS_CONTROL_IN:
472 case ACCESS_CONTROL_OUT:
473 case INQUIRY:
474 case LOG_SENSE:
475 case SERVICE_ACTION_IN_12:
476 case REPORT_LUNS:
477 case REQUEST_SENSE:
478 case PERSISTENT_RESERVE_IN:
479 ret = 0; /*/ Allowed CDBs */
480 break;
481 default:
482 other_cdb = 1;
483 break;
486 * Case where the CDB is explicitly allowed in the above switch
487 * statement.
489 if (!ret && !other_cdb) {
490 pr_debug("Allowing explicit CDB: 0x%02x for %s"
491 " reservation holder\n", cdb[0],
492 core_scsi3_pr_dump_type(pr_reg_type));
494 return ret;
497 * Check if write exclusive initiator ports *NOT* holding the
498 * WRITE_EXCLUSIVE_* reservation.
500 if (we && !registered_nexus) {
501 if (cmd->data_direction == DMA_TO_DEVICE) {
503 * Conflict for write exclusive
505 pr_debug("%s Conflict for unregistered nexus"
506 " %s CDB: 0x%02x to %s reservation\n",
507 transport_dump_cmd_direction(cmd),
508 se_sess->se_node_acl->initiatorname, cdb[0],
509 core_scsi3_pr_dump_type(pr_reg_type));
510 return 1;
511 } else {
513 * Allow non WRITE CDBs for all Write Exclusive
514 * PR TYPEs to pass for registered and
515 * non-registered_nexuxes NOT holding the reservation.
517 * We only make noise for the unregisterd nexuses,
518 * as we expect registered non-reservation holding
519 * nexuses to issue CDBs.
522 if (!registered_nexus) {
523 pr_debug("Allowing implicit CDB: 0x%02x"
524 " for %s reservation on unregistered"
525 " nexus\n", cdb[0],
526 core_scsi3_pr_dump_type(pr_reg_type));
529 return 0;
531 } else if ((reg_only) || (all_reg)) {
532 if (registered_nexus) {
534 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
535 * allow commands from registered nexuses.
538 pr_debug("Allowing implicit CDB: 0x%02x for %s"
539 " reservation\n", cdb[0],
540 core_scsi3_pr_dump_type(pr_reg_type));
542 return 0;
544 } else if (we && registered_nexus) {
546 * Reads are allowed for Write Exclusive locks
547 * from all registrants.
549 if (cmd->data_direction == DMA_FROM_DEVICE) {
550 pr_debug("Allowing READ CDB: 0x%02x for %s"
551 " reservation\n", cdb[0],
552 core_scsi3_pr_dump_type(pr_reg_type));
554 return 0;
557 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
558 " for %s reservation\n", transport_dump_cmd_direction(cmd),
559 (registered_nexus) ? "" : "un",
560 se_sess->se_node_acl->initiatorname, cdb[0],
561 core_scsi3_pr_dump_type(pr_reg_type));
563 return 1; /* Conflict by default */
566 static sense_reason_t
567 target_scsi3_pr_reservation_check(struct se_cmd *cmd)
569 struct se_device *dev = cmd->se_dev;
570 struct se_session *sess = cmd->se_sess;
571 u32 pr_reg_type;
572 bool isid_mismatch = false;
574 if (!dev->dev_pr_res_holder)
575 return 0;
577 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
578 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
579 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
580 goto check_nonholder;
582 if (dev->dev_pr_res_holder->isid_present_at_reg) {
583 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
584 sess->sess_bin_isid) {
585 isid_mismatch = true;
586 goto check_nonholder;
590 return 0;
592 check_nonholder:
593 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
594 return TCM_RESERVATION_CONFLICT;
595 return 0;
598 static u32 core_scsi3_pr_generation(struct se_device *dev)
600 u32 prg;
603 * PRGeneration field shall contain the value of a 32-bit wrapping
604 * counter mainted by the device server.
606 * Note that this is done regardless of Active Persist across
607 * Target PowerLoss (APTPL)
609 * See spc4r17 section 6.3.12 READ_KEYS service action
611 spin_lock(&dev->dev_reservation_lock);
612 prg = dev->t10_pr.pr_generation++;
613 spin_unlock(&dev->dev_reservation_lock);
615 return prg;
618 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
619 struct se_device *dev,
620 struct se_node_acl *nacl,
621 struct se_lun *lun,
622 struct se_dev_entry *dest_deve,
623 u64 mapped_lun,
624 unsigned char *isid,
625 u64 sa_res_key,
626 int all_tg_pt,
627 int aptpl)
629 struct t10_pr_registration *pr_reg;
631 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
632 if (!pr_reg) {
633 pr_err("Unable to allocate struct t10_pr_registration\n");
634 return NULL;
637 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
638 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
639 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
640 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
641 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
642 atomic_set(&pr_reg->pr_res_holders, 0);
643 pr_reg->pr_reg_nacl = nacl;
645 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
646 * the se_dev_entry->pr_ref will have been already obtained by
647 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
649 * Otherwise, locate se_dev_entry now and obtain a reference until
650 * registration completes in __core_scsi3_add_registration().
652 if (dest_deve) {
653 pr_reg->pr_reg_deve = dest_deve;
654 } else {
655 rcu_read_lock();
656 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
657 if (!pr_reg->pr_reg_deve) {
658 rcu_read_unlock();
659 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
660 nacl->initiatorname, mapped_lun);
661 kmem_cache_free(t10_pr_reg_cache, pr_reg);
662 return NULL;
664 kref_get(&pr_reg->pr_reg_deve->pr_kref);
665 rcu_read_unlock();
667 pr_reg->pr_res_mapped_lun = mapped_lun;
668 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
669 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
670 pr_reg->pr_res_key = sa_res_key;
671 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
672 pr_reg->pr_reg_aptpl = aptpl;
674 * If an ISID value for this SCSI Initiator Port exists,
675 * save it to the registration now.
677 if (isid != NULL) {
678 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
679 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
680 pr_reg->isid_present_at_reg = 1;
683 return pr_reg;
686 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
687 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
690 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
691 * modes.
693 static struct t10_pr_registration *__core_scsi3_alloc_registration(
694 struct se_device *dev,
695 struct se_node_acl *nacl,
696 struct se_lun *lun,
697 struct se_dev_entry *deve,
698 u64 mapped_lun,
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_lun_acl *lacl_tmp;
707 struct se_lun *lun_tmp, *next, *dest_lun;
708 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
709 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
710 int ret;
712 * Create a registration for the I_T Nexus upon which the
713 * PROUT REGISTER was received.
715 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
716 isid, sa_res_key, all_tg_pt,
717 aptpl);
718 if (!pr_reg)
719 return NULL;
721 * Return pointer to pr_reg for ALL_TG_PT=0
723 if (!all_tg_pt)
724 return pr_reg;
726 * Create list of matching SCSI Initiator Port registrations
727 * for ALL_TG_PT=1
729 spin_lock(&dev->se_port_lock);
730 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
731 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
732 continue;
733 spin_unlock(&dev->se_port_lock);
735 spin_lock(&lun_tmp->lun_deve_lock);
736 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
738 * This pointer will be NULL for demo mode MappedLUNs
739 * that have not been make explicit via a ConfigFS
740 * MappedLUN group for the SCSI Initiator Node ACL.
742 if (!deve_tmp->se_lun_acl)
743 continue;
745 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
746 lockdep_is_held(&lun_tmp->lun_deve_lock));
747 nacl_tmp = lacl_tmp->se_lun_nacl;
749 * Skip the matching struct se_node_acl that is allocated
750 * above..
752 if (nacl == nacl_tmp)
753 continue;
755 * Only perform PR registrations for target ports on
756 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
757 * arrived.
759 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
760 continue;
762 * Look for a matching Initiator Node ACL in ASCII format
764 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
765 continue;
767 kref_get(&deve_tmp->pr_kref);
768 spin_unlock(&lun_tmp->lun_deve_lock);
770 * Grab a configfs group dependency that is released
771 * for the exception path at label out: below, or upon
772 * completion of adding ALL_TG_PT=1 registrations in
773 * __core_scsi3_add_registration()
775 ret = core_scsi3_lunacl_depend_item(deve_tmp);
776 if (ret < 0) {
777 pr_err("core_scsi3_lunacl_depend"
778 "_item() failed\n");
779 percpu_ref_put(&lun_tmp->lun_ref);
780 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
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 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
791 atomic_read(&deve_tmp->pr_kref.refcount) != 0);
793 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
794 nacl_tmp, dest_lun, deve_tmp,
795 deve_tmp->mapped_lun, NULL,
796 sa_res_key, all_tg_pt, aptpl);
797 if (!pr_reg_atp) {
798 percpu_ref_put(&lun_tmp->lun_ref);
799 core_scsi3_lunacl_undepend_item(deve_tmp);
800 goto out;
803 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
804 &pr_reg->pr_reg_atp_list);
805 spin_lock(&lun_tmp->lun_deve_lock);
807 spin_unlock(&lun_tmp->lun_deve_lock);
809 spin_lock(&dev->se_port_lock);
810 percpu_ref_put(&lun_tmp->lun_ref);
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 u64 mapped_lun,
832 unsigned char *t_port,
833 u16 tpgt,
834 u64 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;
852 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
853 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
854 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
855 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
856 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
857 atomic_set(&pr_reg->pr_res_holders, 0);
858 pr_reg->pr_reg_nacl = NULL;
859 pr_reg->pr_reg_deve = NULL;
860 pr_reg->pr_res_mapped_lun = mapped_lun;
861 pr_reg->pr_aptpl_target_lun = target_lun;
862 pr_reg->pr_res_key = sa_res_key;
863 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
864 pr_reg->pr_reg_aptpl = 1;
865 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
866 pr_reg->pr_res_type = type;
868 * If an ISID value had been saved in APTPL metadata for this
869 * SCSI Initiator Port, restore it now.
871 if (isid != NULL) {
872 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
873 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
874 pr_reg->isid_present_at_reg = 1;
877 * Copy the i_port and t_port information from caller.
879 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
880 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
881 pr_reg->pr_reg_tpgt = tpgt;
883 * Set pr_res_holder from caller, the pr_reg who is the reservation
884 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
885 * the Initiator Node LUN ACL from the fabric module is created for
886 * this registration.
888 pr_reg->pr_res_holder = res_holder;
890 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
891 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
892 " metadata\n", (res_holder) ? "+reservation" : "");
893 return 0;
896 static void core_scsi3_aptpl_reserve(
897 struct se_device *dev,
898 struct se_portal_group *tpg,
899 struct se_node_acl *node_acl,
900 struct t10_pr_registration *pr_reg)
902 char i_buf[PR_REG_ISID_ID_LEN];
904 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
905 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
907 spin_lock(&dev->dev_reservation_lock);
908 dev->dev_pr_res_holder = pr_reg;
909 spin_unlock(&dev->dev_reservation_lock);
911 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
912 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
913 tpg->se_tpg_tfo->get_fabric_name(),
914 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
915 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
916 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
917 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
918 i_buf);
921 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
922 struct t10_pr_registration *, enum register_type, int);
924 static int __core_scsi3_check_aptpl_registration(
925 struct se_device *dev,
926 struct se_portal_group *tpg,
927 struct se_lun *lun,
928 u64 target_lun,
929 struct se_node_acl *nacl,
930 u64 mapped_lun)
932 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
933 struct t10_reservation *pr_tmpl = &dev->t10_pr;
934 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
935 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
936 u16 tpgt;
938 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
939 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
941 * Copy Initiator Port information from struct se_node_acl
943 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
944 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
945 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
946 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
948 * Look for the matching registrations+reservation from those
949 * created from APTPL metadata. Note that multiple registrations
950 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
951 * TransportIDs.
953 spin_lock(&pr_tmpl->aptpl_reg_lock);
954 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
955 pr_reg_aptpl_list) {
957 if (!strcmp(pr_reg->pr_iport, i_port) &&
958 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
959 !(strcmp(pr_reg->pr_tport, t_port)) &&
960 (pr_reg->pr_reg_tpgt == tpgt) &&
961 (pr_reg->pr_aptpl_target_lun == target_lun)) {
963 * Obtain the ->pr_reg_deve pointer + reference, that
964 * is released by __core_scsi3_add_registration() below.
966 rcu_read_lock();
967 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
968 if (!pr_reg->pr_reg_deve) {
969 pr_err("Unable to locate PR APTPL %s mapped_lun:"
970 " %llu\n", nacl->initiatorname, mapped_lun);
971 rcu_read_unlock();
972 continue;
974 kref_get(&pr_reg->pr_reg_deve->pr_kref);
975 rcu_read_unlock();
977 pr_reg->pr_reg_nacl = nacl;
978 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
979 list_del(&pr_reg->pr_reg_aptpl_list);
980 spin_unlock(&pr_tmpl->aptpl_reg_lock);
982 * At this point all of the pointers in *pr_reg will
983 * be setup, so go ahead and add the registration.
985 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
987 * If this registration is the reservation holder,
988 * make that happen now..
990 if (pr_reg->pr_res_holder)
991 core_scsi3_aptpl_reserve(dev, tpg,
992 nacl, pr_reg);
994 * Reenable pr_aptpl_active to accept new metadata
995 * updates once the SCSI device is active again..
997 spin_lock(&pr_tmpl->aptpl_reg_lock);
998 pr_tmpl->pr_aptpl_active = 1;
1001 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1003 return 0;
1006 int core_scsi3_check_aptpl_registration(
1007 struct se_device *dev,
1008 struct se_portal_group *tpg,
1009 struct se_lun *lun,
1010 struct se_node_acl *nacl,
1011 u64 mapped_lun)
1013 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1014 return 0;
1016 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1017 lun->unpacked_lun, nacl,
1018 mapped_lun);
1021 static void __core_scsi3_dump_registration(
1022 const struct target_core_fabric_ops *tfo,
1023 struct se_device *dev,
1024 struct se_node_acl *nacl,
1025 struct t10_pr_registration *pr_reg,
1026 enum register_type register_type)
1028 struct se_portal_group *se_tpg = nacl->se_tpg;
1029 char i_buf[PR_REG_ISID_ID_LEN];
1031 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1032 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1034 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1035 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
1036 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1037 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1038 i_buf);
1039 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1040 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1041 tfo->tpg_get_tag(se_tpg));
1042 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1043 " Port(s)\n", tfo->get_fabric_name(),
1044 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1045 dev->transport->name);
1046 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1047 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1048 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1049 pr_reg->pr_reg_aptpl);
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 enum register_type register_type,
1057 int register_move)
1059 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1060 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1061 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1062 struct se_dev_entry *deve;
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 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);
1080 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1081 spin_unlock(&pr_tmpl->registration_lock);
1083 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1085 if (!pr_reg->pr_reg_all_tg_pt || register_move)
1086 goto out;
1088 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1089 * allocated in __core_scsi3_alloc_registration()
1091 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1092 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1093 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1095 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1097 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1099 spin_lock(&pr_tmpl->registration_lock);
1100 list_add_tail(&pr_reg_tmp->pr_reg_list,
1101 &pr_tmpl->registration_list);
1103 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1104 register_type);
1105 spin_unlock(&pr_tmpl->registration_lock);
1107 * Drop configfs group dependency reference and deve->pr_kref
1108 * obtained from __core_scsi3_alloc_registration() code.
1110 rcu_read_lock();
1111 deve = pr_reg_tmp->pr_reg_deve;
1112 if (deve) {
1113 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1114 core_scsi3_lunacl_undepend_item(deve);
1115 pr_reg_tmp->pr_reg_deve = NULL;
1117 rcu_read_unlock();
1119 out:
1121 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1123 rcu_read_lock();
1124 deve = pr_reg->pr_reg_deve;
1125 if (deve) {
1126 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1127 kref_put(&deve->pr_kref, target_pr_kref_release);
1128 pr_reg->pr_reg_deve = NULL;
1130 rcu_read_unlock();
1133 static int core_scsi3_alloc_registration(
1134 struct se_device *dev,
1135 struct se_node_acl *nacl,
1136 struct se_lun *lun,
1137 struct se_dev_entry *deve,
1138 u64 mapped_lun,
1139 unsigned char *isid,
1140 u64 sa_res_key,
1141 int all_tg_pt,
1142 int aptpl,
1143 enum register_type register_type,
1144 int register_move)
1146 struct t10_pr_registration *pr_reg;
1148 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1149 isid, sa_res_key, all_tg_pt,
1150 aptpl);
1151 if (!pr_reg)
1152 return -EPERM;
1154 __core_scsi3_add_registration(dev, nacl, pr_reg,
1155 register_type, register_move);
1156 return 0;
1159 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1160 struct se_device *dev,
1161 struct se_node_acl *nacl,
1162 unsigned char *isid)
1164 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1165 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1166 struct se_portal_group *tpg;
1168 spin_lock(&pr_tmpl->registration_lock);
1169 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1170 &pr_tmpl->registration_list, pr_reg_list) {
1172 * First look for a matching struct se_node_acl
1174 if (pr_reg->pr_reg_nacl != nacl)
1175 continue;
1177 tpg = pr_reg->pr_reg_nacl->se_tpg;
1179 * If this registration does NOT contain a fabric provided
1180 * ISID, then we have found a match.
1182 if (!pr_reg->isid_present_at_reg) {
1184 * Determine if this SCSI device server requires that
1185 * SCSI Intiatior TransportID w/ ISIDs is enforced
1186 * for fabric modules (iSCSI) requiring them.
1188 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1189 if (dev->dev_attrib.enforce_pr_isids)
1190 continue;
1192 atomic_inc_mb(&pr_reg->pr_res_holders);
1193 spin_unlock(&pr_tmpl->registration_lock);
1194 return pr_reg;
1197 * If the *pr_reg contains a fabric defined ISID for multi-value
1198 * SCSI Initiator Port TransportIDs, then we expect a valid
1199 * matching ISID to be provided by the local SCSI Initiator Port.
1201 if (!isid)
1202 continue;
1203 if (strcmp(isid, pr_reg->pr_reg_isid))
1204 continue;
1206 atomic_inc_mb(&pr_reg->pr_res_holders);
1207 spin_unlock(&pr_tmpl->registration_lock);
1208 return pr_reg;
1210 spin_unlock(&pr_tmpl->registration_lock);
1212 return NULL;
1215 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1216 struct se_device *dev,
1217 struct se_node_acl *nacl,
1218 struct se_session *sess)
1220 struct se_portal_group *tpg = nacl->se_tpg;
1221 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1223 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1224 memset(&buf[0], 0, PR_REG_ISID_LEN);
1225 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1226 PR_REG_ISID_LEN);
1227 isid_ptr = &buf[0];
1230 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1233 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1235 atomic_dec_mb(&pr_reg->pr_res_holders);
1238 static int core_scsi3_check_implicit_release(
1239 struct se_device *dev,
1240 struct t10_pr_registration *pr_reg)
1242 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1243 struct t10_pr_registration *pr_res_holder;
1244 int ret = 0;
1246 spin_lock(&dev->dev_reservation_lock);
1247 pr_res_holder = dev->dev_pr_res_holder;
1248 if (!pr_res_holder) {
1249 spin_unlock(&dev->dev_reservation_lock);
1250 return ret;
1252 if (pr_res_holder == pr_reg) {
1254 * Perform an implicit RELEASE if the registration that
1255 * is being released is holding the reservation.
1257 * From spc4r17, section 5.7.11.1:
1259 * e) If the I_T nexus is the persistent reservation holder
1260 * and the persistent reservation is not an all registrants
1261 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1262 * service action or REGISTER AND IGNORE EXISTING KEY
1263 * service action with the SERVICE ACTION RESERVATION KEY
1264 * field set to zero (see 5.7.11.3).
1266 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1267 ret = 1;
1269 * For 'All Registrants' reservation types, all existing
1270 * registrations are still processed as reservation holders
1271 * in core_scsi3_pr_seq_non_holder() after the initial
1272 * reservation holder is implicitly released here.
1274 } else if (pr_reg->pr_reg_all_tg_pt &&
1275 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1276 pr_reg->pr_reg_nacl->initiatorname)) &&
1277 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1278 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1279 " UNREGISTER while existing reservation with matching"
1280 " key 0x%016Lx is present from another SCSI Initiator"
1281 " Port\n", pr_reg->pr_res_key);
1282 ret = -EPERM;
1284 spin_unlock(&dev->dev_reservation_lock);
1286 return ret;
1290 * Called with struct t10_reservation->registration_lock held.
1292 static void __core_scsi3_free_registration(
1293 struct se_device *dev,
1294 struct t10_pr_registration *pr_reg,
1295 struct list_head *preempt_and_abort_list,
1296 int dec_holders)
1297 __releases(&pr_tmpl->registration_lock)
1298 __acquires(&pr_tmpl->registration_lock)
1300 const struct target_core_fabric_ops *tfo =
1301 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1302 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1303 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1304 struct se_dev_entry *deve;
1305 char i_buf[PR_REG_ISID_ID_LEN];
1307 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1308 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1310 if (!list_empty(&pr_reg->pr_reg_list))
1311 list_del(&pr_reg->pr_reg_list);
1313 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1314 * so call core_scsi3_put_pr_reg() to decrement our reference.
1316 if (dec_holders)
1317 core_scsi3_put_pr_reg(pr_reg);
1319 spin_unlock(&pr_tmpl->registration_lock);
1321 * Wait until all reference from any other I_T nexuses for this
1322 * *pr_reg have been released. Because list_del() is called above,
1323 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1324 * count back to zero, and we release *pr_reg.
1326 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1327 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1328 tfo->get_fabric_name());
1329 cpu_relax();
1332 rcu_read_lock();
1333 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1334 if (deve)
1335 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1336 rcu_read_unlock();
1338 spin_lock(&pr_tmpl->registration_lock);
1339 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1340 " Node: %s%s\n", tfo->get_fabric_name(),
1341 pr_reg->pr_reg_nacl->initiatorname,
1342 i_buf);
1343 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1344 " Port(s)\n", tfo->get_fabric_name(),
1345 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1346 dev->transport->name);
1347 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1348 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1349 pr_reg->pr_res_generation);
1351 if (!preempt_and_abort_list) {
1352 pr_reg->pr_reg_deve = NULL;
1353 pr_reg->pr_reg_nacl = NULL;
1354 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1355 return;
1358 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1359 * are released once the ABORT_TASK_SET has completed..
1361 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1364 void core_scsi3_free_pr_reg_from_nacl(
1365 struct se_device *dev,
1366 struct se_node_acl *nacl)
1368 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1369 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1370 bool free_reg = false;
1372 * If the passed se_node_acl matches the reservation holder,
1373 * release the reservation.
1375 spin_lock(&dev->dev_reservation_lock);
1376 pr_res_holder = dev->dev_pr_res_holder;
1377 if ((pr_res_holder != NULL) &&
1378 (pr_res_holder->pr_reg_nacl == nacl)) {
1379 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1380 free_reg = true;
1382 spin_unlock(&dev->dev_reservation_lock);
1384 * Release any registration associated with the struct se_node_acl.
1386 spin_lock(&pr_tmpl->registration_lock);
1387 if (pr_res_holder && free_reg)
1388 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1390 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1391 &pr_tmpl->registration_list, pr_reg_list) {
1393 if (pr_reg->pr_reg_nacl != nacl)
1394 continue;
1396 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1398 spin_unlock(&pr_tmpl->registration_lock);
1401 void core_scsi3_free_all_registrations(
1402 struct se_device *dev)
1404 struct t10_reservation *pr_tmpl = &dev->t10_pr;
1405 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1407 spin_lock(&dev->dev_reservation_lock);
1408 pr_res_holder = dev->dev_pr_res_holder;
1409 if (pr_res_holder != NULL) {
1410 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1411 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1412 pr_res_holder, 0, 0);
1414 spin_unlock(&dev->dev_reservation_lock);
1416 spin_lock(&pr_tmpl->registration_lock);
1417 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1418 &pr_tmpl->registration_list, pr_reg_list) {
1420 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1422 spin_unlock(&pr_tmpl->registration_lock);
1424 spin_lock(&pr_tmpl->aptpl_reg_lock);
1425 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1426 pr_reg_aptpl_list) {
1427 list_del(&pr_reg->pr_reg_aptpl_list);
1428 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1430 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1433 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1435 return target_depend_item(&tpg->tpg_group.cg_item);
1438 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1440 target_undepend_item(&tpg->tpg_group.cg_item);
1441 atomic_dec_mb(&tpg->tpg_pr_ref_count);
1444 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1446 if (nacl->dynamic_node_acl)
1447 return 0;
1448 return target_depend_item(&nacl->acl_group.cg_item);
1451 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1453 if (!nacl->dynamic_node_acl)
1454 target_undepend_item(&nacl->acl_group.cg_item);
1455 atomic_dec_mb(&nacl->acl_pr_ref_count);
1458 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1460 struct se_lun_acl *lun_acl;
1463 * For nacl->dynamic_node_acl=1
1465 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1466 atomic_read(&se_deve->pr_kref.refcount) != 0);
1467 if (!lun_acl)
1468 return 0;
1470 return target_depend_item(&lun_acl->se_lun_group.cg_item);
1473 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1475 struct se_lun_acl *lun_acl;
1478 * For nacl->dynamic_node_acl=1
1480 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1481 atomic_read(&se_deve->pr_kref.refcount) != 0);
1482 if (!lun_acl) {
1483 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1484 return;
1487 target_undepend_item(&lun_acl->se_lun_group.cg_item);
1488 kref_put(&se_deve->pr_kref, target_pr_kref_release);
1491 static sense_reason_t
1492 core_scsi3_decode_spec_i_port(
1493 struct se_cmd *cmd,
1494 struct se_portal_group *tpg,
1495 unsigned char *l_isid,
1496 u64 sa_res_key,
1497 int all_tg_pt,
1498 int aptpl)
1500 struct se_device *dev = cmd->se_dev;
1501 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1502 struct se_session *se_sess = cmd->se_sess;
1503 struct se_node_acl *dest_node_acl = NULL;
1504 struct se_dev_entry *dest_se_deve = NULL;
1505 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1506 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1507 LIST_HEAD(tid_dest_list);
1508 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1509 unsigned char *buf, *ptr, proto_ident;
1510 const unsigned char *i_str = NULL;
1511 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1512 sense_reason_t ret;
1513 u32 tpdl, tid_len = 0;
1514 u32 dest_rtpi = 0;
1517 * Allocate a struct pr_transport_id_holder and setup the
1518 * local_node_acl pointer and add to struct list_head tid_dest_list
1519 * for add registration processing in the loop of tid_dest_list below.
1521 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1522 if (!tidh_new) {
1523 pr_err("Unable to allocate tidh_new\n");
1524 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1526 INIT_LIST_HEAD(&tidh_new->dest_list);
1527 tidh_new->dest_tpg = tpg;
1528 tidh_new->dest_node_acl = se_sess->se_node_acl;
1530 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1531 se_sess->se_node_acl, cmd->se_lun,
1532 NULL, cmd->orig_fe_lun, l_isid,
1533 sa_res_key, all_tg_pt, aptpl);
1534 if (!local_pr_reg) {
1535 kfree(tidh_new);
1536 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1538 tidh_new->dest_pr_reg = local_pr_reg;
1540 * The local I_T nexus does not hold any configfs dependances,
1541 * so we set tidh_new->dest_se_deve to NULL to prevent the
1542 * configfs_undepend_item() calls in the tid_dest_list loops below.
1544 tidh_new->dest_se_deve = NULL;
1545 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1547 if (cmd->data_length < 28) {
1548 pr_warn("SPC-PR: Received PR OUT parameter list"
1549 " length too small: %u\n", cmd->data_length);
1550 ret = TCM_INVALID_PARAMETER_LIST;
1551 goto out;
1554 buf = transport_kmap_data_sg(cmd);
1555 if (!buf) {
1556 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1557 goto out;
1561 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1562 * first extract TransportID Parameter Data Length, and make sure
1563 * the value matches up to the SCSI expected data transfer length.
1565 tpdl = (buf[24] & 0xff) << 24;
1566 tpdl |= (buf[25] & 0xff) << 16;
1567 tpdl |= (buf[26] & 0xff) << 8;
1568 tpdl |= buf[27] & 0xff;
1570 if ((tpdl + 28) != cmd->data_length) {
1571 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1572 " does not equal CDB data_length: %u\n", tpdl,
1573 cmd->data_length);
1574 ret = TCM_INVALID_PARAMETER_LIST;
1575 goto out_unmap;
1578 * Start processing the received transport IDs using the
1579 * receiving I_T Nexus portal's fabric dependent methods to
1580 * obtain the SCSI Initiator Port/Device Identifiers.
1582 ptr = &buf[28];
1584 while (tpdl > 0) {
1585 struct se_lun *dest_lun, *tmp_lun;
1587 proto_ident = (ptr[0] & 0x0f);
1588 dest_tpg = NULL;
1590 spin_lock(&dev->se_port_lock);
1591 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1592 tmp_tpg = tmp_lun->lun_tpg;
1595 * Look for the matching proto_ident provided by
1596 * the received TransportID
1598 if (tmp_tpg->proto_id != proto_ident)
1599 continue;
1600 dest_rtpi = tmp_lun->lun_rtpi;
1602 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1603 (const char *)ptr, &tid_len, &iport_ptr);
1604 if (!i_str)
1605 continue;
1607 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1608 spin_unlock(&dev->se_port_lock);
1610 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1611 pr_err(" core_scsi3_tpg_depend_item()"
1612 " for tmp_tpg\n");
1613 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1614 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1615 goto out_unmap;
1618 * Locate the destination initiator ACL to be registered
1619 * from the decoded fabric module specific TransportID
1620 * at *i_str.
1622 mutex_lock(&tmp_tpg->acl_node_mutex);
1623 dest_node_acl = __core_tpg_get_initiator_node_acl(
1624 tmp_tpg, i_str);
1625 if (dest_node_acl)
1626 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1627 mutex_unlock(&tmp_tpg->acl_node_mutex);
1629 if (!dest_node_acl) {
1630 core_scsi3_tpg_undepend_item(tmp_tpg);
1631 spin_lock(&dev->se_port_lock);
1632 continue;
1635 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1636 pr_err("configfs_depend_item() failed"
1637 " for dest_node_acl->acl_group\n");
1638 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1639 core_scsi3_tpg_undepend_item(tmp_tpg);
1640 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1641 goto out_unmap;
1644 dest_tpg = tmp_tpg;
1645 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1646 " %s Port RTPI: %hu\n",
1647 dest_tpg->se_tpg_tfo->get_fabric_name(),
1648 dest_node_acl->initiatorname, dest_rtpi);
1650 spin_lock(&dev->se_port_lock);
1651 break;
1653 spin_unlock(&dev->se_port_lock);
1655 if (!dest_tpg) {
1656 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1657 " dest_tpg\n");
1658 ret = TCM_INVALID_PARAMETER_LIST;
1659 goto out_unmap;
1662 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1663 " tid_len: %d for %s + %s\n",
1664 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1665 tpdl, tid_len, i_str, iport_ptr);
1667 if (tid_len > tpdl) {
1668 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1669 " %u for Transport ID: %s\n", tid_len, ptr);
1670 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1671 core_scsi3_tpg_undepend_item(dest_tpg);
1672 ret = TCM_INVALID_PARAMETER_LIST;
1673 goto out_unmap;
1676 * Locate the desintation struct se_dev_entry pointer for matching
1677 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1678 * Target Port.
1680 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1681 dest_rtpi);
1682 if (!dest_se_deve) {
1683 pr_err("Unable to locate %s dest_se_deve"
1684 " from destination RTPI: %hu\n",
1685 dest_tpg->se_tpg_tfo->get_fabric_name(),
1686 dest_rtpi);
1688 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1689 core_scsi3_tpg_undepend_item(dest_tpg);
1690 ret = TCM_INVALID_PARAMETER_LIST;
1691 goto out_unmap;
1694 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1695 pr_err("core_scsi3_lunacl_depend_item()"
1696 " failed\n");
1697 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1698 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1699 core_scsi3_tpg_undepend_item(dest_tpg);
1700 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1701 goto out_unmap;
1704 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1705 " dest_se_deve mapped_lun: %llu\n",
1706 dest_tpg->se_tpg_tfo->get_fabric_name(),
1707 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1710 * Skip any TransportIDs that already have a registration for
1711 * this target port.
1713 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1714 iport_ptr);
1715 if (pr_reg_e) {
1716 core_scsi3_put_pr_reg(pr_reg_e);
1717 core_scsi3_lunacl_undepend_item(dest_se_deve);
1718 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1719 core_scsi3_tpg_undepend_item(dest_tpg);
1720 ptr += tid_len;
1721 tpdl -= tid_len;
1722 tid_len = 0;
1723 continue;
1726 * Allocate a struct pr_transport_id_holder and setup
1727 * the dest_node_acl and dest_se_deve pointers for the
1728 * loop below.
1730 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1731 GFP_KERNEL);
1732 if (!tidh_new) {
1733 pr_err("Unable to allocate tidh_new\n");
1734 core_scsi3_lunacl_undepend_item(dest_se_deve);
1735 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1736 core_scsi3_tpg_undepend_item(dest_tpg);
1737 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1738 goto out_unmap;
1740 INIT_LIST_HEAD(&tidh_new->dest_list);
1741 tidh_new->dest_tpg = dest_tpg;
1742 tidh_new->dest_node_acl = dest_node_acl;
1743 tidh_new->dest_se_deve = dest_se_deve;
1746 * Allocate, but do NOT add the registration for the
1747 * TransportID referenced SCSI Initiator port. This
1748 * done because of the following from spc4r17 in section
1749 * 6.14.3 wrt SPEC_I_PT:
1751 * "If a registration fails for any initiator port (e.g., if th
1752 * logical unit does not have enough resources available to
1753 * hold the registration information), no registrations shall be
1754 * made, and the command shall be terminated with
1755 * CHECK CONDITION status."
1757 * That means we call __core_scsi3_alloc_registration() here,
1758 * and then call __core_scsi3_add_registration() in the
1759 * 2nd loop which will never fail.
1761 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
1762 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
1764 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1765 dest_node_acl, dest_lun, dest_se_deve,
1766 dest_se_deve->mapped_lun, iport_ptr,
1767 sa_res_key, all_tg_pt, aptpl);
1768 if (!dest_pr_reg) {
1769 core_scsi3_lunacl_undepend_item(dest_se_deve);
1770 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1771 core_scsi3_tpg_undepend_item(dest_tpg);
1772 kfree(tidh_new);
1773 ret = TCM_INVALID_PARAMETER_LIST;
1774 goto out_unmap;
1776 tidh_new->dest_pr_reg = dest_pr_reg;
1777 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1779 ptr += tid_len;
1780 tpdl -= tid_len;
1781 tid_len = 0;
1785 transport_kunmap_data_sg(cmd);
1788 * Go ahead and create a registrations from tid_dest_list for the
1789 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1790 * and dest_se_deve.
1792 * The SA Reservation Key from the PROUT is set for the
1793 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1794 * means that the TransportID Initiator port will be
1795 * registered on all of the target ports in the SCSI target device
1796 * ALL_TG_PT=0 means the registration will only be for the
1797 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1798 * was received.
1800 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1801 dest_tpg = tidh->dest_tpg;
1802 dest_node_acl = tidh->dest_node_acl;
1803 dest_se_deve = tidh->dest_se_deve;
1804 dest_pr_reg = tidh->dest_pr_reg;
1806 list_del(&tidh->dest_list);
1807 kfree(tidh);
1809 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1810 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1812 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1813 dest_pr_reg, 0, 0);
1815 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1816 " registered Transport ID for Node: %s%s Mapped LUN:"
1817 " %llu\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1818 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1819 dest_se_deve->mapped_lun : 0);
1821 if (!dest_se_deve) {
1822 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1823 target_pr_kref_release);
1824 continue;
1826 core_scsi3_lunacl_undepend_item(dest_se_deve);
1827 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1828 core_scsi3_tpg_undepend_item(dest_tpg);
1831 return 0;
1832 out_unmap:
1833 transport_kunmap_data_sg(cmd);
1834 out:
1836 * For the failure case, release everything from tid_dest_list
1837 * including *dest_pr_reg and the configfs dependances..
1839 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1840 dest_tpg = tidh->dest_tpg;
1841 dest_node_acl = tidh->dest_node_acl;
1842 dest_se_deve = tidh->dest_se_deve;
1843 dest_pr_reg = tidh->dest_pr_reg;
1845 list_del(&tidh->dest_list);
1846 kfree(tidh);
1848 * Release any extra ALL_TG_PT=1 registrations for
1849 * the SPEC_I_PT=1 case.
1851 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1852 &dest_pr_reg->pr_reg_atp_list,
1853 pr_reg_atp_mem_list) {
1854 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1855 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1856 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1859 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1861 if (!dest_se_deve) {
1862 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1863 target_pr_kref_release);
1864 continue;
1866 core_scsi3_lunacl_undepend_item(dest_se_deve);
1867 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1868 core_scsi3_tpg_undepend_item(dest_tpg);
1870 return ret;
1873 static int core_scsi3_update_aptpl_buf(
1874 struct se_device *dev,
1875 unsigned char *buf,
1876 u32 pr_aptpl_buf_len)
1878 struct se_portal_group *tpg;
1879 struct t10_pr_registration *pr_reg;
1880 unsigned char tmp[512], isid_buf[32];
1881 ssize_t len = 0;
1882 int reg_count = 0;
1883 int ret = 0;
1885 spin_lock(&dev->dev_reservation_lock);
1886 spin_lock(&dev->t10_pr.registration_lock);
1888 * Walk the registration list..
1890 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1891 pr_reg_list) {
1893 tmp[0] = '\0';
1894 isid_buf[0] = '\0';
1895 tpg = pr_reg->pr_reg_nacl->se_tpg;
1897 * Write out any ISID value to APTPL metadata that was included
1898 * in the original registration.
1900 if (pr_reg->isid_present_at_reg)
1901 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1902 pr_reg->pr_reg_isid);
1904 * Include special metadata if the pr_reg matches the
1905 * reservation holder.
1907 if (dev->dev_pr_res_holder == pr_reg) {
1908 snprintf(tmp, 512, "PR_REG_START: %d"
1909 "\ninitiator_fabric=%s\n"
1910 "initiator_node=%s\n%s"
1911 "sa_res_key=%llu\n"
1912 "res_holder=1\nres_type=%02x\n"
1913 "res_scope=%02x\nres_all_tg_pt=%d\n"
1914 "mapped_lun=%llu\n", reg_count,
1915 tpg->se_tpg_tfo->get_fabric_name(),
1916 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1917 pr_reg->pr_res_key, pr_reg->pr_res_type,
1918 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1919 pr_reg->pr_res_mapped_lun);
1920 } else {
1921 snprintf(tmp, 512, "PR_REG_START: %d\n"
1922 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1923 "sa_res_key=%llu\nres_holder=0\n"
1924 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
1925 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1926 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1927 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1928 pr_reg->pr_res_mapped_lun);
1931 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1932 pr_err("Unable to update renaming APTPL metadata,"
1933 " reallocating larger buffer\n");
1934 ret = -EMSGSIZE;
1935 goto out;
1937 len += sprintf(buf+len, "%s", tmp);
1940 * Include information about the associated SCSI target port.
1942 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1943 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
1944 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1945 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1946 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1947 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1948 reg_count);
1950 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1951 pr_err("Unable to update renaming APTPL metadata,"
1952 " reallocating larger buffer\n");
1953 ret = -EMSGSIZE;
1954 goto out;
1956 len += sprintf(buf+len, "%s", tmp);
1957 reg_count++;
1960 if (!reg_count)
1961 len += sprintf(buf+len, "No Registrations or Reservations");
1963 out:
1964 spin_unlock(&dev->t10_pr.registration_lock);
1965 spin_unlock(&dev->dev_reservation_lock);
1967 return ret;
1970 static int __core_scsi3_write_aptpl_to_file(
1971 struct se_device *dev,
1972 unsigned char *buf)
1974 struct t10_wwn *wwn = &dev->t10_wwn;
1975 struct file *file;
1976 int flags = O_RDWR | O_CREAT | O_TRUNC;
1977 char path[512];
1978 u32 pr_aptpl_buf_len;
1979 int ret;
1981 memset(path, 0, 512);
1983 if (strlen(&wwn->unit_serial[0]) >= 512) {
1984 pr_err("WWN value for struct se_device does not fit"
1985 " into path buffer\n");
1986 return -EMSGSIZE;
1989 snprintf(path, 512, "%s/pr/aptpl_%s", db_root, &wwn->unit_serial[0]);
1990 file = filp_open(path, flags, 0600);
1991 if (IS_ERR(file)) {
1992 pr_err("filp_open(%s) for APTPL metadata"
1993 " failed\n", path);
1994 return PTR_ERR(file);
1997 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
1999 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
2001 if (ret < 0)
2002 pr_debug("Error writing APTPL metadata file: %s\n", path);
2003 fput(file);
2005 return (ret < 0) ? -EIO : 0;
2009 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2010 * write out the updated metadata to struct file for this SCSI device.
2012 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
2014 unsigned char *buf;
2015 int rc, len = PR_APTPL_BUF_LEN;
2017 if (!aptpl) {
2018 char *null_buf = "No Registrations or Reservations\n";
2020 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2021 dev->t10_pr.pr_aptpl_active = 0;
2022 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
2024 if (rc)
2025 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2027 return 0;
2029 retry:
2030 buf = vzalloc(len);
2031 if (!buf)
2032 return TCM_OUT_OF_RESOURCES;
2034 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
2035 if (rc < 0) {
2036 vfree(buf);
2037 len *= 2;
2038 goto retry;
2041 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2042 if (rc != 0) {
2043 pr_err("SPC-3 PR: Could not update APTPL\n");
2044 vfree(buf);
2045 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2047 dev->t10_pr.pr_aptpl_active = 1;
2048 vfree(buf);
2049 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2050 return 0;
2053 static sense_reason_t
2054 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2055 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2057 struct se_session *se_sess = cmd->se_sess;
2058 struct se_device *dev = cmd->se_dev;
2059 struct se_lun *se_lun = cmd->se_lun;
2060 struct se_portal_group *se_tpg;
2061 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2062 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2063 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2064 sense_reason_t ret = TCM_NO_SENSE;
2065 int pr_holder = 0, type;
2067 if (!se_sess || !se_lun) {
2068 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2069 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2071 se_tpg = se_sess->se_tpg;
2073 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2074 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2075 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2076 PR_REG_ISID_LEN);
2077 isid_ptr = &isid_buf[0];
2080 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2082 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2083 if (!pr_reg) {
2084 if (res_key) {
2085 pr_warn("SPC-3 PR: Reservation Key non-zero"
2086 " for SA REGISTER, returning CONFLICT\n");
2087 return TCM_RESERVATION_CONFLICT;
2090 * Do nothing but return GOOD status.
2092 if (!sa_res_key)
2093 return 0;
2095 if (!spec_i_pt) {
2097 * Perform the Service Action REGISTER on the Initiator
2098 * Port Endpoint that the PRO was received from on the
2099 * Logical Unit of the SCSI device server.
2101 if (core_scsi3_alloc_registration(cmd->se_dev,
2102 se_sess->se_node_acl, cmd->se_lun,
2103 NULL, cmd->orig_fe_lun, isid_ptr,
2104 sa_res_key, all_tg_pt, aptpl,
2105 register_type, 0)) {
2106 pr_err("Unable to allocate"
2107 " struct t10_pr_registration\n");
2108 return TCM_INVALID_PARAMETER_LIST;
2110 } else {
2112 * Register both the Initiator port that received
2113 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2114 * TransportID from Parameter list and loop through
2115 * fabric dependent parameter list while calling
2116 * logic from of core_scsi3_alloc_registration() for
2117 * each TransportID provided SCSI Initiator Port/Device
2119 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2120 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2121 if (ret != 0)
2122 return ret;
2124 return core_scsi3_update_and_write_aptpl(dev, aptpl);
2127 /* ok, existing registration */
2129 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2130 pr_err("SPC-3 PR REGISTER: Received"
2131 " res_key: 0x%016Lx does not match"
2132 " existing SA REGISTER res_key:"
2133 " 0x%016Lx\n", res_key,
2134 pr_reg->pr_res_key);
2135 ret = TCM_RESERVATION_CONFLICT;
2136 goto out;
2139 if (spec_i_pt) {
2140 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2141 " set on a registered nexus\n");
2142 ret = TCM_INVALID_PARAMETER_LIST;
2143 goto out;
2147 * An existing ALL_TG_PT=1 registration being released
2148 * must also set ALL_TG_PT=1 in the incoming PROUT.
2150 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2151 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2152 " registration exists, but ALL_TG_PT=1 bit not"
2153 " present in received PROUT\n");
2154 ret = TCM_INVALID_CDB_FIELD;
2155 goto out;
2159 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2161 if (sa_res_key) {
2163 * Increment PRgeneration counter for struct se_device"
2164 * upon a successful REGISTER, see spc4r17 section 6.3.2
2165 * READ_KEYS service action.
2167 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2168 pr_reg->pr_res_key = sa_res_key;
2169 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2170 " Key for %s to: 0x%016Lx PRgeneration:"
2171 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2172 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2173 pr_reg->pr_reg_nacl->initiatorname,
2174 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2176 } else {
2178 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2180 type = pr_reg->pr_res_type;
2181 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2182 pr_reg);
2183 if (pr_holder < 0) {
2184 ret = TCM_RESERVATION_CONFLICT;
2185 goto out;
2188 spin_lock(&pr_tmpl->registration_lock);
2190 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2191 * and matching pr_res_key.
2193 if (pr_reg->pr_reg_all_tg_pt) {
2194 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2195 &pr_tmpl->registration_list,
2196 pr_reg_list) {
2198 if (!pr_reg_p->pr_reg_all_tg_pt)
2199 continue;
2200 if (pr_reg_p->pr_res_key != res_key)
2201 continue;
2202 if (pr_reg == pr_reg_p)
2203 continue;
2204 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2205 pr_reg_p->pr_reg_nacl->initiatorname))
2206 continue;
2208 __core_scsi3_free_registration(dev,
2209 pr_reg_p, NULL, 0);
2214 * Release the calling I_T Nexus registration now..
2216 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2217 pr_reg = NULL;
2220 * From spc4r17, section 5.7.11.3 Unregistering
2222 * If the persistent reservation is a registrants only
2223 * type, the device server shall establish a unit
2224 * attention condition for the initiator port associated
2225 * with every registered I_T nexus except for the I_T
2226 * nexus on which the PERSISTENT RESERVE OUT command was
2227 * received, with the additional sense code set to
2228 * RESERVATIONS RELEASED.
2230 if (pr_holder &&
2231 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2232 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2233 list_for_each_entry(pr_reg_p,
2234 &pr_tmpl->registration_list,
2235 pr_reg_list) {
2237 target_ua_allocate_lun(
2238 pr_reg_p->pr_reg_nacl,
2239 pr_reg_p->pr_res_mapped_lun,
2240 0x2A,
2241 ASCQ_2AH_RESERVATIONS_RELEASED);
2245 spin_unlock(&pr_tmpl->registration_lock);
2248 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2250 out:
2251 if (pr_reg)
2252 core_scsi3_put_pr_reg(pr_reg);
2253 return ret;
2256 unsigned char *core_scsi3_pr_dump_type(int type)
2258 switch (type) {
2259 case PR_TYPE_WRITE_EXCLUSIVE:
2260 return "Write Exclusive Access";
2261 case PR_TYPE_EXCLUSIVE_ACCESS:
2262 return "Exclusive Access";
2263 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2264 return "Write Exclusive Access, Registrants Only";
2265 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2266 return "Exclusive Access, Registrants Only";
2267 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2268 return "Write Exclusive Access, All Registrants";
2269 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2270 return "Exclusive Access, All Registrants";
2271 default:
2272 break;
2275 return "Unknown SPC-3 PR Type";
2278 static sense_reason_t
2279 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2281 struct se_device *dev = cmd->se_dev;
2282 struct se_session *se_sess = cmd->se_sess;
2283 struct se_lun *se_lun = cmd->se_lun;
2284 struct t10_pr_registration *pr_reg, *pr_res_holder;
2285 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2286 char i_buf[PR_REG_ISID_ID_LEN];
2287 sense_reason_t ret;
2289 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2291 if (!se_sess || !se_lun) {
2292 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2293 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2296 * Locate the existing *pr_reg via struct se_node_acl pointers
2298 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2299 se_sess);
2300 if (!pr_reg) {
2301 pr_err("SPC-3 PR: Unable to locate"
2302 " PR_REGISTERED *pr_reg for RESERVE\n");
2303 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2306 * From spc4r17 Section 5.7.9: Reserving:
2308 * An application client creates a persistent reservation by issuing
2309 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2310 * a registered I_T nexus with the following parameters:
2311 * a) RESERVATION KEY set to the value of the reservation key that is
2312 * registered with the logical unit for the I_T nexus; and
2314 if (res_key != pr_reg->pr_res_key) {
2315 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2316 " does not match existing SA REGISTER res_key:"
2317 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2318 ret = TCM_RESERVATION_CONFLICT;
2319 goto out_put_pr_reg;
2322 * From spc4r17 Section 5.7.9: Reserving:
2324 * From above:
2325 * b) TYPE field and SCOPE field set to the persistent reservation
2326 * being created.
2328 * Only one persistent reservation is allowed at a time per logical unit
2329 * and that persistent reservation has a scope of LU_SCOPE.
2331 if (scope != PR_SCOPE_LU_SCOPE) {
2332 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2333 ret = TCM_INVALID_PARAMETER_LIST;
2334 goto out_put_pr_reg;
2337 * See if we have an existing PR reservation holder pointer at
2338 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2339 * *pr_res_holder.
2341 spin_lock(&dev->dev_reservation_lock);
2342 pr_res_holder = dev->dev_pr_res_holder;
2343 if (pr_res_holder) {
2345 * From spc4r17 Section 5.7.9: Reserving:
2347 * If the device server receives a PERSISTENT RESERVE OUT
2348 * command from an I_T nexus other than a persistent reservation
2349 * holder (see 5.7.10) that attempts to create a persistent
2350 * reservation when a persistent reservation already exists for
2351 * the logical unit, then the command shall be completed with
2352 * RESERVATION CONFLICT status.
2354 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2355 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2356 pr_err("SPC-3 PR: Attempted RESERVE from"
2357 " [%s]: %s while reservation already held by"
2358 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2359 cmd->se_tfo->get_fabric_name(),
2360 se_sess->se_node_acl->initiatorname,
2361 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2362 pr_res_holder->pr_reg_nacl->initiatorname);
2364 spin_unlock(&dev->dev_reservation_lock);
2365 ret = TCM_RESERVATION_CONFLICT;
2366 goto out_put_pr_reg;
2369 * From spc4r17 Section 5.7.9: Reserving:
2371 * If a persistent reservation holder attempts to modify the
2372 * type or scope of an existing persistent reservation, the
2373 * command shall be completed with RESERVATION CONFLICT status.
2375 if ((pr_res_holder->pr_res_type != type) ||
2376 (pr_res_holder->pr_res_scope != scope)) {
2377 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2378 pr_err("SPC-3 PR: Attempted RESERVE from"
2379 " [%s]: %s trying to change TYPE and/or SCOPE,"
2380 " while reservation already held by [%s]: %s,"
2381 " returning RESERVATION_CONFLICT\n",
2382 cmd->se_tfo->get_fabric_name(),
2383 se_sess->se_node_acl->initiatorname,
2384 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2385 pr_res_holder->pr_reg_nacl->initiatorname);
2387 spin_unlock(&dev->dev_reservation_lock);
2388 ret = TCM_RESERVATION_CONFLICT;
2389 goto out_put_pr_reg;
2392 * From spc4r17 Section 5.7.9: Reserving:
2394 * If the device server receives a PERSISTENT RESERVE OUT
2395 * command with RESERVE service action where the TYPE field and
2396 * the SCOPE field contain the same values as the existing type
2397 * and scope from a persistent reservation holder, it shall not
2398 * make any change to the existing persistent reservation and
2399 * shall completethe command with GOOD status.
2401 spin_unlock(&dev->dev_reservation_lock);
2402 ret = 0;
2403 goto out_put_pr_reg;
2406 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2407 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2409 pr_reg->pr_res_scope = scope;
2410 pr_reg->pr_res_type = type;
2411 pr_reg->pr_res_holder = 1;
2412 dev->dev_pr_res_holder = pr_reg;
2413 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2415 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2416 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2417 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2418 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2419 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2420 cmd->se_tfo->get_fabric_name(),
2421 se_sess->se_node_acl->initiatorname,
2422 i_buf);
2423 spin_unlock(&dev->dev_reservation_lock);
2425 if (pr_tmpl->pr_aptpl_active)
2426 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2428 ret = 0;
2429 out_put_pr_reg:
2430 core_scsi3_put_pr_reg(pr_reg);
2431 return ret;
2434 static sense_reason_t
2435 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2436 u64 res_key)
2438 switch (type) {
2439 case PR_TYPE_WRITE_EXCLUSIVE:
2440 case PR_TYPE_EXCLUSIVE_ACCESS:
2441 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2442 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2443 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2444 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2445 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2446 default:
2447 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2448 " 0x%02x\n", type);
2449 return TCM_INVALID_CDB_FIELD;
2454 * Called with struct se_device->dev_reservation_lock held.
2456 static void __core_scsi3_complete_pro_release(
2457 struct se_device *dev,
2458 struct se_node_acl *se_nacl,
2459 struct t10_pr_registration *pr_reg,
2460 int explicit,
2461 int unreg)
2463 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2464 char i_buf[PR_REG_ISID_ID_LEN];
2465 int pr_res_type = 0, pr_res_scope = 0;
2467 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2468 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2470 * Go ahead and release the current PR reservation holder.
2471 * If an All Registrants reservation is currently active and
2472 * a unregister operation is requested, replace the current
2473 * dev_pr_res_holder with another active registration.
2475 if (dev->dev_pr_res_holder) {
2476 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2477 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2478 dev->dev_pr_res_holder->pr_res_type = 0;
2479 dev->dev_pr_res_holder->pr_res_scope = 0;
2480 dev->dev_pr_res_holder->pr_res_holder = 0;
2481 dev->dev_pr_res_holder = NULL;
2483 if (!unreg)
2484 goto out;
2486 spin_lock(&dev->t10_pr.registration_lock);
2487 list_del_init(&pr_reg->pr_reg_list);
2489 * If the I_T nexus is a reservation holder, the persistent reservation
2490 * is of an all registrants type, and the I_T nexus is the last remaining
2491 * registered I_T nexus, then the device server shall also release the
2492 * persistent reservation.
2494 if (!list_empty(&dev->t10_pr.registration_list) &&
2495 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2496 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2497 dev->dev_pr_res_holder =
2498 list_entry(dev->t10_pr.registration_list.next,
2499 struct t10_pr_registration, pr_reg_list);
2500 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2501 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2502 dev->dev_pr_res_holder->pr_res_holder = 1;
2504 spin_unlock(&dev->t10_pr.registration_lock);
2505 out:
2506 if (!dev->dev_pr_res_holder) {
2507 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2508 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2509 tfo->get_fabric_name(), (explicit) ? "explicit" :
2510 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2511 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2513 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2514 tfo->get_fabric_name(), se_nacl->initiatorname,
2515 i_buf);
2517 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2519 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2522 static sense_reason_t
2523 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2524 u64 res_key)
2526 struct se_device *dev = cmd->se_dev;
2527 struct se_session *se_sess = cmd->se_sess;
2528 struct se_lun *se_lun = cmd->se_lun;
2529 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2530 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2531 sense_reason_t ret = 0;
2533 if (!se_sess || !se_lun) {
2534 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2535 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2538 * Locate the existing *pr_reg via struct se_node_acl pointers
2540 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2541 if (!pr_reg) {
2542 pr_err("SPC-3 PR: Unable to locate"
2543 " PR_REGISTERED *pr_reg for RELEASE\n");
2544 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2547 * From spc4r17 Section 5.7.11.2 Releasing:
2549 * If there is no persistent reservation or in response to a persistent
2550 * reservation release request from a registered I_T nexus that is not a
2551 * persistent reservation holder (see 5.7.10), the device server shall
2552 * do the following:
2554 * a) Not release the persistent reservation, if any;
2555 * b) Not remove any registrations; and
2556 * c) Complete the command with GOOD status.
2558 spin_lock(&dev->dev_reservation_lock);
2559 pr_res_holder = dev->dev_pr_res_holder;
2560 if (!pr_res_holder) {
2562 * No persistent reservation, return GOOD status.
2564 spin_unlock(&dev->dev_reservation_lock);
2565 goto out_put_pr_reg;
2568 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2570 * Release request from a registered I_T nexus that is not a
2571 * persistent reservation holder. return GOOD status.
2573 spin_unlock(&dev->dev_reservation_lock);
2574 goto out_put_pr_reg;
2578 * From spc4r17 Section 5.7.11.2 Releasing:
2580 * Only the persistent reservation holder (see 5.7.10) is allowed to
2581 * release a persistent reservation.
2583 * An application client releases the persistent reservation by issuing
2584 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2585 * an I_T nexus that is a persistent reservation holder with the
2586 * following parameters:
2588 * a) RESERVATION KEY field set to the value of the reservation key
2589 * that is registered with the logical unit for the I_T nexus;
2591 if (res_key != pr_reg->pr_res_key) {
2592 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2593 " does not match existing SA REGISTER res_key:"
2594 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2595 spin_unlock(&dev->dev_reservation_lock);
2596 ret = TCM_RESERVATION_CONFLICT;
2597 goto out_put_pr_reg;
2600 * From spc4r17 Section 5.7.11.2 Releasing and above:
2602 * b) TYPE field and SCOPE field set to match the persistent
2603 * reservation being released.
2605 if ((pr_res_holder->pr_res_type != type) ||
2606 (pr_res_holder->pr_res_scope != scope)) {
2607 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2608 pr_err("SPC-3 PR RELEASE: Attempted to release"
2609 " reservation from [%s]: %s with different TYPE "
2610 "and/or SCOPE while reservation already held by"
2611 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2612 cmd->se_tfo->get_fabric_name(),
2613 se_sess->se_node_acl->initiatorname,
2614 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2615 pr_res_holder->pr_reg_nacl->initiatorname);
2617 spin_unlock(&dev->dev_reservation_lock);
2618 ret = TCM_RESERVATION_CONFLICT;
2619 goto out_put_pr_reg;
2622 * In response to a persistent reservation release request from the
2623 * persistent reservation holder the device server shall perform a
2624 * release by doing the following as an uninterrupted series of actions:
2625 * a) Release the persistent reservation;
2626 * b) Not remove any registration(s);
2627 * c) If the released persistent reservation is a registrants only type
2628 * or all registrants type persistent reservation,
2629 * the device server shall establish a unit attention condition for
2630 * the initiator port associated with every regis-
2631 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2632 * RESERVE OUT command with RELEASE service action was received,
2633 * with the additional sense code set to RESERVATIONS RELEASED; and
2634 * d) If the persistent reservation is of any other type, the device
2635 * server shall not establish a unit attention condition.
2637 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2638 pr_reg, 1, 0);
2640 spin_unlock(&dev->dev_reservation_lock);
2642 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2643 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2644 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2645 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2647 * If no UNIT ATTENTION conditions will be established for
2648 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2649 * go ahead and check for APTPL=1 update+write below
2651 goto write_aptpl;
2654 spin_lock(&pr_tmpl->registration_lock);
2655 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2656 pr_reg_list) {
2658 * Do not establish a UNIT ATTENTION condition
2659 * for the calling I_T Nexus
2661 if (pr_reg_p == pr_reg)
2662 continue;
2664 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2665 pr_reg_p->pr_res_mapped_lun,
2666 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2668 spin_unlock(&pr_tmpl->registration_lock);
2670 write_aptpl:
2671 if (pr_tmpl->pr_aptpl_active)
2672 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2674 out_put_pr_reg:
2675 core_scsi3_put_pr_reg(pr_reg);
2676 return ret;
2679 static sense_reason_t
2680 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2682 struct se_device *dev = cmd->se_dev;
2683 struct se_node_acl *pr_reg_nacl;
2684 struct se_session *se_sess = cmd->se_sess;
2685 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2686 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2687 u64 pr_res_mapped_lun = 0;
2688 int calling_it_nexus = 0;
2690 * Locate the existing *pr_reg via struct se_node_acl pointers
2692 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2693 se_sess->se_node_acl, se_sess);
2694 if (!pr_reg_n) {
2695 pr_err("SPC-3 PR: Unable to locate"
2696 " PR_REGISTERED *pr_reg for CLEAR\n");
2697 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2700 * From spc4r17 section 5.7.11.6, Clearing:
2702 * Any application client may release the persistent reservation and
2703 * remove all registrations from a device server by issuing a
2704 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2705 * registered I_T nexus with the following parameter:
2707 * a) RESERVATION KEY field set to the value of the reservation key
2708 * that is registered with the logical unit for the I_T nexus.
2710 if (res_key != pr_reg_n->pr_res_key) {
2711 pr_err("SPC-3 PR REGISTER: Received"
2712 " res_key: 0x%016Lx does not match"
2713 " existing SA REGISTER res_key:"
2714 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2715 core_scsi3_put_pr_reg(pr_reg_n);
2716 return TCM_RESERVATION_CONFLICT;
2719 * a) Release the persistent reservation, if any;
2721 spin_lock(&dev->dev_reservation_lock);
2722 pr_res_holder = dev->dev_pr_res_holder;
2723 if (pr_res_holder) {
2724 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2725 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2726 pr_res_holder, 0, 0);
2728 spin_unlock(&dev->dev_reservation_lock);
2730 * b) Remove all registration(s) (see spc4r17 5.7.7);
2732 spin_lock(&pr_tmpl->registration_lock);
2733 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2734 &pr_tmpl->registration_list, pr_reg_list) {
2736 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2737 pr_reg_nacl = pr_reg->pr_reg_nacl;
2738 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2739 __core_scsi3_free_registration(dev, pr_reg, NULL,
2740 calling_it_nexus);
2742 * e) Establish a unit attention condition for the initiator
2743 * port associated with every registered I_T nexus other
2744 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2745 * command with CLEAR service action was received, with the
2746 * additional sense code set to RESERVATIONS PREEMPTED.
2748 if (!calling_it_nexus)
2749 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2750 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2752 spin_unlock(&pr_tmpl->registration_lock);
2754 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2755 cmd->se_tfo->get_fabric_name());
2757 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2759 core_scsi3_pr_generation(dev);
2760 return 0;
2764 * Called with struct se_device->dev_reservation_lock held.
2766 static void __core_scsi3_complete_pro_preempt(
2767 struct se_device *dev,
2768 struct t10_pr_registration *pr_reg,
2769 struct list_head *preempt_and_abort_list,
2770 int type,
2771 int scope,
2772 enum preempt_type preempt_type)
2774 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2775 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2776 char i_buf[PR_REG_ISID_ID_LEN];
2778 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2779 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2781 * Do an implicit RELEASE of the existing reservation.
2783 if (dev->dev_pr_res_holder)
2784 __core_scsi3_complete_pro_release(dev, nacl,
2785 dev->dev_pr_res_holder, 0, 0);
2787 dev->dev_pr_res_holder = pr_reg;
2788 pr_reg->pr_res_holder = 1;
2789 pr_reg->pr_res_type = type;
2790 pr_reg->pr_res_scope = scope;
2792 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2793 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2794 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2795 core_scsi3_pr_dump_type(type),
2796 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2797 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2798 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2799 nacl->initiatorname, i_buf);
2801 * For PREEMPT_AND_ABORT, add the preempting reservation's
2802 * struct t10_pr_registration to the list that will be compared
2803 * against received CDBs..
2805 if (preempt_and_abort_list)
2806 list_add_tail(&pr_reg->pr_reg_abort_list,
2807 preempt_and_abort_list);
2810 static void core_scsi3_release_preempt_and_abort(
2811 struct list_head *preempt_and_abort_list,
2812 struct t10_pr_registration *pr_reg_holder)
2814 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2816 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2817 pr_reg_abort_list) {
2819 list_del(&pr_reg->pr_reg_abort_list);
2820 if (pr_reg_holder == pr_reg)
2821 continue;
2822 if (pr_reg->pr_res_holder) {
2823 pr_warn("pr_reg->pr_res_holder still set\n");
2824 continue;
2827 pr_reg->pr_reg_deve = NULL;
2828 pr_reg->pr_reg_nacl = NULL;
2829 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2833 static sense_reason_t
2834 core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2835 u64 sa_res_key, enum preempt_type preempt_type)
2837 struct se_device *dev = cmd->se_dev;
2838 struct se_node_acl *pr_reg_nacl;
2839 struct se_session *se_sess = cmd->se_sess;
2840 LIST_HEAD(preempt_and_abort_list);
2841 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2842 struct t10_reservation *pr_tmpl = &dev->t10_pr;
2843 u64 pr_res_mapped_lun = 0;
2844 int all_reg = 0, calling_it_nexus = 0;
2845 bool sa_res_key_unmatched = sa_res_key != 0;
2846 int prh_type = 0, prh_scope = 0;
2848 if (!se_sess)
2849 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2851 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2852 se_sess);
2853 if (!pr_reg_n) {
2854 pr_err("SPC-3 PR: Unable to locate"
2855 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2856 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2857 return TCM_RESERVATION_CONFLICT;
2859 if (pr_reg_n->pr_res_key != res_key) {
2860 core_scsi3_put_pr_reg(pr_reg_n);
2861 return TCM_RESERVATION_CONFLICT;
2863 if (scope != PR_SCOPE_LU_SCOPE) {
2864 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2865 core_scsi3_put_pr_reg(pr_reg_n);
2866 return TCM_INVALID_PARAMETER_LIST;
2869 spin_lock(&dev->dev_reservation_lock);
2870 pr_res_holder = dev->dev_pr_res_holder;
2871 if (pr_res_holder &&
2872 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2873 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2874 all_reg = 1;
2876 if (!all_reg && !sa_res_key) {
2877 spin_unlock(&dev->dev_reservation_lock);
2878 core_scsi3_put_pr_reg(pr_reg_n);
2879 return TCM_INVALID_PARAMETER_LIST;
2882 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2884 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2885 * persistent reservation holder or there is no persistent reservation
2886 * holder (i.e., there is no persistent reservation), then the device
2887 * server shall perform a preempt by doing the following in an
2888 * uninterrupted series of actions. (See below..)
2890 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2892 * No existing or SA Reservation Key matching reservations..
2894 * PROUT SA PREEMPT with All Registrant type reservations are
2895 * allowed to be processed without a matching SA Reservation Key
2897 spin_lock(&pr_tmpl->registration_lock);
2898 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2899 &pr_tmpl->registration_list, pr_reg_list) {
2901 * Removing of registrations in non all registrants
2902 * type reservations without a matching SA reservation
2903 * key.
2905 * a) Remove the registrations for all I_T nexuses
2906 * specified by the SERVICE ACTION RESERVATION KEY
2907 * field;
2908 * b) Ignore the contents of the SCOPE and TYPE fields;
2909 * c) Process tasks as defined in 5.7.1; and
2910 * d) Establish a unit attention condition for the
2911 * initiator port associated with every I_T nexus
2912 * that lost its registration other than the I_T
2913 * nexus on which the PERSISTENT RESERVE OUT command
2914 * was received, with the additional sense code set
2915 * to REGISTRATIONS PREEMPTED.
2917 if (!all_reg) {
2918 if (pr_reg->pr_res_key != sa_res_key)
2919 continue;
2920 sa_res_key_unmatched = false;
2922 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2923 pr_reg_nacl = pr_reg->pr_reg_nacl;
2924 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2925 __core_scsi3_free_registration(dev, pr_reg,
2926 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2927 NULL, calling_it_nexus);
2928 } else {
2930 * Case for any existing all registrants type
2931 * reservation, follow logic in spc4r17 section
2932 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2934 * For a ZERO SA Reservation key, release
2935 * all other registrations and do an implicit
2936 * release of active persistent reservation.
2938 * For a non-ZERO SA Reservation key, only
2939 * release the matching reservation key from
2940 * registrations.
2942 if ((sa_res_key) &&
2943 (pr_reg->pr_res_key != sa_res_key))
2944 continue;
2945 sa_res_key_unmatched = false;
2947 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2948 if (calling_it_nexus)
2949 continue;
2951 pr_reg_nacl = pr_reg->pr_reg_nacl;
2952 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2953 __core_scsi3_free_registration(dev, pr_reg,
2954 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2955 NULL, 0);
2957 if (!calling_it_nexus)
2958 target_ua_allocate_lun(pr_reg_nacl,
2959 pr_res_mapped_lun, 0x2A,
2960 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2962 spin_unlock(&pr_tmpl->registration_lock);
2964 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2965 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2966 * RESERVATION KEY field to a value that does not match any
2967 * registered reservation key, then the device server shall
2968 * complete the command with RESERVATION CONFLICT status.
2970 if (sa_res_key_unmatched) {
2971 spin_unlock(&dev->dev_reservation_lock);
2972 core_scsi3_put_pr_reg(pr_reg_n);
2973 return TCM_RESERVATION_CONFLICT;
2976 * For an existing all registrants type reservation
2977 * with a zero SA rservation key, preempt the existing
2978 * reservation with the new PR type and scope.
2980 if (pr_res_holder && all_reg && !(sa_res_key)) {
2981 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
2982 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2983 type, scope, preempt_type);
2985 if (preempt_type == PREEMPT_AND_ABORT)
2986 core_scsi3_release_preempt_and_abort(
2987 &preempt_and_abort_list, pr_reg_n);
2989 spin_unlock(&dev->dev_reservation_lock);
2991 if (pr_tmpl->pr_aptpl_active)
2992 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2994 core_scsi3_put_pr_reg(pr_reg_n);
2995 core_scsi3_pr_generation(cmd->se_dev);
2996 return 0;
2999 * The PREEMPTing SA reservation key matches that of the
3000 * existing persistent reservation, first, we check if
3001 * we are preempting our own reservation.
3002 * From spc4r17, section 5.7.11.4.3 Preempting
3003 * persistent reservations and registration handling
3005 * If an all registrants persistent reservation is not
3006 * present, it is not an error for the persistent
3007 * reservation holder to preempt itself (i.e., a
3008 * PERSISTENT RESERVE OUT with a PREEMPT service action
3009 * or a PREEMPT AND ABORT service action with the
3010 * SERVICE ACTION RESERVATION KEY value equal to the
3011 * persistent reservation holder's reservation key that
3012 * is received from the persistent reservation holder).
3013 * In that case, the device server shall establish the
3014 * new persistent reservation and maintain the
3015 * registration.
3017 prh_type = pr_res_holder->pr_res_type;
3018 prh_scope = pr_res_holder->pr_res_scope;
3020 * If the SERVICE ACTION RESERVATION KEY field identifies a
3021 * persistent reservation holder (see 5.7.10), the device
3022 * server shall perform a preempt by doing the following as
3023 * an uninterrupted series of actions:
3025 * a) Release the persistent reservation for the holder
3026 * identified by the SERVICE ACTION RESERVATION KEY field;
3028 if (pr_reg_n != pr_res_holder)
3029 __core_scsi3_complete_pro_release(dev,
3030 pr_res_holder->pr_reg_nacl,
3031 dev->dev_pr_res_holder, 0, 0);
3033 * b) Remove the registrations for all I_T nexuses identified
3034 * by the SERVICE ACTION RESERVATION KEY field, except the
3035 * I_T nexus that is being used for the PERSISTENT RESERVE
3036 * OUT command. If an all registrants persistent reservation
3037 * is present and the SERVICE ACTION RESERVATION KEY field
3038 * is set to zero, then all registrations shall be removed
3039 * except for that of the I_T nexus that is being used for
3040 * the PERSISTENT RESERVE OUT command;
3042 spin_lock(&pr_tmpl->registration_lock);
3043 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3044 &pr_tmpl->registration_list, pr_reg_list) {
3046 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3047 if (calling_it_nexus)
3048 continue;
3050 if (pr_reg->pr_res_key != sa_res_key)
3051 continue;
3053 pr_reg_nacl = pr_reg->pr_reg_nacl;
3054 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3055 __core_scsi3_free_registration(dev, pr_reg,
3056 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3057 calling_it_nexus);
3059 * e) Establish a unit attention condition for the initiator
3060 * port associated with every I_T nexus that lost its
3061 * persistent reservation and/or registration, with the
3062 * additional sense code set to REGISTRATIONS PREEMPTED;
3064 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3065 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3067 spin_unlock(&pr_tmpl->registration_lock);
3069 * c) Establish a persistent reservation for the preempting
3070 * I_T nexus using the contents of the SCOPE and TYPE fields;
3072 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3073 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3074 type, scope, preempt_type);
3076 * d) Process tasks as defined in 5.7.1;
3077 * e) See above..
3078 * f) If the type or scope has changed, then for every I_T nexus
3079 * whose reservation key was not removed, except for the I_T
3080 * nexus on which the PERSISTENT RESERVE OUT command was
3081 * received, the device server shall establish a unit
3082 * attention condition for the initiator port associated with
3083 * that I_T nexus, with the additional sense code set to
3084 * RESERVATIONS RELEASED. If the type or scope have not
3085 * changed, then no unit attention condition(s) shall be
3086 * established for this reason.
3088 if ((prh_type != type) || (prh_scope != scope)) {
3089 spin_lock(&pr_tmpl->registration_lock);
3090 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3091 &pr_tmpl->registration_list, pr_reg_list) {
3093 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3094 if (calling_it_nexus)
3095 continue;
3097 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3098 pr_reg->pr_res_mapped_lun, 0x2A,
3099 ASCQ_2AH_RESERVATIONS_RELEASED);
3101 spin_unlock(&pr_tmpl->registration_lock);
3103 spin_unlock(&dev->dev_reservation_lock);
3105 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3106 * All received CDBs for the matching existing reservation and
3107 * registrations undergo ABORT_TASK logic.
3109 * From there, core_scsi3_release_preempt_and_abort() will
3110 * release every registration in the list (which have already
3111 * been removed from the primary pr_reg list), except the
3112 * new persistent reservation holder, the calling Initiator Port.
3114 if (preempt_type == PREEMPT_AND_ABORT) {
3115 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3116 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3117 pr_reg_n);
3120 if (pr_tmpl->pr_aptpl_active)
3121 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3123 core_scsi3_put_pr_reg(pr_reg_n);
3124 core_scsi3_pr_generation(cmd->se_dev);
3125 return 0;
3128 static sense_reason_t
3129 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3130 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3132 switch (type) {
3133 case PR_TYPE_WRITE_EXCLUSIVE:
3134 case PR_TYPE_EXCLUSIVE_ACCESS:
3135 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3136 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3137 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3138 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3139 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3140 sa_res_key, preempt_type);
3141 default:
3142 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3143 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3144 return TCM_INVALID_CDB_FIELD;
3149 static sense_reason_t
3150 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3151 u64 sa_res_key, int aptpl, int unreg)
3153 struct se_session *se_sess = cmd->se_sess;
3154 struct se_device *dev = cmd->se_dev;
3155 struct se_dev_entry *dest_se_deve = NULL;
3156 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3157 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3158 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3159 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3160 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3161 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3162 unsigned char *buf;
3163 const unsigned char *initiator_str;
3164 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
3165 u32 tid_len, tmp_tid_len;
3166 int new_reg = 0, type, scope, matching_iname;
3167 sense_reason_t ret;
3168 unsigned short rtpi;
3169 unsigned char proto_ident;
3171 if (!se_sess || !se_lun) {
3172 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3173 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3176 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3177 se_tpg = se_sess->se_tpg;
3178 tf_ops = se_tpg->se_tpg_tfo;
3180 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3181 * Register behaviors for a REGISTER AND MOVE service action
3183 * Locate the existing *pr_reg via struct se_node_acl pointers
3185 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3186 se_sess);
3187 if (!pr_reg) {
3188 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3189 " *pr_reg for REGISTER_AND_MOVE\n");
3190 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3193 * The provided reservation key much match the existing reservation key
3194 * provided during this initiator's I_T nexus registration.
3196 if (res_key != pr_reg->pr_res_key) {
3197 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3198 " res_key: 0x%016Lx does not match existing SA REGISTER"
3199 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3200 ret = TCM_RESERVATION_CONFLICT;
3201 goto out_put_pr_reg;
3204 * The service active reservation key needs to be non zero
3206 if (!sa_res_key) {
3207 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3208 " sa_res_key\n");
3209 ret = TCM_INVALID_PARAMETER_LIST;
3210 goto out_put_pr_reg;
3214 * Determine the Relative Target Port Identifier where the reservation
3215 * will be moved to for the TransportID containing SCSI initiator WWN
3216 * information.
3218 buf = transport_kmap_data_sg(cmd);
3219 if (!buf) {
3220 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3221 goto out_put_pr_reg;
3224 rtpi = (buf[18] & 0xff) << 8;
3225 rtpi |= buf[19] & 0xff;
3226 tid_len = (buf[20] & 0xff) << 24;
3227 tid_len |= (buf[21] & 0xff) << 16;
3228 tid_len |= (buf[22] & 0xff) << 8;
3229 tid_len |= buf[23] & 0xff;
3230 transport_kunmap_data_sg(cmd);
3231 buf = NULL;
3233 if ((tid_len + 24) != cmd->data_length) {
3234 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3235 " does not equal CDB data_length: %u\n", tid_len,
3236 cmd->data_length);
3237 ret = TCM_INVALID_PARAMETER_LIST;
3238 goto out_put_pr_reg;
3241 spin_lock(&dev->se_port_lock);
3242 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3243 if (tmp_lun->lun_rtpi != rtpi)
3244 continue;
3245 dest_se_tpg = tmp_lun->lun_tpg;
3246 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3247 if (!dest_tf_ops)
3248 continue;
3250 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3251 spin_unlock(&dev->se_port_lock);
3253 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3254 pr_err("core_scsi3_tpg_depend_item() failed"
3255 " for dest_se_tpg\n");
3256 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3257 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3258 goto out_put_pr_reg;
3261 spin_lock(&dev->se_port_lock);
3262 break;
3264 spin_unlock(&dev->se_port_lock);
3266 if (!dest_se_tpg || !dest_tf_ops) {
3267 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3268 " fabric ops from Relative Target Port Identifier:"
3269 " %hu\n", rtpi);
3270 ret = TCM_INVALID_PARAMETER_LIST;
3271 goto out_put_pr_reg;
3274 buf = transport_kmap_data_sg(cmd);
3275 if (!buf) {
3276 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3277 goto out_put_pr_reg;
3279 proto_ident = (buf[24] & 0x0f);
3281 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3282 " 0x%02x\n", proto_ident);
3284 if (proto_ident != dest_se_tpg->proto_id) {
3285 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3286 " proto_ident: 0x%02x does not match ident: 0x%02x"
3287 " from fabric: %s\n", proto_ident,
3288 dest_se_tpg->proto_id,
3289 dest_tf_ops->get_fabric_name());
3290 ret = TCM_INVALID_PARAMETER_LIST;
3291 goto out;
3293 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
3294 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3295 if (!initiator_str) {
3296 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3297 " initiator_str from Transport ID\n");
3298 ret = TCM_INVALID_PARAMETER_LIST;
3299 goto out;
3302 transport_kunmap_data_sg(cmd);
3303 buf = NULL;
3305 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3306 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3307 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3308 iport_ptr : "");
3310 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3311 * action specifies a TransportID that is the same as the initiator port
3312 * of the I_T nexus for the command received, then the command shall
3313 * be terminated with CHECK CONDITION status, with the sense key set to
3314 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3315 * IN PARAMETER LIST.
3317 pr_reg_nacl = pr_reg->pr_reg_nacl;
3318 matching_iname = (!strcmp(initiator_str,
3319 pr_reg_nacl->initiatorname)) ? 1 : 0;
3320 if (!matching_iname)
3321 goto after_iport_check;
3323 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3324 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3325 " matches: %s on received I_T Nexus\n", initiator_str,
3326 pr_reg_nacl->initiatorname);
3327 ret = TCM_INVALID_PARAMETER_LIST;
3328 goto out;
3330 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3331 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3332 " matches: %s %s on received I_T Nexus\n",
3333 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3334 pr_reg->pr_reg_isid);
3335 ret = TCM_INVALID_PARAMETER_LIST;
3336 goto out;
3338 after_iport_check:
3340 * Locate the destination struct se_node_acl from the received Transport ID
3342 mutex_lock(&dest_se_tpg->acl_node_mutex);
3343 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3344 initiator_str);
3345 if (dest_node_acl)
3346 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3347 mutex_unlock(&dest_se_tpg->acl_node_mutex);
3349 if (!dest_node_acl) {
3350 pr_err("Unable to locate %s dest_node_acl for"
3351 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3352 initiator_str);
3353 ret = TCM_INVALID_PARAMETER_LIST;
3354 goto out;
3357 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3358 pr_err("core_scsi3_nodeacl_depend_item() for"
3359 " dest_node_acl\n");
3360 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3361 dest_node_acl = NULL;
3362 ret = TCM_INVALID_PARAMETER_LIST;
3363 goto out;
3366 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3367 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3368 dest_node_acl->initiatorname);
3371 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3372 * PORT IDENTIFIER.
3374 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3375 if (!dest_se_deve) {
3376 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3377 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
3378 ret = TCM_INVALID_PARAMETER_LIST;
3379 goto out;
3382 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3383 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3384 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3385 dest_se_deve = NULL;
3386 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3387 goto out;
3390 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3391 " ACL for dest_se_deve->mapped_lun: %llu\n",
3392 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3393 dest_se_deve->mapped_lun);
3396 * A persistent reservation needs to already existing in order to
3397 * successfully complete the REGISTER_AND_MOVE service action..
3399 spin_lock(&dev->dev_reservation_lock);
3400 pr_res_holder = dev->dev_pr_res_holder;
3401 if (!pr_res_holder) {
3402 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3403 " currently held\n");
3404 spin_unlock(&dev->dev_reservation_lock);
3405 ret = TCM_INVALID_CDB_FIELD;
3406 goto out;
3409 * The received on I_T Nexus must be the reservation holder.
3411 * From spc4r17 section 5.7.8 Table 50 --
3412 * Register behaviors for a REGISTER AND MOVE service action
3414 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3415 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3416 " Nexus is not reservation holder\n");
3417 spin_unlock(&dev->dev_reservation_lock);
3418 ret = TCM_RESERVATION_CONFLICT;
3419 goto out;
3422 * From spc4r17 section 5.7.8: registering and moving reservation
3424 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3425 * action is received and the established persistent reservation is a
3426 * Write Exclusive - All Registrants type or Exclusive Access -
3427 * All Registrants type reservation, then the command shall be completed
3428 * with RESERVATION CONFLICT status.
3430 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3431 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3432 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3433 " reservation for type: %s\n",
3434 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3435 spin_unlock(&dev->dev_reservation_lock);
3436 ret = TCM_RESERVATION_CONFLICT;
3437 goto out;
3439 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3441 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3443 type = pr_res_holder->pr_res_type;
3444 scope = pr_res_holder->pr_res_type;
3446 * c) Associate the reservation key specified in the SERVICE ACTION
3447 * RESERVATION KEY field with the I_T nexus specified as the
3448 * destination of the register and move, where:
3449 * A) The I_T nexus is specified by the TransportID and the
3450 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3451 * B) Regardless of the TransportID format used, the association for
3452 * the initiator port is based on either the initiator port name
3453 * (see 3.1.71) on SCSI transport protocols where port names are
3454 * required or the initiator port identifier (see 3.1.70) on SCSI
3455 * transport protocols where port names are not required;
3456 * d) Register the reservation key specified in the SERVICE ACTION
3457 * RESERVATION KEY field;
3458 * e) Retain the reservation key specified in the SERVICE ACTION
3459 * RESERVATION KEY field and associated information;
3461 * Also, It is not an error for a REGISTER AND MOVE service action to
3462 * register an I_T nexus that is already registered with the same
3463 * reservation key or a different reservation key.
3465 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3466 iport_ptr);
3467 if (!dest_pr_reg) {
3468 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
3469 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
3471 spin_unlock(&dev->dev_reservation_lock);
3472 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3473 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3474 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
3475 ret = TCM_INVALID_PARAMETER_LIST;
3476 goto out;
3478 spin_lock(&dev->dev_reservation_lock);
3479 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3480 iport_ptr);
3481 new_reg = 1;
3484 * f) Release the persistent reservation for the persistent reservation
3485 * holder (i.e., the I_T nexus on which the
3487 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3488 dev->dev_pr_res_holder, 0, 0);
3490 * g) Move the persistent reservation to the specified I_T nexus using
3491 * the same scope and type as the persistent reservation released in
3492 * item f); and
3494 dev->dev_pr_res_holder = dest_pr_reg;
3495 dest_pr_reg->pr_res_holder = 1;
3496 dest_pr_reg->pr_res_type = type;
3497 pr_reg->pr_res_scope = scope;
3498 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3500 * Increment PRGeneration for existing registrations..
3502 if (!new_reg)
3503 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3504 spin_unlock(&dev->dev_reservation_lock);
3506 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3507 " created new reservation holder TYPE: %s on object RTPI:"
3508 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3509 core_scsi3_pr_dump_type(type), rtpi,
3510 dest_pr_reg->pr_res_generation);
3511 pr_debug("SPC-3 PR Successfully moved reservation from"
3512 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3513 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3514 i_buf, dest_tf_ops->get_fabric_name(),
3515 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3516 iport_ptr : "");
3518 * It is now safe to release configfs group dependencies for destination
3519 * of Transport ID Initiator Device/Port Identifier
3521 core_scsi3_lunacl_undepend_item(dest_se_deve);
3522 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3523 core_scsi3_tpg_undepend_item(dest_se_tpg);
3525 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3526 * nexus on which PERSISTENT RESERVE OUT command was received.
3528 if (unreg) {
3529 spin_lock(&pr_tmpl->registration_lock);
3530 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3531 spin_unlock(&pr_tmpl->registration_lock);
3532 } else
3533 core_scsi3_put_pr_reg(pr_reg);
3535 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3537 transport_kunmap_data_sg(cmd);
3539 core_scsi3_put_pr_reg(dest_pr_reg);
3540 return 0;
3541 out:
3542 if (buf)
3543 transport_kunmap_data_sg(cmd);
3544 if (dest_se_deve)
3545 core_scsi3_lunacl_undepend_item(dest_se_deve);
3546 if (dest_node_acl)
3547 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3548 core_scsi3_tpg_undepend_item(dest_se_tpg);
3550 out_put_pr_reg:
3551 core_scsi3_put_pr_reg(pr_reg);
3552 return ret;
3555 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3557 unsigned int __v1, __v2;
3559 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3560 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3562 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3566 * See spc4r17 section 6.14 Table 170
3568 sense_reason_t
3569 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3571 struct se_device *dev = cmd->se_dev;
3572 unsigned char *cdb = &cmd->t_task_cdb[0];
3573 unsigned char *buf;
3574 u64 res_key, sa_res_key;
3575 int sa, scope, type, aptpl;
3576 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3577 sense_reason_t ret;
3580 * Following spc2r20 5.5.1 Reservations overview:
3582 * If a logical unit has been reserved by any RESERVE command and is
3583 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3584 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3585 * initiator or service action and shall terminate with a RESERVATION
3586 * CONFLICT status.
3588 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3589 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3590 " SPC-2 reservation is held, returning"
3591 " RESERVATION_CONFLICT\n");
3592 return TCM_RESERVATION_CONFLICT;
3596 * FIXME: A NULL struct se_session pointer means an this is not coming from
3597 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3599 if (!cmd->se_sess)
3600 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3602 if (cmd->data_length < 24) {
3603 pr_warn("SPC-PR: Received PR OUT parameter list"
3604 " length too small: %u\n", cmd->data_length);
3605 return TCM_INVALID_PARAMETER_LIST;
3609 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3611 sa = (cdb[1] & 0x1f);
3612 scope = (cdb[2] & 0xf0);
3613 type = (cdb[2] & 0x0f);
3615 buf = transport_kmap_data_sg(cmd);
3616 if (!buf)
3617 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3620 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3622 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3623 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3625 * REGISTER_AND_MOVE uses a different SA parameter list containing
3626 * SCSI TransportIDs.
3628 if (sa != PRO_REGISTER_AND_MOVE) {
3629 spec_i_pt = (buf[20] & 0x08);
3630 all_tg_pt = (buf[20] & 0x04);
3631 aptpl = (buf[20] & 0x01);
3632 } else {
3633 aptpl = (buf[17] & 0x01);
3634 unreg = (buf[17] & 0x02);
3637 * If the backend device has been configured to force APTPL metadata
3638 * write-out, go ahead and propigate aptpl=1 down now.
3640 if (dev->dev_attrib.force_pr_aptpl)
3641 aptpl = 1;
3643 transport_kunmap_data_sg(cmd);
3644 buf = NULL;
3647 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3649 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3650 return TCM_INVALID_PARAMETER_LIST;
3653 * From spc4r17 section 6.14:
3655 * If the SPEC_I_PT bit is set to zero, the service action is not
3656 * REGISTER AND MOVE, and the parameter list length is not 24, then
3657 * the command shall be terminated with CHECK CONDITION status, with
3658 * the sense key set to ILLEGAL REQUEST, and the additional sense
3659 * code set to PARAMETER LIST LENGTH ERROR.
3661 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3662 (cmd->data_length != 24)) {
3663 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3664 " list length: %u\n", cmd->data_length);
3665 return TCM_INVALID_PARAMETER_LIST;
3669 * (core_scsi3_emulate_pro_* function parameters
3670 * are defined by spc4r17 Table 174:
3671 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3673 switch (sa) {
3674 case PRO_REGISTER:
3675 ret = core_scsi3_emulate_pro_register(cmd,
3676 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3677 break;
3678 case PRO_RESERVE:
3679 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3680 break;
3681 case PRO_RELEASE:
3682 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3683 break;
3684 case PRO_CLEAR:
3685 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3686 break;
3687 case PRO_PREEMPT:
3688 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3689 res_key, sa_res_key, PREEMPT);
3690 break;
3691 case PRO_PREEMPT_AND_ABORT:
3692 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3693 res_key, sa_res_key, PREEMPT_AND_ABORT);
3694 break;
3695 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3696 ret = core_scsi3_emulate_pro_register(cmd,
3697 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3698 break;
3699 case PRO_REGISTER_AND_MOVE:
3700 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3701 sa_res_key, aptpl, unreg);
3702 break;
3703 default:
3704 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3705 " action: 0x%02x\n", cdb[1] & 0x1f);
3706 return TCM_INVALID_CDB_FIELD;
3709 if (!ret)
3710 target_complete_cmd(cmd, GOOD);
3711 return ret;
3715 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3717 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3719 static sense_reason_t
3720 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3722 struct se_device *dev = cmd->se_dev;
3723 struct t10_pr_registration *pr_reg;
3724 unsigned char *buf;
3725 u32 add_len = 0, off = 8;
3727 if (cmd->data_length < 8) {
3728 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3729 " too small\n", cmd->data_length);
3730 return TCM_INVALID_CDB_FIELD;
3733 buf = transport_kmap_data_sg(cmd);
3734 if (!buf)
3735 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3737 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3738 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3739 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3740 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3742 spin_lock(&dev->t10_pr.registration_lock);
3743 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3744 pr_reg_list) {
3746 * Check for overflow of 8byte PRI READ_KEYS payload and
3747 * next reservation key list descriptor.
3749 if ((add_len + 8) > (cmd->data_length - 8))
3750 break;
3752 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3753 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3754 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3755 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3756 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3757 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3758 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3759 buf[off++] = (pr_reg->pr_res_key & 0xff);
3761 add_len += 8;
3763 spin_unlock(&dev->t10_pr.registration_lock);
3765 buf[4] = ((add_len >> 24) & 0xff);
3766 buf[5] = ((add_len >> 16) & 0xff);
3767 buf[6] = ((add_len >> 8) & 0xff);
3768 buf[7] = (add_len & 0xff);
3770 transport_kunmap_data_sg(cmd);
3772 return 0;
3776 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3778 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3780 static sense_reason_t
3781 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3783 struct se_device *dev = cmd->se_dev;
3784 struct t10_pr_registration *pr_reg;
3785 unsigned char *buf;
3786 u64 pr_res_key;
3787 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3789 if (cmd->data_length < 8) {
3790 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3791 " too small\n", cmd->data_length);
3792 return TCM_INVALID_CDB_FIELD;
3795 buf = transport_kmap_data_sg(cmd);
3796 if (!buf)
3797 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3799 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3800 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3801 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3802 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3804 spin_lock(&dev->dev_reservation_lock);
3805 pr_reg = dev->dev_pr_res_holder;
3806 if (pr_reg) {
3808 * Set the hardcoded Additional Length
3810 buf[4] = ((add_len >> 24) & 0xff);
3811 buf[5] = ((add_len >> 16) & 0xff);
3812 buf[6] = ((add_len >> 8) & 0xff);
3813 buf[7] = (add_len & 0xff);
3815 if (cmd->data_length < 22)
3816 goto err;
3819 * Set the Reservation key.
3821 * From spc4r17, section 5.7.10:
3822 * A persistent reservation holder has its reservation key
3823 * returned in the parameter data from a PERSISTENT
3824 * RESERVE IN command with READ RESERVATION service action as
3825 * follows:
3826 * a) For a persistent reservation of the type Write Exclusive
3827 * - All Registrants or Exclusive Access ­ All Regitrants,
3828 * the reservation key shall be set to zero; or
3829 * b) For all other persistent reservation types, the
3830 * reservation key shall be set to the registered
3831 * reservation key for the I_T nexus that holds the
3832 * persistent reservation.
3834 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3835 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3836 pr_res_key = 0;
3837 else
3838 pr_res_key = pr_reg->pr_res_key;
3840 buf[8] = ((pr_res_key >> 56) & 0xff);
3841 buf[9] = ((pr_res_key >> 48) & 0xff);
3842 buf[10] = ((pr_res_key >> 40) & 0xff);
3843 buf[11] = ((pr_res_key >> 32) & 0xff);
3844 buf[12] = ((pr_res_key >> 24) & 0xff);
3845 buf[13] = ((pr_res_key >> 16) & 0xff);
3846 buf[14] = ((pr_res_key >> 8) & 0xff);
3847 buf[15] = (pr_res_key & 0xff);
3849 * Set the SCOPE and TYPE
3851 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3852 (pr_reg->pr_res_type & 0x0f);
3855 err:
3856 spin_unlock(&dev->dev_reservation_lock);
3857 transport_kunmap_data_sg(cmd);
3859 return 0;
3863 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3865 * See spc4r17 section 6.13.4 Table 165
3867 static sense_reason_t
3868 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3870 struct se_device *dev = cmd->se_dev;
3871 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3872 unsigned char *buf;
3873 u16 add_len = 8; /* Hardcoded to 8. */
3875 if (cmd->data_length < 6) {
3876 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3877 " %u too small\n", cmd->data_length);
3878 return TCM_INVALID_CDB_FIELD;
3881 buf = transport_kmap_data_sg(cmd);
3882 if (!buf)
3883 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3885 buf[0] = ((add_len >> 8) & 0xff);
3886 buf[1] = (add_len & 0xff);
3887 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3888 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3889 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3890 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3892 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3893 * set the TMV: Task Mask Valid bit.
3895 buf[3] |= 0x80;
3897 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3899 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3901 * PTPL_A: Persistence across Target Power Loss Active bit
3903 if (pr_tmpl->pr_aptpl_active)
3904 buf[3] |= 0x01;
3906 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3908 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3909 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3910 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3911 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3912 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3913 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3915 transport_kunmap_data_sg(cmd);
3917 return 0;
3921 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3923 * See spc4r17 section 6.13.5 Table 168 and 169
3925 static sense_reason_t
3926 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3928 struct se_device *dev = cmd->se_dev;
3929 struct se_node_acl *se_nacl;
3930 struct se_portal_group *se_tpg;
3931 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3932 struct t10_reservation *pr_tmpl = &dev->t10_pr;
3933 unsigned char *buf;
3934 u32 add_desc_len = 0, add_len = 0;
3935 u32 off = 8; /* off into first Full Status descriptor */
3936 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3937 int exp_desc_len, desc_len;
3938 bool all_reg = false;
3940 if (cmd->data_length < 8) {
3941 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3942 " too small\n", cmd->data_length);
3943 return TCM_INVALID_CDB_FIELD;
3946 buf = transport_kmap_data_sg(cmd);
3947 if (!buf)
3948 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3950 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3951 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3952 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3953 buf[3] = (dev->t10_pr.pr_generation & 0xff);
3955 spin_lock(&dev->dev_reservation_lock);
3956 if (dev->dev_pr_res_holder) {
3957 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3959 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3960 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3961 all_reg = true;
3962 pr_res_type = pr_holder->pr_res_type;
3963 pr_res_scope = pr_holder->pr_res_scope;
3966 spin_unlock(&dev->dev_reservation_lock);
3968 spin_lock(&pr_tmpl->registration_lock);
3969 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3970 &pr_tmpl->registration_list, pr_reg_list) {
3972 se_nacl = pr_reg->pr_reg_nacl;
3973 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3974 add_desc_len = 0;
3976 atomic_inc_mb(&pr_reg->pr_res_holders);
3977 spin_unlock(&pr_tmpl->registration_lock);
3979 * Determine expected length of $FABRIC_MOD specific
3980 * TransportID full status descriptor..
3982 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3983 &format_code);
3984 if (exp_desc_len < 0 ||
3985 exp_desc_len + add_len > cmd->data_length) {
3986 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3987 " out of buffer: %d\n", cmd->data_length);
3988 spin_lock(&pr_tmpl->registration_lock);
3989 atomic_dec_mb(&pr_reg->pr_res_holders);
3990 break;
3993 * Set RESERVATION KEY
3995 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3996 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3997 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3998 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3999 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4000 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4001 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4002 buf[off++] = (pr_reg->pr_res_key & 0xff);
4003 off += 4; /* Skip Over Reserved area */
4006 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4008 if (pr_reg->pr_reg_all_tg_pt)
4009 buf[off] = 0x02;
4011 * The struct se_lun pointer will be present for the
4012 * reservation holder for PR_HOLDER bit.
4014 * Also, if this registration is the reservation
4015 * holder or there is an All Registrants reservation
4016 * active, fill in SCOPE and TYPE in the next byte.
4018 if (pr_reg->pr_res_holder) {
4019 buf[off++] |= 0x01;
4020 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4021 (pr_reg->pr_res_type & 0x0f);
4022 } else if (all_reg) {
4023 buf[off++] |= 0x01;
4024 buf[off++] = (pr_res_scope & 0xf0) |
4025 (pr_res_type & 0x0f);
4026 } else {
4027 off += 2;
4030 off += 4; /* Skip over reserved area */
4032 * From spc4r17 6.3.15:
4034 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4035 * IDENTIFIER field contains the relative port identifier (see
4036 * 3.1.120) of the target port that is part of the I_T nexus
4037 * described by this full status descriptor. If the ALL_TG_PT
4038 * bit is set to one, the contents of the RELATIVE TARGET PORT
4039 * IDENTIFIER field are not defined by this standard.
4041 if (!pr_reg->pr_reg_all_tg_pt) {
4042 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
4044 buf[off++] = ((sep_rtpi >> 8) & 0xff);
4045 buf[off++] = (sep_rtpi & 0xff);
4046 } else
4047 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4049 buf[off+4] = se_tpg->proto_id;
4052 * Now, have the $FABRIC_MOD fill in the transport ID.
4054 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4055 &format_code, &buf[off+4]);
4057 spin_lock(&pr_tmpl->registration_lock);
4058 atomic_dec_mb(&pr_reg->pr_res_holders);
4060 if (desc_len < 0)
4061 break;
4063 * Set the ADDITIONAL DESCRIPTOR LENGTH
4065 buf[off++] = ((desc_len >> 24) & 0xff);
4066 buf[off++] = ((desc_len >> 16) & 0xff);
4067 buf[off++] = ((desc_len >> 8) & 0xff);
4068 buf[off++] = (desc_len & 0xff);
4070 * Size of full desctipor header minus TransportID
4071 * containing $FABRIC_MOD specific) initiator device/port
4072 * WWN information.
4074 * See spc4r17 Section 6.13.5 Table 169
4076 add_desc_len = (24 + desc_len);
4078 off += desc_len;
4079 add_len += add_desc_len;
4081 spin_unlock(&pr_tmpl->registration_lock);
4083 * Set ADDITIONAL_LENGTH
4085 buf[4] = ((add_len >> 24) & 0xff);
4086 buf[5] = ((add_len >> 16) & 0xff);
4087 buf[6] = ((add_len >> 8) & 0xff);
4088 buf[7] = (add_len & 0xff);
4090 transport_kunmap_data_sg(cmd);
4092 return 0;
4095 sense_reason_t
4096 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4098 sense_reason_t ret;
4101 * Following spc2r20 5.5.1 Reservations overview:
4103 * If a logical unit has been reserved by any RESERVE command and is
4104 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4105 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4106 * initiator or service action and shall terminate with a RESERVATION
4107 * CONFLICT status.
4109 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4110 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4111 " SPC-2 reservation is held, returning"
4112 " RESERVATION_CONFLICT\n");
4113 return TCM_RESERVATION_CONFLICT;
4116 switch (cmd->t_task_cdb[1] & 0x1f) {
4117 case PRI_READ_KEYS:
4118 ret = core_scsi3_pri_read_keys(cmd);
4119 break;
4120 case PRI_READ_RESERVATION:
4121 ret = core_scsi3_pri_read_reservation(cmd);
4122 break;
4123 case PRI_REPORT_CAPABILITIES:
4124 ret = core_scsi3_pri_report_capabilities(cmd);
4125 break;
4126 case PRI_READ_FULL_STATUS:
4127 ret = core_scsi3_pri_read_full_status(cmd);
4128 break;
4129 default:
4130 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4131 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4132 return TCM_INVALID_CDB_FIELD;
4135 if (!ret)
4136 target_complete_cmd(cmd, GOOD);
4137 return ret;
4140 sense_reason_t
4141 target_check_reservation(struct se_cmd *cmd)
4143 struct se_device *dev = cmd->se_dev;
4144 sense_reason_t ret;
4146 if (!cmd->se_sess)
4147 return 0;
4148 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4149 return 0;
4150 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
4151 return 0;
4153 spin_lock(&dev->dev_reservation_lock);
4154 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4155 ret = target_scsi2_reservation_check(cmd);
4156 else
4157 ret = target_scsi3_pr_reservation_check(cmd);
4158 spin_unlock(&dev->dev_reservation_lock);
4160 return ret;