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