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