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