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