1 /******************************************************************************* 2 * Filename: target_core_alua.c 3 * 4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA) 5 * 6 * (c) Copyright 2009-2013 Datera, Inc. 7 * 8 * Nicholas A. Bellinger <nab@kernel.org> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 23 * 24 ******************************************************************************/ 25 26 #include <linux/slab.h> 27 #include <linux/spinlock.h> 28 #include <linux/configfs.h> 29 #include <linux/export.h> 30 #include <linux/file.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_alua.h" 42 #include "target_core_ua.h" 43 44 static sense_reason_t core_alua_check_transition(int state, int *primary); 45 static int core_alua_set_tg_pt_secondary_state( 46 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 47 struct se_port *port, int explict, int offline); 48 49 static u16 alua_lu_gps_counter; 50 static u32 alua_lu_gps_count; 51 52 static DEFINE_SPINLOCK(lu_gps_lock); 53 static LIST_HEAD(lu_gps_list); 54 55 struct t10_alua_lu_gp *default_lu_gp; 56 57 /* 58 * REPORT_TARGET_PORT_GROUPS 59 * 60 * See spc4r17 section 6.27 61 */ 62 sense_reason_t 63 target_emulate_report_target_port_groups(struct se_cmd *cmd) 64 { 65 struct se_device *dev = cmd->se_dev; 66 struct se_port *port; 67 struct t10_alua_tg_pt_gp *tg_pt_gp; 68 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 69 unsigned char *buf; 70 u32 rd_len = 0, off; 71 int ext_hdr = (cmd->t_task_cdb[1] & 0x20); 72 73 /* 74 * Skip over RESERVED area to first Target port group descriptor 75 * depending on the PARAMETER DATA FORMAT type.. 76 */ 77 if (ext_hdr != 0) 78 off = 8; 79 else 80 off = 4; 81 82 if (cmd->data_length < off) { 83 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too" 84 " small for %s header\n", cmd->data_length, 85 (ext_hdr) ? "extended" : "normal"); 86 return TCM_INVALID_CDB_FIELD; 87 } 88 buf = transport_kmap_data_sg(cmd); 89 if (!buf) 90 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 91 92 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 93 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list, 94 tg_pt_gp_list) { 95 /* 96 * Check if the Target port group and Target port descriptor list 97 * based on tg_pt_gp_members count will fit into the response payload. 98 * Otherwise, bump rd_len to let the initiator know we have exceeded 99 * the allocation length and the response is truncated. 100 */ 101 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) > 102 cmd->data_length) { 103 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4); 104 continue; 105 } 106 /* 107 * PREF: Preferred target port bit, determine if this 108 * bit should be set for port group. 109 */ 110 if (tg_pt_gp->tg_pt_gp_pref) 111 buf[off] = 0x80; 112 /* 113 * Set the ASYMMETRIC ACCESS State 114 */ 115 buf[off++] |= (atomic_read( 116 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff); 117 /* 118 * Set supported ASYMMETRIC ACCESS State bits 119 */ 120 buf[off] = 0x80; /* T_SUP */ 121 buf[off] |= 0x40; /* O_SUP */ 122 buf[off] |= 0x8; /* U_SUP */ 123 buf[off] |= 0x4; /* S_SUP */ 124 buf[off] |= 0x2; /* AN_SUP */ 125 buf[off++] |= 0x1; /* AO_SUP */ 126 /* 127 * TARGET PORT GROUP 128 */ 129 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff); 130 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff); 131 132 off++; /* Skip over Reserved */ 133 /* 134 * STATUS CODE 135 */ 136 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff); 137 /* 138 * Vendor Specific field 139 */ 140 buf[off++] = 0x00; 141 /* 142 * TARGET PORT COUNT 143 */ 144 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff); 145 rd_len += 8; 146 147 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 148 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list, 149 tg_pt_gp_mem_list) { 150 port = tg_pt_gp_mem->tg_pt; 151 /* 152 * Start Target Port descriptor format 153 * 154 * See spc4r17 section 6.2.7 Table 247 155 */ 156 off += 2; /* Skip over Obsolete */ 157 /* 158 * Set RELATIVE TARGET PORT IDENTIFIER 159 */ 160 buf[off++] = ((port->sep_rtpi >> 8) & 0xff); 161 buf[off++] = (port->sep_rtpi & 0xff); 162 rd_len += 4; 163 } 164 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 165 } 166 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 167 /* 168 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload 169 */ 170 put_unaligned_be32(rd_len, &buf[0]); 171 172 /* 173 * Fill in the Extended header parameter data format if requested 174 */ 175 if (ext_hdr != 0) { 176 buf[4] = 0x10; 177 /* 178 * Set the implict transition time (in seconds) for the application 179 * client to use as a base for it's transition timeout value. 180 * 181 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN 182 * this CDB was received upon to determine this value individually 183 * for ALUA target port group. 184 */ 185 port = cmd->se_lun->lun_sep; 186 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 187 if (tg_pt_gp_mem) { 188 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 189 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 190 if (tg_pt_gp) 191 buf[5] = tg_pt_gp->tg_pt_gp_implict_trans_secs; 192 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 193 } 194 } 195 transport_kunmap_data_sg(cmd); 196 197 target_complete_cmd(cmd, GOOD); 198 return 0; 199 } 200 201 /* 202 * SET_TARGET_PORT_GROUPS for explict ALUA operation. 203 * 204 * See spc4r17 section 6.35 205 */ 206 sense_reason_t 207 target_emulate_set_target_port_groups(struct se_cmd *cmd) 208 { 209 struct se_device *dev = cmd->se_dev; 210 struct se_port *port, *l_port = cmd->se_lun->lun_sep; 211 struct se_node_acl *nacl = cmd->se_sess->se_node_acl; 212 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp; 213 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem; 214 unsigned char *buf; 215 unsigned char *ptr; 216 sense_reason_t rc = TCM_NO_SENSE; 217 u32 len = 4; /* Skip over RESERVED area in header */ 218 int alua_access_state, primary = 0; 219 u16 tg_pt_id, rtpi; 220 221 if (!l_port) 222 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 223 224 if (cmd->data_length < 4) { 225 pr_warn("SET TARGET PORT GROUPS parameter list length %u too" 226 " small\n", cmd->data_length); 227 return TCM_INVALID_PARAMETER_LIST; 228 } 229 230 buf = transport_kmap_data_sg(cmd); 231 if (!buf) 232 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 233 234 /* 235 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed 236 * for the local tg_pt_gp. 237 */ 238 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem; 239 if (!l_tg_pt_gp_mem) { 240 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n"); 241 rc = TCM_UNSUPPORTED_SCSI_OPCODE; 242 goto out; 243 } 244 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock); 245 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp; 246 if (!l_tg_pt_gp) { 247 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock); 248 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n"); 249 rc = TCM_UNSUPPORTED_SCSI_OPCODE; 250 goto out; 251 } 252 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock); 253 254 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)) { 255 pr_debug("Unable to process SET_TARGET_PORT_GROUPS" 256 " while TPGS_EXPLICT_ALUA is disabled\n"); 257 rc = TCM_UNSUPPORTED_SCSI_OPCODE; 258 goto out; 259 } 260 261 ptr = &buf[4]; /* Skip over RESERVED area in header */ 262 263 while (len < cmd->data_length) { 264 bool found = false; 265 alua_access_state = (ptr[0] & 0x0f); 266 /* 267 * Check the received ALUA access state, and determine if 268 * the state is a primary or secondary target port asymmetric 269 * access state. 270 */ 271 rc = core_alua_check_transition(alua_access_state, &primary); 272 if (rc) { 273 /* 274 * If the SET TARGET PORT GROUPS attempts to establish 275 * an invalid combination of target port asymmetric 276 * access states or attempts to establish an 277 * unsupported target port asymmetric access state, 278 * then the command shall be terminated with CHECK 279 * CONDITION status, with the sense key set to ILLEGAL 280 * REQUEST, and the additional sense code set to INVALID 281 * FIELD IN PARAMETER LIST. 282 */ 283 goto out; 284 } 285 286 /* 287 * If the ASYMMETRIC ACCESS STATE field (see table 267) 288 * specifies a primary target port asymmetric access state, 289 * then the TARGET PORT GROUP OR TARGET PORT field specifies 290 * a primary target port group for which the primary target 291 * port asymmetric access state shall be changed. If the 292 * ASYMMETRIC ACCESS STATE field specifies a secondary target 293 * port asymmetric access state, then the TARGET PORT GROUP OR 294 * TARGET PORT field specifies the relative target port 295 * identifier (see 3.1.120) of the target port for which the 296 * secondary target port asymmetric access state shall be 297 * changed. 298 */ 299 if (primary) { 300 tg_pt_id = get_unaligned_be16(ptr + 2); 301 /* 302 * Locate the matching target port group ID from 303 * the global tg_pt_gp list 304 */ 305 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 306 list_for_each_entry(tg_pt_gp, 307 &dev->t10_alua.tg_pt_gps_list, 308 tg_pt_gp_list) { 309 if (!tg_pt_gp->tg_pt_gp_valid_id) 310 continue; 311 312 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id) 313 continue; 314 315 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt); 316 smp_mb__after_atomic_inc(); 317 318 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 319 320 if (!core_alua_do_port_transition(tg_pt_gp, 321 dev, l_port, nacl, 322 alua_access_state, 1)) 323 found = true; 324 325 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 326 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt); 327 smp_mb__after_atomic_dec(); 328 break; 329 } 330 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 331 } else { 332 /* 333 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify 334 * the Target Port in question for the the incoming 335 * SET_TARGET_PORT_GROUPS op. 336 */ 337 rtpi = get_unaligned_be16(ptr + 2); 338 /* 339 * Locate the matching relative target port identifier 340 * for the struct se_device storage object. 341 */ 342 spin_lock(&dev->se_port_lock); 343 list_for_each_entry(port, &dev->dev_sep_list, 344 sep_list) { 345 if (port->sep_rtpi != rtpi) 346 continue; 347 348 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 349 350 spin_unlock(&dev->se_port_lock); 351 352 if (!core_alua_set_tg_pt_secondary_state( 353 tg_pt_gp_mem, port, 1, 1)) 354 found = true; 355 356 spin_lock(&dev->se_port_lock); 357 break; 358 } 359 spin_unlock(&dev->se_port_lock); 360 } 361 362 if (!found) { 363 rc = TCM_INVALID_PARAMETER_LIST; 364 goto out; 365 } 366 367 ptr += 4; 368 len += 4; 369 } 370 371 out: 372 transport_kunmap_data_sg(cmd); 373 if (!rc) 374 target_complete_cmd(cmd, GOOD); 375 return rc; 376 } 377 378 static inline int core_alua_state_nonoptimized( 379 struct se_cmd *cmd, 380 unsigned char *cdb, 381 int nonop_delay_msecs, 382 u8 *alua_ascq) 383 { 384 /* 385 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked 386 * later to determine if processing of this cmd needs to be 387 * temporarily delayed for the Active/NonOptimized primary access state. 388 */ 389 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED; 390 cmd->alua_nonop_delay = nonop_delay_msecs; 391 return 0; 392 } 393 394 static inline int core_alua_state_standby( 395 struct se_cmd *cmd, 396 unsigned char *cdb, 397 u8 *alua_ascq) 398 { 399 /* 400 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by 401 * spc4r17 section 5.9.2.4.4 402 */ 403 switch (cdb[0]) { 404 case INQUIRY: 405 case LOG_SELECT: 406 case LOG_SENSE: 407 case MODE_SELECT: 408 case MODE_SENSE: 409 case REPORT_LUNS: 410 case RECEIVE_DIAGNOSTIC: 411 case SEND_DIAGNOSTIC: 412 return 0; 413 case MAINTENANCE_IN: 414 switch (cdb[1] & 0x1f) { 415 case MI_REPORT_TARGET_PGS: 416 return 0; 417 default: 418 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY; 419 return 1; 420 } 421 case MAINTENANCE_OUT: 422 switch (cdb[1]) { 423 case MO_SET_TARGET_PGS: 424 return 0; 425 default: 426 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY; 427 return 1; 428 } 429 case REQUEST_SENSE: 430 case PERSISTENT_RESERVE_IN: 431 case PERSISTENT_RESERVE_OUT: 432 case READ_BUFFER: 433 case WRITE_BUFFER: 434 return 0; 435 default: 436 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY; 437 return 1; 438 } 439 440 return 0; 441 } 442 443 static inline int core_alua_state_unavailable( 444 struct se_cmd *cmd, 445 unsigned char *cdb, 446 u8 *alua_ascq) 447 { 448 /* 449 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by 450 * spc4r17 section 5.9.2.4.5 451 */ 452 switch (cdb[0]) { 453 case INQUIRY: 454 case REPORT_LUNS: 455 return 0; 456 case MAINTENANCE_IN: 457 switch (cdb[1] & 0x1f) { 458 case MI_REPORT_TARGET_PGS: 459 return 0; 460 default: 461 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE; 462 return 1; 463 } 464 case MAINTENANCE_OUT: 465 switch (cdb[1]) { 466 case MO_SET_TARGET_PGS: 467 return 0; 468 default: 469 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE; 470 return 1; 471 } 472 case REQUEST_SENSE: 473 case READ_BUFFER: 474 case WRITE_BUFFER: 475 return 0; 476 default: 477 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE; 478 return 1; 479 } 480 481 return 0; 482 } 483 484 static inline int core_alua_state_transition( 485 struct se_cmd *cmd, 486 unsigned char *cdb, 487 u8 *alua_ascq) 488 { 489 /* 490 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by 491 * spc4r17 section 5.9.2.5 492 */ 493 switch (cdb[0]) { 494 case INQUIRY: 495 case REPORT_LUNS: 496 return 0; 497 case MAINTENANCE_IN: 498 switch (cdb[1] & 0x1f) { 499 case MI_REPORT_TARGET_PGS: 500 return 0; 501 default: 502 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION; 503 return 1; 504 } 505 case REQUEST_SENSE: 506 case READ_BUFFER: 507 case WRITE_BUFFER: 508 return 0; 509 default: 510 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION; 511 return 1; 512 } 513 514 return 0; 515 } 516 517 /* 518 * return 1: Is used to signal LUN not accecsable, and check condition/not ready 519 * return 0: Used to signal success 520 * reutrn -1: Used to signal failure, and invalid cdb field 521 */ 522 sense_reason_t 523 target_alua_state_check(struct se_cmd *cmd) 524 { 525 struct se_device *dev = cmd->se_dev; 526 unsigned char *cdb = cmd->t_task_cdb; 527 struct se_lun *lun = cmd->se_lun; 528 struct se_port *port = lun->lun_sep; 529 struct t10_alua_tg_pt_gp *tg_pt_gp; 530 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 531 int out_alua_state, nonop_delay_msecs; 532 u8 alua_ascq; 533 int ret; 534 535 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) 536 return 0; 537 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) 538 return 0; 539 540 if (!port) 541 return 0; 542 /* 543 * First, check for a struct se_port specific secondary ALUA target port 544 * access state: OFFLINE 545 */ 546 if (atomic_read(&port->sep_tg_pt_secondary_offline)) { 547 pr_debug("ALUA: Got secondary offline status for local" 548 " target port\n"); 549 alua_ascq = ASCQ_04H_ALUA_OFFLINE; 550 ret = 1; 551 goto out; 552 } 553 /* 554 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the 555 * ALUA target port group, to obtain current ALUA access state. 556 * Otherwise look for the underlying struct se_device association with 557 * a ALUA logical unit group. 558 */ 559 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 560 if (!tg_pt_gp_mem) 561 return 0; 562 563 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 564 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 565 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state); 566 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs; 567 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 568 /* 569 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional 570 * statement so the compiler knows explicitly to check this case first. 571 * For the Optimized ALUA access state case, we want to process the 572 * incoming fabric cmd ASAP.. 573 */ 574 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED) 575 return 0; 576 577 switch (out_alua_state) { 578 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: 579 ret = core_alua_state_nonoptimized(cmd, cdb, 580 nonop_delay_msecs, &alua_ascq); 581 break; 582 case ALUA_ACCESS_STATE_STANDBY: 583 ret = core_alua_state_standby(cmd, cdb, &alua_ascq); 584 break; 585 case ALUA_ACCESS_STATE_UNAVAILABLE: 586 ret = core_alua_state_unavailable(cmd, cdb, &alua_ascq); 587 break; 588 case ALUA_ACCESS_STATE_TRANSITION: 589 ret = core_alua_state_transition(cmd, cdb, &alua_ascq); 590 break; 591 /* 592 * OFFLINE is a secondary ALUA target port group access state, that is 593 * handled above with struct se_port->sep_tg_pt_secondary_offline=1 594 */ 595 case ALUA_ACCESS_STATE_OFFLINE: 596 default: 597 pr_err("Unknown ALUA access state: 0x%02x\n", 598 out_alua_state); 599 return TCM_INVALID_CDB_FIELD; 600 } 601 602 out: 603 if (ret > 0) { 604 /* 605 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible'; 606 * The ALUA additional sense code qualifier (ASCQ) is determined 607 * by the ALUA primary or secondary access state.. 608 */ 609 pr_debug("[%s]: ALUA TG Port not available, " 610 "SenseKey: NOT_READY, ASC/ASCQ: " 611 "0x04/0x%02x\n", 612 cmd->se_tfo->get_fabric_name(), alua_ascq); 613 614 cmd->scsi_asc = 0x04; 615 cmd->scsi_ascq = alua_ascq; 616 return TCM_CHECK_CONDITION_NOT_READY; 617 } 618 619 return 0; 620 } 621 622 /* 623 * Check implict and explict ALUA state change request. 624 */ 625 static sense_reason_t 626 core_alua_check_transition(int state, int *primary) 627 { 628 switch (state) { 629 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED: 630 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: 631 case ALUA_ACCESS_STATE_STANDBY: 632 case ALUA_ACCESS_STATE_UNAVAILABLE: 633 /* 634 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are 635 * defined as primary target port asymmetric access states. 636 */ 637 *primary = 1; 638 break; 639 case ALUA_ACCESS_STATE_OFFLINE: 640 /* 641 * OFFLINE state is defined as a secondary target port 642 * asymmetric access state. 643 */ 644 *primary = 0; 645 break; 646 default: 647 pr_err("Unknown ALUA access state: 0x%02x\n", state); 648 return TCM_INVALID_PARAMETER_LIST; 649 } 650 651 return 0; 652 } 653 654 static char *core_alua_dump_state(int state) 655 { 656 switch (state) { 657 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED: 658 return "Active/Optimized"; 659 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: 660 return "Active/NonOptimized"; 661 case ALUA_ACCESS_STATE_STANDBY: 662 return "Standby"; 663 case ALUA_ACCESS_STATE_UNAVAILABLE: 664 return "Unavailable"; 665 case ALUA_ACCESS_STATE_OFFLINE: 666 return "Offline"; 667 default: 668 return "Unknown"; 669 } 670 671 return NULL; 672 } 673 674 char *core_alua_dump_status(int status) 675 { 676 switch (status) { 677 case ALUA_STATUS_NONE: 678 return "None"; 679 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG: 680 return "Altered by Explict STPG"; 681 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA: 682 return "Altered by Implict ALUA"; 683 default: 684 return "Unknown"; 685 } 686 687 return NULL; 688 } 689 690 /* 691 * Used by fabric modules to determine when we need to delay processing 692 * for the Active/NonOptimized paths.. 693 */ 694 int core_alua_check_nonop_delay( 695 struct se_cmd *cmd) 696 { 697 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED)) 698 return 0; 699 if (in_interrupt()) 700 return 0; 701 /* 702 * The ALUA Active/NonOptimized access state delay can be disabled 703 * in via configfs with a value of zero 704 */ 705 if (!cmd->alua_nonop_delay) 706 return 0; 707 /* 708 * struct se_cmd->alua_nonop_delay gets set by a target port group 709 * defined interval in core_alua_state_nonoptimized() 710 */ 711 msleep_interruptible(cmd->alua_nonop_delay); 712 return 0; 713 } 714 EXPORT_SYMBOL(core_alua_check_nonop_delay); 715 716 /* 717 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex 718 * 719 */ 720 static int core_alua_write_tpg_metadata( 721 const char *path, 722 unsigned char *md_buf, 723 u32 md_buf_len) 724 { 725 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600); 726 int ret; 727 728 if (IS_ERR(file)) { 729 pr_err("filp_open(%s) for ALUA metadata failed\n", path); 730 return -ENODEV; 731 } 732 ret = kernel_write(file, md_buf, md_buf_len, 0); 733 if (ret < 0) 734 pr_err("Error writing ALUA metadata file: %s\n", path); 735 fput(file); 736 return (ret < 0) ? -EIO : 0; 737 } 738 739 /* 740 * Called with tg_pt_gp->tg_pt_gp_md_mutex held 741 */ 742 static int core_alua_update_tpg_primary_metadata( 743 struct t10_alua_tg_pt_gp *tg_pt_gp, 744 int primary_state, 745 unsigned char *md_buf) 746 { 747 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn; 748 char path[ALUA_METADATA_PATH_LEN]; 749 int len; 750 751 memset(path, 0, ALUA_METADATA_PATH_LEN); 752 753 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len, 754 "tg_pt_gp_id=%hu\n" 755 "alua_access_state=0x%02x\n" 756 "alua_access_status=0x%02x\n", 757 tg_pt_gp->tg_pt_gp_id, primary_state, 758 tg_pt_gp->tg_pt_gp_alua_access_status); 759 760 snprintf(path, ALUA_METADATA_PATH_LEN, 761 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0], 762 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item)); 763 764 return core_alua_write_tpg_metadata(path, md_buf, len); 765 } 766 767 static int core_alua_do_transition_tg_pt( 768 struct t10_alua_tg_pt_gp *tg_pt_gp, 769 struct se_port *l_port, 770 struct se_node_acl *nacl, 771 unsigned char *md_buf, 772 int new_state, 773 int explict) 774 { 775 struct se_dev_entry *se_deve; 776 struct se_lun_acl *lacl; 777 struct se_port *port; 778 struct t10_alua_tg_pt_gp_member *mem; 779 int old_state = 0; 780 /* 781 * Save the old primary ALUA access state, and set the current state 782 * to ALUA_ACCESS_STATE_TRANSITION. 783 */ 784 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state); 785 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, 786 ALUA_ACCESS_STATE_TRANSITION); 787 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ? 788 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG : 789 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA; 790 /* 791 * Check for the optional ALUA primary state transition delay 792 */ 793 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0) 794 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs); 795 796 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 797 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list, 798 tg_pt_gp_mem_list) { 799 port = mem->tg_pt; 800 /* 801 * After an implicit target port asymmetric access state 802 * change, a device server shall establish a unit attention 803 * condition for the initiator port associated with every I_T 804 * nexus with the additional sense code set to ASYMMETRIC 805 * ACCESS STATE CHAGED. 806 * 807 * After an explicit target port asymmetric access state 808 * change, a device server shall establish a unit attention 809 * condition with the additional sense code set to ASYMMETRIC 810 * ACCESS STATE CHANGED for the initiator port associated with 811 * every I_T nexus other than the I_T nexus on which the SET 812 * TARGET PORT GROUPS command 813 */ 814 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt); 815 smp_mb__after_atomic_inc(); 816 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 817 818 spin_lock_bh(&port->sep_alua_lock); 819 list_for_each_entry(se_deve, &port->sep_alua_list, 820 alua_port_list) { 821 lacl = se_deve->se_lun_acl; 822 /* 823 * se_deve->se_lun_acl pointer may be NULL for a 824 * entry created without explict Node+MappedLUN ACLs 825 */ 826 if (!lacl) 827 continue; 828 829 if (explict && 830 (nacl != NULL) && (nacl == lacl->se_lun_nacl) && 831 (l_port != NULL) && (l_port == port)) 832 continue; 833 834 core_scsi3_ua_allocate(lacl->se_lun_nacl, 835 se_deve->mapped_lun, 0x2A, 836 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED); 837 } 838 spin_unlock_bh(&port->sep_alua_lock); 839 840 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 841 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt); 842 smp_mb__after_atomic_dec(); 843 } 844 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 845 /* 846 * Update the ALUA metadata buf that has been allocated in 847 * core_alua_do_port_transition(), this metadata will be written 848 * to struct file. 849 * 850 * Note that there is the case where we do not want to update the 851 * metadata when the saved metadata is being parsed in userspace 852 * when setting the existing port access state and access status. 853 * 854 * Also note that the failure to write out the ALUA metadata to 855 * struct file does NOT affect the actual ALUA transition. 856 */ 857 if (tg_pt_gp->tg_pt_gp_write_metadata) { 858 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex); 859 core_alua_update_tpg_primary_metadata(tg_pt_gp, 860 new_state, md_buf); 861 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex); 862 } 863 /* 864 * Set the current primary ALUA access state to the requested new state 865 */ 866 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state); 867 868 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu" 869 " from primary access state %s to %s\n", (explict) ? "explict" : 870 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item), 871 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state), 872 core_alua_dump_state(new_state)); 873 874 return 0; 875 } 876 877 int core_alua_do_port_transition( 878 struct t10_alua_tg_pt_gp *l_tg_pt_gp, 879 struct se_device *l_dev, 880 struct se_port *l_port, 881 struct se_node_acl *l_nacl, 882 int new_state, 883 int explict) 884 { 885 struct se_device *dev; 886 struct se_port *port; 887 struct se_node_acl *nacl; 888 struct t10_alua_lu_gp *lu_gp; 889 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem; 890 struct t10_alua_tg_pt_gp *tg_pt_gp; 891 unsigned char *md_buf; 892 int primary; 893 894 if (core_alua_check_transition(new_state, &primary) != 0) 895 return -EINVAL; 896 897 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL); 898 if (!md_buf) { 899 pr_err("Unable to allocate buf for ALUA metadata\n"); 900 return -ENOMEM; 901 } 902 903 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem; 904 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock); 905 lu_gp = local_lu_gp_mem->lu_gp; 906 atomic_inc(&lu_gp->lu_gp_ref_cnt); 907 smp_mb__after_atomic_inc(); 908 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock); 909 /* 910 * For storage objects that are members of the 'default_lu_gp', 911 * we only do transition on the passed *l_tp_pt_gp, and not 912 * on all of the matching target port groups IDs in default_lu_gp. 913 */ 914 if (!lu_gp->lu_gp_id) { 915 /* 916 * core_alua_do_transition_tg_pt() will always return 917 * success. 918 */ 919 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl, 920 md_buf, new_state, explict); 921 atomic_dec(&lu_gp->lu_gp_ref_cnt); 922 smp_mb__after_atomic_dec(); 923 kfree(md_buf); 924 return 0; 925 } 926 /* 927 * For all other LU groups aside from 'default_lu_gp', walk all of 928 * the associated storage objects looking for a matching target port 929 * group ID from the local target port group. 930 */ 931 spin_lock(&lu_gp->lu_gp_lock); 932 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, 933 lu_gp_mem_list) { 934 935 dev = lu_gp_mem->lu_gp_mem_dev; 936 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt); 937 smp_mb__after_atomic_inc(); 938 spin_unlock(&lu_gp->lu_gp_lock); 939 940 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 941 list_for_each_entry(tg_pt_gp, 942 &dev->t10_alua.tg_pt_gps_list, 943 tg_pt_gp_list) { 944 945 if (!tg_pt_gp->tg_pt_gp_valid_id) 946 continue; 947 /* 948 * If the target behavior port asymmetric access state 949 * is changed for any target port group accessiable via 950 * a logical unit within a LU group, the target port 951 * behavior group asymmetric access states for the same 952 * target port group accessible via other logical units 953 * in that LU group will also change. 954 */ 955 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id) 956 continue; 957 958 if (l_tg_pt_gp == tg_pt_gp) { 959 port = l_port; 960 nacl = l_nacl; 961 } else { 962 port = NULL; 963 nacl = NULL; 964 } 965 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt); 966 smp_mb__after_atomic_inc(); 967 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 968 /* 969 * core_alua_do_transition_tg_pt() will always return 970 * success. 971 */ 972 core_alua_do_transition_tg_pt(tg_pt_gp, port, 973 nacl, md_buf, new_state, explict); 974 975 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 976 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt); 977 smp_mb__after_atomic_dec(); 978 } 979 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 980 981 spin_lock(&lu_gp->lu_gp_lock); 982 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt); 983 smp_mb__after_atomic_dec(); 984 } 985 spin_unlock(&lu_gp->lu_gp_lock); 986 987 pr_debug("Successfully processed LU Group: %s all ALUA TG PT" 988 " Group IDs: %hu %s transition to primary state: %s\n", 989 config_item_name(&lu_gp->lu_gp_group.cg_item), 990 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict", 991 core_alua_dump_state(new_state)); 992 993 atomic_dec(&lu_gp->lu_gp_ref_cnt); 994 smp_mb__after_atomic_dec(); 995 kfree(md_buf); 996 return 0; 997 } 998 999 /* 1000 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held 1001 */ 1002 static int core_alua_update_tpg_secondary_metadata( 1003 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 1004 struct se_port *port, 1005 unsigned char *md_buf, 1006 u32 md_buf_len) 1007 { 1008 struct se_portal_group *se_tpg = port->sep_tpg; 1009 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN]; 1010 int len; 1011 1012 memset(path, 0, ALUA_METADATA_PATH_LEN); 1013 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN); 1014 1015 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s", 1016 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg)); 1017 1018 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL) 1019 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu", 1020 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg)); 1021 1022 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n" 1023 "alua_tg_pt_status=0x%02x\n", 1024 atomic_read(&port->sep_tg_pt_secondary_offline), 1025 port->sep_tg_pt_secondary_stat); 1026 1027 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u", 1028 se_tpg->se_tpg_tfo->get_fabric_name(), wwn, 1029 port->sep_lun->unpacked_lun); 1030 1031 return core_alua_write_tpg_metadata(path, md_buf, len); 1032 } 1033 1034 static int core_alua_set_tg_pt_secondary_state( 1035 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 1036 struct se_port *port, 1037 int explict, 1038 int offline) 1039 { 1040 struct t10_alua_tg_pt_gp *tg_pt_gp; 1041 unsigned char *md_buf; 1042 u32 md_buf_len; 1043 int trans_delay_msecs; 1044 1045 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1046 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 1047 if (!tg_pt_gp) { 1048 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1049 pr_err("Unable to complete secondary state" 1050 " transition\n"); 1051 return -EINVAL; 1052 } 1053 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs; 1054 /* 1055 * Set the secondary ALUA target port access state to OFFLINE 1056 * or release the previously secondary state for struct se_port 1057 */ 1058 if (offline) 1059 atomic_set(&port->sep_tg_pt_secondary_offline, 1); 1060 else 1061 atomic_set(&port->sep_tg_pt_secondary_offline, 0); 1062 1063 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len; 1064 port->sep_tg_pt_secondary_stat = (explict) ? 1065 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG : 1066 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA; 1067 1068 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu" 1069 " to secondary access state: %s\n", (explict) ? "explict" : 1070 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item), 1071 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE"); 1072 1073 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1074 /* 1075 * Do the optional transition delay after we set the secondary 1076 * ALUA access state. 1077 */ 1078 if (trans_delay_msecs != 0) 1079 msleep_interruptible(trans_delay_msecs); 1080 /* 1081 * See if we need to update the ALUA fabric port metadata for 1082 * secondary state and status 1083 */ 1084 if (port->sep_tg_pt_secondary_write_md) { 1085 md_buf = kzalloc(md_buf_len, GFP_KERNEL); 1086 if (!md_buf) { 1087 pr_err("Unable to allocate md_buf for" 1088 " secondary ALUA access metadata\n"); 1089 return -ENOMEM; 1090 } 1091 mutex_lock(&port->sep_tg_pt_md_mutex); 1092 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port, 1093 md_buf, md_buf_len); 1094 mutex_unlock(&port->sep_tg_pt_md_mutex); 1095 1096 kfree(md_buf); 1097 } 1098 1099 return 0; 1100 } 1101 1102 struct t10_alua_lu_gp * 1103 core_alua_allocate_lu_gp(const char *name, int def_group) 1104 { 1105 struct t10_alua_lu_gp *lu_gp; 1106 1107 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL); 1108 if (!lu_gp) { 1109 pr_err("Unable to allocate struct t10_alua_lu_gp\n"); 1110 return ERR_PTR(-ENOMEM); 1111 } 1112 INIT_LIST_HEAD(&lu_gp->lu_gp_node); 1113 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list); 1114 spin_lock_init(&lu_gp->lu_gp_lock); 1115 atomic_set(&lu_gp->lu_gp_ref_cnt, 0); 1116 1117 if (def_group) { 1118 lu_gp->lu_gp_id = alua_lu_gps_counter++; 1119 lu_gp->lu_gp_valid_id = 1; 1120 alua_lu_gps_count++; 1121 } 1122 1123 return lu_gp; 1124 } 1125 1126 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id) 1127 { 1128 struct t10_alua_lu_gp *lu_gp_tmp; 1129 u16 lu_gp_id_tmp; 1130 /* 1131 * The lu_gp->lu_gp_id may only be set once.. 1132 */ 1133 if (lu_gp->lu_gp_valid_id) { 1134 pr_warn("ALUA LU Group already has a valid ID," 1135 " ignoring request\n"); 1136 return -EINVAL; 1137 } 1138 1139 spin_lock(&lu_gps_lock); 1140 if (alua_lu_gps_count == 0x0000ffff) { 1141 pr_err("Maximum ALUA alua_lu_gps_count:" 1142 " 0x0000ffff reached\n"); 1143 spin_unlock(&lu_gps_lock); 1144 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp); 1145 return -ENOSPC; 1146 } 1147 again: 1148 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id : 1149 alua_lu_gps_counter++; 1150 1151 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) { 1152 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) { 1153 if (!lu_gp_id) 1154 goto again; 1155 1156 pr_warn("ALUA Logical Unit Group ID: %hu" 1157 " already exists, ignoring request\n", 1158 lu_gp_id); 1159 spin_unlock(&lu_gps_lock); 1160 return -EINVAL; 1161 } 1162 } 1163 1164 lu_gp->lu_gp_id = lu_gp_id_tmp; 1165 lu_gp->lu_gp_valid_id = 1; 1166 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list); 1167 alua_lu_gps_count++; 1168 spin_unlock(&lu_gps_lock); 1169 1170 return 0; 1171 } 1172 1173 static struct t10_alua_lu_gp_member * 1174 core_alua_allocate_lu_gp_mem(struct se_device *dev) 1175 { 1176 struct t10_alua_lu_gp_member *lu_gp_mem; 1177 1178 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL); 1179 if (!lu_gp_mem) { 1180 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n"); 1181 return ERR_PTR(-ENOMEM); 1182 } 1183 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list); 1184 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock); 1185 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0); 1186 1187 lu_gp_mem->lu_gp_mem_dev = dev; 1188 dev->dev_alua_lu_gp_mem = lu_gp_mem; 1189 1190 return lu_gp_mem; 1191 } 1192 1193 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp) 1194 { 1195 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp; 1196 /* 1197 * Once we have reached this point, config_item_put() has 1198 * already been called from target_core_alua_drop_lu_gp(). 1199 * 1200 * Here, we remove the *lu_gp from the global list so that 1201 * no associations can be made while we are releasing 1202 * struct t10_alua_lu_gp. 1203 */ 1204 spin_lock(&lu_gps_lock); 1205 list_del(&lu_gp->lu_gp_node); 1206 alua_lu_gps_count--; 1207 spin_unlock(&lu_gps_lock); 1208 /* 1209 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name() 1210 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be 1211 * released with core_alua_put_lu_gp_from_name() 1212 */ 1213 while (atomic_read(&lu_gp->lu_gp_ref_cnt)) 1214 cpu_relax(); 1215 /* 1216 * Release reference to struct t10_alua_lu_gp * from all associated 1217 * struct se_device. 1218 */ 1219 spin_lock(&lu_gp->lu_gp_lock); 1220 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp, 1221 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) { 1222 if (lu_gp_mem->lu_gp_assoc) { 1223 list_del(&lu_gp_mem->lu_gp_mem_list); 1224 lu_gp->lu_gp_members--; 1225 lu_gp_mem->lu_gp_assoc = 0; 1226 } 1227 spin_unlock(&lu_gp->lu_gp_lock); 1228 /* 1229 * 1230 * lu_gp_mem is associated with a single 1231 * struct se_device->dev_alua_lu_gp_mem, and is released when 1232 * struct se_device is released via core_alua_free_lu_gp_mem(). 1233 * 1234 * If the passed lu_gp does NOT match the default_lu_gp, assume 1235 * we want to re-assocate a given lu_gp_mem with default_lu_gp. 1236 */ 1237 spin_lock(&lu_gp_mem->lu_gp_mem_lock); 1238 if (lu_gp != default_lu_gp) 1239 __core_alua_attach_lu_gp_mem(lu_gp_mem, 1240 default_lu_gp); 1241 else 1242 lu_gp_mem->lu_gp = NULL; 1243 spin_unlock(&lu_gp_mem->lu_gp_mem_lock); 1244 1245 spin_lock(&lu_gp->lu_gp_lock); 1246 } 1247 spin_unlock(&lu_gp->lu_gp_lock); 1248 1249 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp); 1250 } 1251 1252 void core_alua_free_lu_gp_mem(struct se_device *dev) 1253 { 1254 struct t10_alua_lu_gp *lu_gp; 1255 struct t10_alua_lu_gp_member *lu_gp_mem; 1256 1257 lu_gp_mem = dev->dev_alua_lu_gp_mem; 1258 if (!lu_gp_mem) 1259 return; 1260 1261 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt)) 1262 cpu_relax(); 1263 1264 spin_lock(&lu_gp_mem->lu_gp_mem_lock); 1265 lu_gp = lu_gp_mem->lu_gp; 1266 if (lu_gp) { 1267 spin_lock(&lu_gp->lu_gp_lock); 1268 if (lu_gp_mem->lu_gp_assoc) { 1269 list_del(&lu_gp_mem->lu_gp_mem_list); 1270 lu_gp->lu_gp_members--; 1271 lu_gp_mem->lu_gp_assoc = 0; 1272 } 1273 spin_unlock(&lu_gp->lu_gp_lock); 1274 lu_gp_mem->lu_gp = NULL; 1275 } 1276 spin_unlock(&lu_gp_mem->lu_gp_mem_lock); 1277 1278 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem); 1279 } 1280 1281 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name) 1282 { 1283 struct t10_alua_lu_gp *lu_gp; 1284 struct config_item *ci; 1285 1286 spin_lock(&lu_gps_lock); 1287 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) { 1288 if (!lu_gp->lu_gp_valid_id) 1289 continue; 1290 ci = &lu_gp->lu_gp_group.cg_item; 1291 if (!strcmp(config_item_name(ci), name)) { 1292 atomic_inc(&lu_gp->lu_gp_ref_cnt); 1293 spin_unlock(&lu_gps_lock); 1294 return lu_gp; 1295 } 1296 } 1297 spin_unlock(&lu_gps_lock); 1298 1299 return NULL; 1300 } 1301 1302 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp) 1303 { 1304 spin_lock(&lu_gps_lock); 1305 atomic_dec(&lu_gp->lu_gp_ref_cnt); 1306 spin_unlock(&lu_gps_lock); 1307 } 1308 1309 /* 1310 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock 1311 */ 1312 void __core_alua_attach_lu_gp_mem( 1313 struct t10_alua_lu_gp_member *lu_gp_mem, 1314 struct t10_alua_lu_gp *lu_gp) 1315 { 1316 spin_lock(&lu_gp->lu_gp_lock); 1317 lu_gp_mem->lu_gp = lu_gp; 1318 lu_gp_mem->lu_gp_assoc = 1; 1319 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list); 1320 lu_gp->lu_gp_members++; 1321 spin_unlock(&lu_gp->lu_gp_lock); 1322 } 1323 1324 /* 1325 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock 1326 */ 1327 void __core_alua_drop_lu_gp_mem( 1328 struct t10_alua_lu_gp_member *lu_gp_mem, 1329 struct t10_alua_lu_gp *lu_gp) 1330 { 1331 spin_lock(&lu_gp->lu_gp_lock); 1332 list_del(&lu_gp_mem->lu_gp_mem_list); 1333 lu_gp_mem->lu_gp = NULL; 1334 lu_gp_mem->lu_gp_assoc = 0; 1335 lu_gp->lu_gp_members--; 1336 spin_unlock(&lu_gp->lu_gp_lock); 1337 } 1338 1339 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev, 1340 const char *name, int def_group) 1341 { 1342 struct t10_alua_tg_pt_gp *tg_pt_gp; 1343 1344 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL); 1345 if (!tg_pt_gp) { 1346 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n"); 1347 return NULL; 1348 } 1349 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list); 1350 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list); 1351 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex); 1352 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock); 1353 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0); 1354 tg_pt_gp->tg_pt_gp_dev = dev; 1355 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN; 1356 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, 1357 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED); 1358 /* 1359 * Enable both explict and implict ALUA support by default 1360 */ 1361 tg_pt_gp->tg_pt_gp_alua_access_type = 1362 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA; 1363 /* 1364 * Set the default Active/NonOptimized Delay in milliseconds 1365 */ 1366 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS; 1367 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS; 1368 tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS; 1369 1370 if (def_group) { 1371 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 1372 tg_pt_gp->tg_pt_gp_id = 1373 dev->t10_alua.alua_tg_pt_gps_counter++; 1374 tg_pt_gp->tg_pt_gp_valid_id = 1; 1375 dev->t10_alua.alua_tg_pt_gps_count++; 1376 list_add_tail(&tg_pt_gp->tg_pt_gp_list, 1377 &dev->t10_alua.tg_pt_gps_list); 1378 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1379 } 1380 1381 return tg_pt_gp; 1382 } 1383 1384 int core_alua_set_tg_pt_gp_id( 1385 struct t10_alua_tg_pt_gp *tg_pt_gp, 1386 u16 tg_pt_gp_id) 1387 { 1388 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; 1389 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp; 1390 u16 tg_pt_gp_id_tmp; 1391 1392 /* 1393 * The tg_pt_gp->tg_pt_gp_id may only be set once.. 1394 */ 1395 if (tg_pt_gp->tg_pt_gp_valid_id) { 1396 pr_warn("ALUA TG PT Group already has a valid ID," 1397 " ignoring request\n"); 1398 return -EINVAL; 1399 } 1400 1401 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 1402 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) { 1403 pr_err("Maximum ALUA alua_tg_pt_gps_count:" 1404 " 0x0000ffff reached\n"); 1405 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1406 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp); 1407 return -ENOSPC; 1408 } 1409 again: 1410 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id : 1411 dev->t10_alua.alua_tg_pt_gps_counter++; 1412 1413 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list, 1414 tg_pt_gp_list) { 1415 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) { 1416 if (!tg_pt_gp_id) 1417 goto again; 1418 1419 pr_err("ALUA Target Port Group ID: %hu already" 1420 " exists, ignoring request\n", tg_pt_gp_id); 1421 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1422 return -EINVAL; 1423 } 1424 } 1425 1426 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp; 1427 tg_pt_gp->tg_pt_gp_valid_id = 1; 1428 list_add_tail(&tg_pt_gp->tg_pt_gp_list, 1429 &dev->t10_alua.tg_pt_gps_list); 1430 dev->t10_alua.alua_tg_pt_gps_count++; 1431 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1432 1433 return 0; 1434 } 1435 1436 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem( 1437 struct se_port *port) 1438 { 1439 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 1440 1441 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache, 1442 GFP_KERNEL); 1443 if (!tg_pt_gp_mem) { 1444 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n"); 1445 return ERR_PTR(-ENOMEM); 1446 } 1447 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list); 1448 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1449 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0); 1450 1451 tg_pt_gp_mem->tg_pt = port; 1452 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem; 1453 1454 return tg_pt_gp_mem; 1455 } 1456 1457 void core_alua_free_tg_pt_gp( 1458 struct t10_alua_tg_pt_gp *tg_pt_gp) 1459 { 1460 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; 1461 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp; 1462 1463 /* 1464 * Once we have reached this point, config_item_put() has already 1465 * been called from target_core_alua_drop_tg_pt_gp(). 1466 * 1467 * Here we remove *tg_pt_gp from the global list so that 1468 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS 1469 * can be made while we are releasing struct t10_alua_tg_pt_gp. 1470 */ 1471 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 1472 list_del(&tg_pt_gp->tg_pt_gp_list); 1473 dev->t10_alua.alua_tg_pt_gps_counter--; 1474 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1475 1476 /* 1477 * Allow a struct t10_alua_tg_pt_gp_member * referenced by 1478 * core_alua_get_tg_pt_gp_by_name() in 1479 * target_core_configfs.c:target_core_store_alua_tg_pt_gp() 1480 * to be released with core_alua_put_tg_pt_gp_from_name(). 1481 */ 1482 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt)) 1483 cpu_relax(); 1484 1485 /* 1486 * Release reference to struct t10_alua_tg_pt_gp from all associated 1487 * struct se_port. 1488 */ 1489 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1490 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp, 1491 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) { 1492 if (tg_pt_gp_mem->tg_pt_gp_assoc) { 1493 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list); 1494 tg_pt_gp->tg_pt_gp_members--; 1495 tg_pt_gp_mem->tg_pt_gp_assoc = 0; 1496 } 1497 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1498 /* 1499 * tg_pt_gp_mem is associated with a single 1500 * se_port->sep_alua_tg_pt_gp_mem, and is released via 1501 * core_alua_free_tg_pt_gp_mem(). 1502 * 1503 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp, 1504 * assume we want to re-assocate a given tg_pt_gp_mem with 1505 * default_tg_pt_gp. 1506 */ 1507 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1508 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) { 1509 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, 1510 dev->t10_alua.default_tg_pt_gp); 1511 } else 1512 tg_pt_gp_mem->tg_pt_gp = NULL; 1513 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1514 1515 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1516 } 1517 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1518 1519 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp); 1520 } 1521 1522 void core_alua_free_tg_pt_gp_mem(struct se_port *port) 1523 { 1524 struct t10_alua_tg_pt_gp *tg_pt_gp; 1525 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 1526 1527 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 1528 if (!tg_pt_gp_mem) 1529 return; 1530 1531 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt)) 1532 cpu_relax(); 1533 1534 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1535 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 1536 if (tg_pt_gp) { 1537 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1538 if (tg_pt_gp_mem->tg_pt_gp_assoc) { 1539 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list); 1540 tg_pt_gp->tg_pt_gp_members--; 1541 tg_pt_gp_mem->tg_pt_gp_assoc = 0; 1542 } 1543 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1544 tg_pt_gp_mem->tg_pt_gp = NULL; 1545 } 1546 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1547 1548 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem); 1549 } 1550 1551 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name( 1552 struct se_device *dev, const char *name) 1553 { 1554 struct t10_alua_tg_pt_gp *tg_pt_gp; 1555 struct config_item *ci; 1556 1557 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 1558 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list, 1559 tg_pt_gp_list) { 1560 if (!tg_pt_gp->tg_pt_gp_valid_id) 1561 continue; 1562 ci = &tg_pt_gp->tg_pt_gp_group.cg_item; 1563 if (!strcmp(config_item_name(ci), name)) { 1564 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt); 1565 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1566 return tg_pt_gp; 1567 } 1568 } 1569 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1570 1571 return NULL; 1572 } 1573 1574 static void core_alua_put_tg_pt_gp_from_name( 1575 struct t10_alua_tg_pt_gp *tg_pt_gp) 1576 { 1577 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; 1578 1579 spin_lock(&dev->t10_alua.tg_pt_gps_lock); 1580 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt); 1581 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1582 } 1583 1584 /* 1585 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held 1586 */ 1587 void __core_alua_attach_tg_pt_gp_mem( 1588 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 1589 struct t10_alua_tg_pt_gp *tg_pt_gp) 1590 { 1591 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1592 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp; 1593 tg_pt_gp_mem->tg_pt_gp_assoc = 1; 1594 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list, 1595 &tg_pt_gp->tg_pt_gp_mem_list); 1596 tg_pt_gp->tg_pt_gp_members++; 1597 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1598 } 1599 1600 /* 1601 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held 1602 */ 1603 static void __core_alua_drop_tg_pt_gp_mem( 1604 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 1605 struct t10_alua_tg_pt_gp *tg_pt_gp) 1606 { 1607 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1608 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list); 1609 tg_pt_gp_mem->tg_pt_gp = NULL; 1610 tg_pt_gp_mem->tg_pt_gp_assoc = 0; 1611 tg_pt_gp->tg_pt_gp_members--; 1612 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1613 } 1614 1615 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page) 1616 { 1617 struct config_item *tg_pt_ci; 1618 struct t10_alua_tg_pt_gp *tg_pt_gp; 1619 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 1620 ssize_t len = 0; 1621 1622 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 1623 if (!tg_pt_gp_mem) 1624 return len; 1625 1626 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1627 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 1628 if (tg_pt_gp) { 1629 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item; 1630 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:" 1631 " %hu\nTG Port Primary Access State: %s\nTG Port " 1632 "Primary Access Status: %s\nTG Port Secondary Access" 1633 " State: %s\nTG Port Secondary Access Status: %s\n", 1634 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id, 1635 core_alua_dump_state(atomic_read( 1636 &tg_pt_gp->tg_pt_gp_alua_access_state)), 1637 core_alua_dump_status( 1638 tg_pt_gp->tg_pt_gp_alua_access_status), 1639 (atomic_read(&port->sep_tg_pt_secondary_offline)) ? 1640 "Offline" : "None", 1641 core_alua_dump_status(port->sep_tg_pt_secondary_stat)); 1642 } 1643 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1644 1645 return len; 1646 } 1647 1648 ssize_t core_alua_store_tg_pt_gp_info( 1649 struct se_port *port, 1650 const char *page, 1651 size_t count) 1652 { 1653 struct se_portal_group *tpg; 1654 struct se_lun *lun; 1655 struct se_device *dev = port->sep_lun->lun_se_dev; 1656 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL; 1657 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 1658 unsigned char buf[TG_PT_GROUP_NAME_BUF]; 1659 int move = 0; 1660 1661 tpg = port->sep_tpg; 1662 lun = port->sep_lun; 1663 1664 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 1665 if (!tg_pt_gp_mem) 1666 return 0; 1667 1668 if (count > TG_PT_GROUP_NAME_BUF) { 1669 pr_err("ALUA Target Port Group alias too large!\n"); 1670 return -EINVAL; 1671 } 1672 memset(buf, 0, TG_PT_GROUP_NAME_BUF); 1673 memcpy(buf, page, count); 1674 /* 1675 * Any ALUA target port group alias besides "NULL" means we will be 1676 * making a new group association. 1677 */ 1678 if (strcmp(strstrip(buf), "NULL")) { 1679 /* 1680 * core_alua_get_tg_pt_gp_by_name() will increment reference to 1681 * struct t10_alua_tg_pt_gp. This reference is released with 1682 * core_alua_put_tg_pt_gp_from_name() below. 1683 */ 1684 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev, 1685 strstrip(buf)); 1686 if (!tg_pt_gp_new) 1687 return -ENODEV; 1688 } 1689 1690 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1691 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 1692 if (tg_pt_gp) { 1693 /* 1694 * Clearing an existing tg_pt_gp association, and replacing 1695 * with the default_tg_pt_gp. 1696 */ 1697 if (!tg_pt_gp_new) { 1698 pr_debug("Target_Core_ConfigFS: Moving" 1699 " %s/tpgt_%hu/%s from ALUA Target Port Group:" 1700 " alua/%s, ID: %hu back to" 1701 " default_tg_pt_gp\n", 1702 tpg->se_tpg_tfo->tpg_get_wwn(tpg), 1703 tpg->se_tpg_tfo->tpg_get_tag(tpg), 1704 config_item_name(&lun->lun_group.cg_item), 1705 config_item_name( 1706 &tg_pt_gp->tg_pt_gp_group.cg_item), 1707 tg_pt_gp->tg_pt_gp_id); 1708 1709 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp); 1710 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, 1711 dev->t10_alua.default_tg_pt_gp); 1712 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1713 1714 return count; 1715 } 1716 /* 1717 * Removing existing association of tg_pt_gp_mem with tg_pt_gp 1718 */ 1719 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp); 1720 move = 1; 1721 } 1722 /* 1723 * Associate tg_pt_gp_mem with tg_pt_gp_new. 1724 */ 1725 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new); 1726 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1727 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA" 1728 " Target Port Group: alua/%s, ID: %hu\n", (move) ? 1729 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg), 1730 tpg->se_tpg_tfo->tpg_get_tag(tpg), 1731 config_item_name(&lun->lun_group.cg_item), 1732 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item), 1733 tg_pt_gp_new->tg_pt_gp_id); 1734 1735 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new); 1736 return count; 1737 } 1738 1739 ssize_t core_alua_show_access_type( 1740 struct t10_alua_tg_pt_gp *tg_pt_gp, 1741 char *page) 1742 { 1743 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) && 1744 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) 1745 return sprintf(page, "Implict and Explict\n"); 1746 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA) 1747 return sprintf(page, "Implict\n"); 1748 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) 1749 return sprintf(page, "Explict\n"); 1750 else 1751 return sprintf(page, "None\n"); 1752 } 1753 1754 ssize_t core_alua_store_access_type( 1755 struct t10_alua_tg_pt_gp *tg_pt_gp, 1756 const char *page, 1757 size_t count) 1758 { 1759 unsigned long tmp; 1760 int ret; 1761 1762 ret = kstrtoul(page, 0, &tmp); 1763 if (ret < 0) { 1764 pr_err("Unable to extract alua_access_type\n"); 1765 return ret; 1766 } 1767 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) { 1768 pr_err("Illegal value for alua_access_type:" 1769 " %lu\n", tmp); 1770 return -EINVAL; 1771 } 1772 if (tmp == 3) 1773 tg_pt_gp->tg_pt_gp_alua_access_type = 1774 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA; 1775 else if (tmp == 2) 1776 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA; 1777 else if (tmp == 1) 1778 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA; 1779 else 1780 tg_pt_gp->tg_pt_gp_alua_access_type = 0; 1781 1782 return count; 1783 } 1784 1785 ssize_t core_alua_show_nonop_delay_msecs( 1786 struct t10_alua_tg_pt_gp *tg_pt_gp, 1787 char *page) 1788 { 1789 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs); 1790 } 1791 1792 ssize_t core_alua_store_nonop_delay_msecs( 1793 struct t10_alua_tg_pt_gp *tg_pt_gp, 1794 const char *page, 1795 size_t count) 1796 { 1797 unsigned long tmp; 1798 int ret; 1799 1800 ret = kstrtoul(page, 0, &tmp); 1801 if (ret < 0) { 1802 pr_err("Unable to extract nonop_delay_msecs\n"); 1803 return ret; 1804 } 1805 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) { 1806 pr_err("Passed nonop_delay_msecs: %lu, exceeds" 1807 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp, 1808 ALUA_MAX_NONOP_DELAY_MSECS); 1809 return -EINVAL; 1810 } 1811 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp; 1812 1813 return count; 1814 } 1815 1816 ssize_t core_alua_show_trans_delay_msecs( 1817 struct t10_alua_tg_pt_gp *tg_pt_gp, 1818 char *page) 1819 { 1820 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs); 1821 } 1822 1823 ssize_t core_alua_store_trans_delay_msecs( 1824 struct t10_alua_tg_pt_gp *tg_pt_gp, 1825 const char *page, 1826 size_t count) 1827 { 1828 unsigned long tmp; 1829 int ret; 1830 1831 ret = kstrtoul(page, 0, &tmp); 1832 if (ret < 0) { 1833 pr_err("Unable to extract trans_delay_msecs\n"); 1834 return ret; 1835 } 1836 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) { 1837 pr_err("Passed trans_delay_msecs: %lu, exceeds" 1838 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp, 1839 ALUA_MAX_TRANS_DELAY_MSECS); 1840 return -EINVAL; 1841 } 1842 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp; 1843 1844 return count; 1845 } 1846 1847 ssize_t core_alua_show_implict_trans_secs( 1848 struct t10_alua_tg_pt_gp *tg_pt_gp, 1849 char *page) 1850 { 1851 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs); 1852 } 1853 1854 ssize_t core_alua_store_implict_trans_secs( 1855 struct t10_alua_tg_pt_gp *tg_pt_gp, 1856 const char *page, 1857 size_t count) 1858 { 1859 unsigned long tmp; 1860 int ret; 1861 1862 ret = kstrtoul(page, 0, &tmp); 1863 if (ret < 0) { 1864 pr_err("Unable to extract implict_trans_secs\n"); 1865 return ret; 1866 } 1867 if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) { 1868 pr_err("Passed implict_trans_secs: %lu, exceeds" 1869 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp, 1870 ALUA_MAX_IMPLICT_TRANS_SECS); 1871 return -EINVAL; 1872 } 1873 tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp; 1874 1875 return count; 1876 } 1877 1878 ssize_t core_alua_show_preferred_bit( 1879 struct t10_alua_tg_pt_gp *tg_pt_gp, 1880 char *page) 1881 { 1882 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref); 1883 } 1884 1885 ssize_t core_alua_store_preferred_bit( 1886 struct t10_alua_tg_pt_gp *tg_pt_gp, 1887 const char *page, 1888 size_t count) 1889 { 1890 unsigned long tmp; 1891 int ret; 1892 1893 ret = kstrtoul(page, 0, &tmp); 1894 if (ret < 0) { 1895 pr_err("Unable to extract preferred ALUA value\n"); 1896 return ret; 1897 } 1898 if ((tmp != 0) && (tmp != 1)) { 1899 pr_err("Illegal value for preferred ALUA: %lu\n", tmp); 1900 return -EINVAL; 1901 } 1902 tg_pt_gp->tg_pt_gp_pref = (int)tmp; 1903 1904 return count; 1905 } 1906 1907 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page) 1908 { 1909 if (!lun->lun_sep) 1910 return -ENODEV; 1911 1912 return sprintf(page, "%d\n", 1913 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline)); 1914 } 1915 1916 ssize_t core_alua_store_offline_bit( 1917 struct se_lun *lun, 1918 const char *page, 1919 size_t count) 1920 { 1921 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 1922 unsigned long tmp; 1923 int ret; 1924 1925 if (!lun->lun_sep) 1926 return -ENODEV; 1927 1928 ret = kstrtoul(page, 0, &tmp); 1929 if (ret < 0) { 1930 pr_err("Unable to extract alua_tg_pt_offline value\n"); 1931 return ret; 1932 } 1933 if ((tmp != 0) && (tmp != 1)) { 1934 pr_err("Illegal value for alua_tg_pt_offline: %lu\n", 1935 tmp); 1936 return -EINVAL; 1937 } 1938 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem; 1939 if (!tg_pt_gp_mem) { 1940 pr_err("Unable to locate *tg_pt_gp_mem\n"); 1941 return -EINVAL; 1942 } 1943 1944 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem, 1945 lun->lun_sep, 0, (int)tmp); 1946 if (ret < 0) 1947 return -EINVAL; 1948 1949 return count; 1950 } 1951 1952 ssize_t core_alua_show_secondary_status( 1953 struct se_lun *lun, 1954 char *page) 1955 { 1956 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat); 1957 } 1958 1959 ssize_t core_alua_store_secondary_status( 1960 struct se_lun *lun, 1961 const char *page, 1962 size_t count) 1963 { 1964 unsigned long tmp; 1965 int ret; 1966 1967 ret = kstrtoul(page, 0, &tmp); 1968 if (ret < 0) { 1969 pr_err("Unable to extract alua_tg_pt_status\n"); 1970 return ret; 1971 } 1972 if ((tmp != ALUA_STATUS_NONE) && 1973 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) && 1974 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) { 1975 pr_err("Illegal value for alua_tg_pt_status: %lu\n", 1976 tmp); 1977 return -EINVAL; 1978 } 1979 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp; 1980 1981 return count; 1982 } 1983 1984 ssize_t core_alua_show_secondary_write_metadata( 1985 struct se_lun *lun, 1986 char *page) 1987 { 1988 return sprintf(page, "%d\n", 1989 lun->lun_sep->sep_tg_pt_secondary_write_md); 1990 } 1991 1992 ssize_t core_alua_store_secondary_write_metadata( 1993 struct se_lun *lun, 1994 const char *page, 1995 size_t count) 1996 { 1997 unsigned long tmp; 1998 int ret; 1999 2000 ret = kstrtoul(page, 0, &tmp); 2001 if (ret < 0) { 2002 pr_err("Unable to extract alua_tg_pt_write_md\n"); 2003 return ret; 2004 } 2005 if ((tmp != 0) && (tmp != 1)) { 2006 pr_err("Illegal value for alua_tg_pt_write_md:" 2007 " %lu\n", tmp); 2008 return -EINVAL; 2009 } 2010 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp; 2011 2012 return count; 2013 } 2014 2015 int core_setup_alua(struct se_device *dev) 2016 { 2017 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV && 2018 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) { 2019 struct t10_alua_lu_gp_member *lu_gp_mem; 2020 2021 /* 2022 * Associate this struct se_device with the default ALUA 2023 * LUN Group. 2024 */ 2025 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev); 2026 if (IS_ERR(lu_gp_mem)) 2027 return PTR_ERR(lu_gp_mem); 2028 2029 spin_lock(&lu_gp_mem->lu_gp_mem_lock); 2030 __core_alua_attach_lu_gp_mem(lu_gp_mem, 2031 default_lu_gp); 2032 spin_unlock(&lu_gp_mem->lu_gp_mem_lock); 2033 2034 pr_debug("%s: Adding to default ALUA LU Group:" 2035 " core/alua/lu_gps/default_lu_gp\n", 2036 dev->transport->name); 2037 } 2038 2039 return 0; 2040 } 2041