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