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