Linux Audio

Check our new training course

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