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