1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * Copyright (c) 2009-2010 Rising Tide Systems
7 * Copyright (c) 2009-2010 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/configfs.h>
30 #include <linux/export.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_device.h>
37 #include <target/target_core_transport.h>
38 #include <target/target_core_fabric_ops.h>
39 #include <target/target_core_configfs.h>
41 #include "target_core_alua.h"
42 #include "target_core_hba.h"
43 #include "target_core_ua.h"
45 static int core_alua_check_transition(int state, 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 explict, int offline);
50 static u16 alua_lu_gps_counter;
51 static u32 alua_lu_gps_count;
53 static DEFINE_SPINLOCK(lu_gps_lock);
54 static LIST_HEAD(lu_gps_list);
56 struct t10_alua_lu_gp *default_lu_gp;
59 * REPORT_TARGET_PORT_GROUPS
61 * See spc4r17 section 6.27
63 int target_emulate_report_target_port_groups(struct se_task *task)
65 struct se_cmd *cmd = task->task_se_cmd;
66 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
68 struct t10_alua_tg_pt_gp *tg_pt_gp;
69 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
71 u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
72 Target port group descriptor */
74 * Need at least 4 bytes of response data or else we can't
75 * even fit the return data length.
77 if (cmd->data_length < 4) {
78 pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
79 " too small\n", cmd->data_length);
83 buf = transport_kmap_data_sg(cmd);
85 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
86 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
89 * Check if the Target port group and Target port descriptor list
90 * based on tg_pt_gp_members count will fit into the response payload.
91 * Otherwise, bump rd_len to let the initiator know we have exceeded
92 * the allocation length and the response is truncated.
94 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
96 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
100 * PREF: Preferred target port bit, determine if this
101 * bit should be set for port group.
103 if (tg_pt_gp->tg_pt_gp_pref)
106 * Set the ASYMMETRIC ACCESS State
108 buf[off++] |= (atomic_read(
109 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
111 * Set supported ASYMMETRIC ACCESS State bits
113 buf[off] = 0x80; /* T_SUP */
114 buf[off] |= 0x40; /* O_SUP */
115 buf[off] |= 0x8; /* U_SUP */
116 buf[off] |= 0x4; /* S_SUP */
117 buf[off] |= 0x2; /* AN_SUP */
118 buf[off++] |= 0x1; /* AO_SUP */
122 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
123 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
125 off++; /* Skip over Reserved */
129 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
131 * Vendor Specific field
137 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
140 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
141 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
143 port = tg_pt_gp_mem->tg_pt;
145 * Start Target Port descriptor format
147 * See spc4r17 section 6.2.7 Table 247
149 off += 2; /* Skip over Obsolete */
151 * Set RELATIVE TARGET PORT IDENTIFIER
153 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
154 buf[off++] = (port->sep_rtpi & 0xff);
157 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
159 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
161 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
163 buf[0] = ((rd_len >> 24) & 0xff);
164 buf[1] = ((rd_len >> 16) & 0xff);
165 buf[2] = ((rd_len >> 8) & 0xff);
166 buf[3] = (rd_len & 0xff);
168 transport_kunmap_data_sg(cmd);
170 task->task_scsi_status = GOOD;
171 transport_complete_task(task, 1);
176 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
178 * See spc4r17 section 6.35
180 int target_emulate_set_target_port_groups(struct se_task *task)
182 struct se_cmd *cmd = task->task_se_cmd;
183 struct se_device *dev = cmd->se_dev;
184 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
185 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
186 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
187 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
188 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
191 u32 len = 4; /* Skip over RESERVED area in header */
192 int alua_access_state, primary = 0, rc;
196 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
199 buf = transport_kmap_data_sg(cmd);
202 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
203 * for the local tg_pt_gp.
205 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
206 if (!l_tg_pt_gp_mem) {
207 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
208 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
212 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
213 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
215 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
216 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
217 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
221 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
222 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
225 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
226 " while TPGS_EXPLICT_ALUA is disabled\n");
227 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
232 ptr = &buf[4]; /* Skip over RESERVED area in header */
234 while (len < cmd->data_length) {
235 alua_access_state = (ptr[0] & 0x0f);
237 * Check the received ALUA access state, and determine if
238 * the state is a primary or secondary target port asymmetric
241 rc = core_alua_check_transition(alua_access_state, &primary);
244 * If the SET TARGET PORT GROUPS attempts to establish
245 * an invalid combination of target port asymmetric
246 * access states or attempts to establish an
247 * unsupported target port asymmetric access state,
248 * then the command shall be terminated with CHECK
249 * CONDITION status, with the sense key set to ILLEGAL
250 * REQUEST, and the additional sense code set to INVALID
251 * FIELD IN PARAMETER LIST.
253 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
259 * If the ASYMMETRIC ACCESS STATE field (see table 267)
260 * specifies a primary target port asymmetric access state,
261 * then the TARGET PORT GROUP OR TARGET PORT field specifies
262 * a primary target port group for which the primary target
263 * port asymmetric access state shall be changed. If the
264 * ASYMMETRIC ACCESS STATE field specifies a secondary target
265 * port asymmetric access state, then the TARGET PORT GROUP OR
266 * TARGET PORT field specifies the relative target port
267 * identifier (see 3.1.120) of the target port for which the
268 * secondary target port asymmetric access state shall be
272 tg_pt_id = get_unaligned_be16(ptr + 2);
274 * Locate the matching target port group ID from
275 * the global tg_pt_gp list
277 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
278 list_for_each_entry(tg_pt_gp,
279 &su_dev->t10_alua.tg_pt_gps_list,
281 if (!tg_pt_gp->tg_pt_gp_valid_id)
284 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
287 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
288 smp_mb__after_atomic_inc();
289 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
291 rc = core_alua_do_port_transition(tg_pt_gp,
293 alua_access_state, 1);
295 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
296 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
297 smp_mb__after_atomic_dec();
300 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
302 * If not matching target port group ID can be located
303 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
306 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
312 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
313 * the Target Port in question for the the incoming
314 * SET_TARGET_PORT_GROUPS op.
316 rtpi = get_unaligned_be16(ptr + 2);
318 * Locate the matching relative target port identifer
319 * for the struct se_device storage object.
321 spin_lock(&dev->se_port_lock);
322 list_for_each_entry(port, &dev->dev_sep_list,
324 if (port->sep_rtpi != rtpi)
327 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
328 spin_unlock(&dev->se_port_lock);
330 rc = core_alua_set_tg_pt_secondary_state(
331 tg_pt_gp_mem, port, 1, 1);
333 spin_lock(&dev->se_port_lock);
336 spin_unlock(&dev->se_port_lock);
338 * If not matching relative target port identifier can
339 * be located, throw an exception with ASCQ:
340 * INVALID_PARAMETER_LIST
343 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
354 transport_kunmap_data_sg(cmd);
356 task->task_scsi_status = GOOD;
357 transport_complete_task(task, 1);
362 static inline int core_alua_state_nonoptimized(
365 int nonop_delay_msecs,
369 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
370 * later to determine if processing of this cmd needs to be
371 * temporarily delayed for the Active/NonOptimized primary access state.
373 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
374 cmd->alua_nonop_delay = nonop_delay_msecs;
378 static inline int core_alua_state_standby(
384 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
385 * spc4r17 section 5.9.2.4.4
394 case RECEIVE_DIAGNOSTIC:
395 case SEND_DIAGNOSTIC:
398 switch (cdb[1] & 0x1f) {
399 case MI_REPORT_TARGET_PGS:
402 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
405 case MAINTENANCE_OUT:
407 case MO_SET_TARGET_PGS:
410 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
414 case PERSISTENT_RESERVE_IN:
415 case PERSISTENT_RESERVE_OUT:
420 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
427 static inline int core_alua_state_unavailable(
433 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
434 * spc4r17 section 5.9.2.4.5
441 switch (cdb[1] & 0x1f) {
442 case MI_REPORT_TARGET_PGS:
445 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
448 case MAINTENANCE_OUT:
450 case MO_SET_TARGET_PGS:
453 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
461 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
468 static inline int core_alua_state_transition(
474 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
475 * spc4r17 section 5.9.2.5
482 switch (cdb[1] & 0x1f) {
483 case MI_REPORT_TARGET_PGS:
486 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
494 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
502 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
503 * in transport_cmd_sequencer(). This function is assigned to
504 * struct t10_alua *->state_check() in core_setup_alua()
506 static int core_alua_state_check_nop(
515 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
516 * This function is assigned to struct t10_alua *->state_check() in
519 * Also, this function can return three different return codes to
520 * signal transport_generic_cmd_sequencer()
522 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
523 * return 0: Used to signal success
524 * reutrn -1: Used to signal failure, and invalid cdb field
526 static int core_alua_state_check(
531 struct se_lun *lun = cmd->se_lun;
532 struct se_port *port = lun->lun_sep;
533 struct t10_alua_tg_pt_gp *tg_pt_gp;
534 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
535 int out_alua_state, nonop_delay_msecs;
540 * First, check for a struct se_port specific secondary ALUA target port
541 * access state: OFFLINE
543 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
544 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
545 pr_debug("ALUA: Got secondary offline status for local"
547 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
551 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
552 * ALUA target port group, to obtain current ALUA access state.
553 * Otherwise look for the underlying struct se_device association with
554 * a ALUA logical unit group.
556 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
557 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
558 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
559 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
560 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
561 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
563 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
564 * statement so the compiler knows explicitly to check this case first.
565 * For the Optimized ALUA access state case, we want to process the
566 * incoming fabric cmd ASAP..
568 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
571 switch (out_alua_state) {
572 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
573 return core_alua_state_nonoptimized(cmd, cdb,
574 nonop_delay_msecs, alua_ascq);
575 case ALUA_ACCESS_STATE_STANDBY:
576 return core_alua_state_standby(cmd, cdb, alua_ascq);
577 case ALUA_ACCESS_STATE_UNAVAILABLE:
578 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
579 case ALUA_ACCESS_STATE_TRANSITION:
580 return core_alua_state_transition(cmd, cdb, alua_ascq);
582 * OFFLINE is a secondary ALUA target port group access state, that is
583 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
585 case ALUA_ACCESS_STATE_OFFLINE:
587 pr_err("Unknown ALUA access state: 0x%02x\n",
596 * Check implict and explict ALUA state change request.
598 static int core_alua_check_transition(int state, int *primary)
601 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
602 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
603 case ALUA_ACCESS_STATE_STANDBY:
604 case ALUA_ACCESS_STATE_UNAVAILABLE:
606 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
607 * defined as primary target port asymmetric access states.
611 case ALUA_ACCESS_STATE_OFFLINE:
613 * OFFLINE state is defined as a secondary target port
614 * asymmetric access state.
619 pr_err("Unknown ALUA access state: 0x%02x\n", state);
626 static char *core_alua_dump_state(int state)
629 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
630 return "Active/Optimized";
631 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
632 return "Active/NonOptimized";
633 case ALUA_ACCESS_STATE_STANDBY:
635 case ALUA_ACCESS_STATE_UNAVAILABLE:
636 return "Unavailable";
637 case ALUA_ACCESS_STATE_OFFLINE:
646 char *core_alua_dump_status(int status)
649 case ALUA_STATUS_NONE:
651 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
652 return "Altered by Explict STPG";
653 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
654 return "Altered by Implict ALUA";
663 * Used by fabric modules to determine when we need to delay processing
664 * for the Active/NonOptimized paths..
666 int core_alua_check_nonop_delay(
669 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
674 * The ALUA Active/NonOptimized access state delay can be disabled
675 * in via configfs with a value of zero
677 if (!cmd->alua_nonop_delay)
680 * struct se_cmd->alua_nonop_delay gets set by a target port group
681 * defined interval in core_alua_state_nonoptimized()
683 msleep_interruptible(cmd->alua_nonop_delay);
686 EXPORT_SYMBOL(core_alua_check_nonop_delay);
689 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
692 static int core_alua_write_tpg_metadata(
694 unsigned char *md_buf,
700 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
702 memset(iov, 0, sizeof(struct iovec));
704 file = filp_open(path, flags, 0600);
705 if (IS_ERR(file) || !file || !file->f_dentry) {
706 pr_err("filp_open(%s) for ALUA metadata failed\n",
711 iov[0].iov_base = &md_buf[0];
712 iov[0].iov_len = md_buf_len;
716 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
720 pr_err("Error writing ALUA metadata file: %s\n", path);
721 filp_close(file, NULL);
724 filp_close(file, NULL);
730 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
732 static int core_alua_update_tpg_primary_metadata(
733 struct t10_alua_tg_pt_gp *tg_pt_gp,
735 unsigned char *md_buf)
737 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
738 struct t10_wwn *wwn = &su_dev->t10_wwn;
739 char path[ALUA_METADATA_PATH_LEN];
742 memset(path, 0, ALUA_METADATA_PATH_LEN);
744 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
746 "alua_access_state=0x%02x\n"
747 "alua_access_status=0x%02x\n",
748 tg_pt_gp->tg_pt_gp_id, primary_state,
749 tg_pt_gp->tg_pt_gp_alua_access_status);
751 snprintf(path, ALUA_METADATA_PATH_LEN,
752 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
753 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
755 return core_alua_write_tpg_metadata(path, md_buf, len);
758 static int core_alua_do_transition_tg_pt(
759 struct t10_alua_tg_pt_gp *tg_pt_gp,
760 struct se_port *l_port,
761 struct se_node_acl *nacl,
762 unsigned char *md_buf,
766 struct se_dev_entry *se_deve;
767 struct se_lun_acl *lacl;
768 struct se_port *port;
769 struct t10_alua_tg_pt_gp_member *mem;
772 * Save the old primary ALUA access state, and set the current state
773 * to ALUA_ACCESS_STATE_TRANSITION.
775 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
776 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
777 ALUA_ACCESS_STATE_TRANSITION);
778 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
779 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
780 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
782 * Check for the optional ALUA primary state transition delay
784 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
785 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
787 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
788 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
792 * After an implicit target port asymmetric access state
793 * change, a device server shall establish a unit attention
794 * condition for the initiator port associated with every I_T
795 * nexus with the additional sense code set to ASYMMETRIC
796 * ACCESS STATE CHAGED.
798 * After an explicit target port asymmetric access state
799 * change, a device server shall establish a unit attention
800 * condition with the additional sense code set to ASYMMETRIC
801 * ACCESS STATE CHANGED for the initiator port associated with
802 * every I_T nexus other than the I_T nexus on which the SET
803 * TARGET PORT GROUPS command
805 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
806 smp_mb__after_atomic_inc();
807 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
809 spin_lock_bh(&port->sep_alua_lock);
810 list_for_each_entry(se_deve, &port->sep_alua_list,
812 lacl = se_deve->se_lun_acl;
814 * se_deve->se_lun_acl pointer may be NULL for a
815 * entry created without explict Node+MappedLUN ACLs
821 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
822 (l_port != NULL) && (l_port == port))
825 core_scsi3_ua_allocate(lacl->se_lun_nacl,
826 se_deve->mapped_lun, 0x2A,
827 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
829 spin_unlock_bh(&port->sep_alua_lock);
831 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
832 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
833 smp_mb__after_atomic_dec();
835 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
837 * Update the ALUA metadata buf that has been allocated in
838 * core_alua_do_port_transition(), this metadata will be written
841 * Note that there is the case where we do not want to update the
842 * metadata when the saved metadata is being parsed in userspace
843 * when setting the existing port access state and access status.
845 * Also note that the failure to write out the ALUA metadata to
846 * struct file does NOT affect the actual ALUA transition.
848 if (tg_pt_gp->tg_pt_gp_write_metadata) {
849 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
850 core_alua_update_tpg_primary_metadata(tg_pt_gp,
852 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
855 * Set the current primary ALUA access state to the requested new state
857 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
859 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
860 " from primary access state %s to %s\n", (explict) ? "explict" :
861 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
862 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
863 core_alua_dump_state(new_state));
868 int core_alua_do_port_transition(
869 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
870 struct se_device *l_dev,
871 struct se_port *l_port,
872 struct se_node_acl *l_nacl,
876 struct se_device *dev;
877 struct se_port *port;
878 struct se_subsystem_dev *su_dev;
879 struct se_node_acl *nacl;
880 struct t10_alua_lu_gp *lu_gp;
881 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
882 struct t10_alua_tg_pt_gp *tg_pt_gp;
883 unsigned char *md_buf;
886 if (core_alua_check_transition(new_state, &primary) != 0)
889 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
891 pr_err("Unable to allocate buf for ALUA metadata\n");
895 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
896 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
897 lu_gp = local_lu_gp_mem->lu_gp;
898 atomic_inc(&lu_gp->lu_gp_ref_cnt);
899 smp_mb__after_atomic_inc();
900 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
902 * For storage objects that are members of the 'default_lu_gp',
903 * we only do transition on the passed *l_tp_pt_gp, and not
904 * on all of the matching target port groups IDs in default_lu_gp.
906 if (!lu_gp->lu_gp_id) {
908 * core_alua_do_transition_tg_pt() will always return
911 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
912 md_buf, new_state, explict);
913 atomic_dec(&lu_gp->lu_gp_ref_cnt);
914 smp_mb__after_atomic_dec();
919 * For all other LU groups aside from 'default_lu_gp', walk all of
920 * the associated storage objects looking for a matching target port
921 * group ID from the local target port group.
923 spin_lock(&lu_gp->lu_gp_lock);
924 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
927 dev = lu_gp_mem->lu_gp_mem_dev;
928 su_dev = dev->se_sub_dev;
929 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
930 smp_mb__after_atomic_inc();
931 spin_unlock(&lu_gp->lu_gp_lock);
933 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
934 list_for_each_entry(tg_pt_gp,
935 &su_dev->t10_alua.tg_pt_gps_list,
938 if (!tg_pt_gp->tg_pt_gp_valid_id)
941 * If the target behavior port asymmetric access state
942 * is changed for any target port group accessiable via
943 * a logical unit within a LU group, the target port
944 * behavior group asymmetric access states for the same
945 * target port group accessible via other logical units
946 * in that LU group will also change.
948 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
951 if (l_tg_pt_gp == tg_pt_gp) {
958 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
959 smp_mb__after_atomic_inc();
960 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
962 * core_alua_do_transition_tg_pt() will always return
965 core_alua_do_transition_tg_pt(tg_pt_gp, port,
966 nacl, md_buf, new_state, explict);
968 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
969 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
970 smp_mb__after_atomic_dec();
972 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
974 spin_lock(&lu_gp->lu_gp_lock);
975 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
976 smp_mb__after_atomic_dec();
978 spin_unlock(&lu_gp->lu_gp_lock);
980 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
981 " Group IDs: %hu %s transition to primary state: %s\n",
982 config_item_name(&lu_gp->lu_gp_group.cg_item),
983 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
984 core_alua_dump_state(new_state));
986 atomic_dec(&lu_gp->lu_gp_ref_cnt);
987 smp_mb__after_atomic_dec();
993 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
995 static int core_alua_update_tpg_secondary_metadata(
996 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
997 struct se_port *port,
998 unsigned char *md_buf,
1001 struct se_portal_group *se_tpg = port->sep_tpg;
1002 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1005 memset(path, 0, ALUA_METADATA_PATH_LEN);
1006 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1008 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1009 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1011 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1012 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1013 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1015 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1016 "alua_tg_pt_status=0x%02x\n",
1017 atomic_read(&port->sep_tg_pt_secondary_offline),
1018 port->sep_tg_pt_secondary_stat);
1020 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1021 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1022 port->sep_lun->unpacked_lun);
1024 return core_alua_write_tpg_metadata(path, md_buf, len);
1027 static int core_alua_set_tg_pt_secondary_state(
1028 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1029 struct se_port *port,
1033 struct t10_alua_tg_pt_gp *tg_pt_gp;
1034 unsigned char *md_buf;
1036 int trans_delay_msecs;
1038 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1039 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1041 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1042 pr_err("Unable to complete secondary state"
1046 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1048 * Set the secondary ALUA target port access state to OFFLINE
1049 * or release the previously secondary state for struct se_port
1052 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1054 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1056 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1057 port->sep_tg_pt_secondary_stat = (explict) ?
1058 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1059 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1061 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1062 " to secondary access state: %s\n", (explict) ? "explict" :
1063 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1064 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1066 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1068 * Do the optional transition delay after we set the secondary
1069 * ALUA access state.
1071 if (trans_delay_msecs != 0)
1072 msleep_interruptible(trans_delay_msecs);
1074 * See if we need to update the ALUA fabric port metadata for
1075 * secondary state and status
1077 if (port->sep_tg_pt_secondary_write_md) {
1078 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1080 pr_err("Unable to allocate md_buf for"
1081 " secondary ALUA access metadata\n");
1084 mutex_lock(&port->sep_tg_pt_md_mutex);
1085 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1086 md_buf, md_buf_len);
1087 mutex_unlock(&port->sep_tg_pt_md_mutex);
1095 struct t10_alua_lu_gp *
1096 core_alua_allocate_lu_gp(const char *name, int def_group)
1098 struct t10_alua_lu_gp *lu_gp;
1100 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1102 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1103 return ERR_PTR(-ENOMEM);
1105 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1106 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1107 spin_lock_init(&lu_gp->lu_gp_lock);
1108 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1111 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1112 lu_gp->lu_gp_valid_id = 1;
1113 alua_lu_gps_count++;
1119 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1121 struct t10_alua_lu_gp *lu_gp_tmp;
1124 * The lu_gp->lu_gp_id may only be set once..
1126 if (lu_gp->lu_gp_valid_id) {
1127 pr_warn("ALUA LU Group already has a valid ID,"
1128 " ignoring request\n");
1132 spin_lock(&lu_gps_lock);
1133 if (alua_lu_gps_count == 0x0000ffff) {
1134 pr_err("Maximum ALUA alua_lu_gps_count:"
1135 " 0x0000ffff reached\n");
1136 spin_unlock(&lu_gps_lock);
1137 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1141 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1142 alua_lu_gps_counter++;
1144 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1145 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1149 pr_warn("ALUA Logical Unit Group ID: %hu"
1150 " already exists, ignoring request\n",
1152 spin_unlock(&lu_gps_lock);
1157 lu_gp->lu_gp_id = lu_gp_id_tmp;
1158 lu_gp->lu_gp_valid_id = 1;
1159 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1160 alua_lu_gps_count++;
1161 spin_unlock(&lu_gps_lock);
1166 static struct t10_alua_lu_gp_member *
1167 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1169 struct t10_alua_lu_gp_member *lu_gp_mem;
1171 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1173 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1174 return ERR_PTR(-ENOMEM);
1176 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1177 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1178 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1180 lu_gp_mem->lu_gp_mem_dev = dev;
1181 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1186 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1188 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1190 * Once we have reached this point, config_item_put() has
1191 * already been called from target_core_alua_drop_lu_gp().
1193 * Here, we remove the *lu_gp from the global list so that
1194 * no associations can be made while we are releasing
1195 * struct t10_alua_lu_gp.
1197 spin_lock(&lu_gps_lock);
1198 list_del(&lu_gp->lu_gp_node);
1199 alua_lu_gps_count--;
1200 spin_unlock(&lu_gps_lock);
1202 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1203 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1204 * released with core_alua_put_lu_gp_from_name()
1206 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1209 * Release reference to struct t10_alua_lu_gp * from all associated
1212 spin_lock(&lu_gp->lu_gp_lock);
1213 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1214 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1215 if (lu_gp_mem->lu_gp_assoc) {
1216 list_del(&lu_gp_mem->lu_gp_mem_list);
1217 lu_gp->lu_gp_members--;
1218 lu_gp_mem->lu_gp_assoc = 0;
1220 spin_unlock(&lu_gp->lu_gp_lock);
1223 * lu_gp_mem is associated with a single
1224 * struct se_device->dev_alua_lu_gp_mem, and is released when
1225 * struct se_device is released via core_alua_free_lu_gp_mem().
1227 * If the passed lu_gp does NOT match the default_lu_gp, assume
1228 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1230 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1231 if (lu_gp != default_lu_gp)
1232 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1235 lu_gp_mem->lu_gp = NULL;
1236 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1238 spin_lock(&lu_gp->lu_gp_lock);
1240 spin_unlock(&lu_gp->lu_gp_lock);
1242 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1245 void core_alua_free_lu_gp_mem(struct se_device *dev)
1247 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1248 struct t10_alua *alua = &su_dev->t10_alua;
1249 struct t10_alua_lu_gp *lu_gp;
1250 struct t10_alua_lu_gp_member *lu_gp_mem;
1252 if (alua->alua_type != SPC3_ALUA_EMULATED)
1255 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1259 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1262 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1263 lu_gp = lu_gp_mem->lu_gp;
1265 spin_lock(&lu_gp->lu_gp_lock);
1266 if (lu_gp_mem->lu_gp_assoc) {
1267 list_del(&lu_gp_mem->lu_gp_mem_list);
1268 lu_gp->lu_gp_members--;
1269 lu_gp_mem->lu_gp_assoc = 0;
1271 spin_unlock(&lu_gp->lu_gp_lock);
1272 lu_gp_mem->lu_gp = NULL;
1274 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1276 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1279 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1281 struct t10_alua_lu_gp *lu_gp;
1282 struct config_item *ci;
1284 spin_lock(&lu_gps_lock);
1285 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1286 if (!lu_gp->lu_gp_valid_id)
1288 ci = &lu_gp->lu_gp_group.cg_item;
1289 if (!strcmp(config_item_name(ci), name)) {
1290 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1291 spin_unlock(&lu_gps_lock);
1295 spin_unlock(&lu_gps_lock);
1300 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1302 spin_lock(&lu_gps_lock);
1303 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1304 spin_unlock(&lu_gps_lock);
1308 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1310 void __core_alua_attach_lu_gp_mem(
1311 struct t10_alua_lu_gp_member *lu_gp_mem,
1312 struct t10_alua_lu_gp *lu_gp)
1314 spin_lock(&lu_gp->lu_gp_lock);
1315 lu_gp_mem->lu_gp = lu_gp;
1316 lu_gp_mem->lu_gp_assoc = 1;
1317 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1318 lu_gp->lu_gp_members++;
1319 spin_unlock(&lu_gp->lu_gp_lock);
1323 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1325 void __core_alua_drop_lu_gp_mem(
1326 struct t10_alua_lu_gp_member *lu_gp_mem,
1327 struct t10_alua_lu_gp *lu_gp)
1329 spin_lock(&lu_gp->lu_gp_lock);
1330 list_del(&lu_gp_mem->lu_gp_mem_list);
1331 lu_gp_mem->lu_gp = NULL;
1332 lu_gp_mem->lu_gp_assoc = 0;
1333 lu_gp->lu_gp_members--;
1334 spin_unlock(&lu_gp->lu_gp_lock);
1337 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
1338 struct se_subsystem_dev *su_dev,
1342 struct t10_alua_tg_pt_gp *tg_pt_gp;
1344 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1346 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
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_su_dev = su_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);
1359 * Enable both explict and implict ALUA support by default
1361 tg_pt_gp->tg_pt_gp_alua_access_type =
1362 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1364 * Set the default Active/NonOptimized Delay in milliseconds
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;
1370 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1371 tg_pt_gp->tg_pt_gp_id =
1372 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1373 tg_pt_gp->tg_pt_gp_valid_id = 1;
1374 su_dev->t10_alua.alua_tg_pt_gps_count++;
1375 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1376 &su_dev->t10_alua.tg_pt_gps_list);
1377 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1383 int core_alua_set_tg_pt_gp_id(
1384 struct t10_alua_tg_pt_gp *tg_pt_gp,
1387 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1388 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1389 u16 tg_pt_gp_id_tmp;
1391 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1393 if (tg_pt_gp->tg_pt_gp_valid_id) {
1394 pr_warn("ALUA TG PT Group already has a valid ID,"
1395 " ignoring request\n");
1399 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1400 if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1401 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1402 " 0x0000ffff reached\n");
1403 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1404 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1408 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1409 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1411 list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
1413 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1417 pr_err("ALUA Target Port Group ID: %hu already"
1418 " exists, ignoring request\n", tg_pt_gp_id);
1419 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1424 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1425 tg_pt_gp->tg_pt_gp_valid_id = 1;
1426 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1427 &su_dev->t10_alua.tg_pt_gps_list);
1428 su_dev->t10_alua.alua_tg_pt_gps_count++;
1429 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1434 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1435 struct se_port *port)
1437 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1439 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1441 if (!tg_pt_gp_mem) {
1442 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1443 return ERR_PTR(-ENOMEM);
1445 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1446 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1447 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1449 tg_pt_gp_mem->tg_pt = port;
1450 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1452 return tg_pt_gp_mem;
1455 void core_alua_free_tg_pt_gp(
1456 struct t10_alua_tg_pt_gp *tg_pt_gp)
1458 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1459 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1461 * Once we have reached this point, config_item_put() has already
1462 * been called from target_core_alua_drop_tg_pt_gp().
1464 * Here we remove *tg_pt_gp from the global list so that
1465 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1466 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1468 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1469 list_del(&tg_pt_gp->tg_pt_gp_list);
1470 su_dev->t10_alua.alua_tg_pt_gps_counter--;
1471 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1473 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1474 * core_alua_get_tg_pt_gp_by_name() in
1475 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1476 * to be released with core_alua_put_tg_pt_gp_from_name().
1478 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1481 * Release reference to struct t10_alua_tg_pt_gp from all associated
1484 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1485 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1486 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1487 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1488 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1489 tg_pt_gp->tg_pt_gp_members--;
1490 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1492 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1494 * tg_pt_gp_mem is associated with a single
1495 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1496 * core_alua_free_tg_pt_gp_mem().
1498 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1499 * assume we want to re-assocate a given tg_pt_gp_mem with
1502 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1503 if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
1504 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1505 su_dev->t10_alua.default_tg_pt_gp);
1507 tg_pt_gp_mem->tg_pt_gp = NULL;
1508 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1510 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1512 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1514 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1517 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1519 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1520 struct t10_alua *alua = &su_dev->t10_alua;
1521 struct t10_alua_tg_pt_gp *tg_pt_gp;
1522 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1524 if (alua->alua_type != SPC3_ALUA_EMULATED)
1527 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1531 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1534 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1535 tg_pt_gp = tg_pt_gp_mem->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;
1543 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1544 tg_pt_gp_mem->tg_pt_gp = NULL;
1546 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1548 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1551 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1552 struct se_subsystem_dev *su_dev,
1555 struct t10_alua_tg_pt_gp *tg_pt_gp;
1556 struct config_item *ci;
1558 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1559 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
1561 if (!tg_pt_gp->tg_pt_gp_valid_id)
1563 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1564 if (!strcmp(config_item_name(ci), name)) {
1565 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1566 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1570 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1575 static void core_alua_put_tg_pt_gp_from_name(
1576 struct t10_alua_tg_pt_gp *tg_pt_gp)
1578 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1580 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1581 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1582 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1586 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1588 void __core_alua_attach_tg_pt_gp_mem(
1589 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1590 struct t10_alua_tg_pt_gp *tg_pt_gp)
1592 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1593 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1594 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1595 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1596 &tg_pt_gp->tg_pt_gp_mem_list);
1597 tg_pt_gp->tg_pt_gp_members++;
1598 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1602 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1604 static void __core_alua_drop_tg_pt_gp_mem(
1605 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1606 struct t10_alua_tg_pt_gp *tg_pt_gp)
1608 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1609 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1610 tg_pt_gp_mem->tg_pt_gp = NULL;
1611 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1612 tg_pt_gp->tg_pt_gp_members--;
1613 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1616 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1618 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1619 struct config_item *tg_pt_ci;
1620 struct t10_alua *alua = &su_dev->t10_alua;
1621 struct t10_alua_tg_pt_gp *tg_pt_gp;
1622 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1625 if (alua->alua_type != SPC3_ALUA_EMULATED)
1628 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1632 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1633 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1635 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1636 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1637 " %hu\nTG Port Primary Access State: %s\nTG Port "
1638 "Primary Access Status: %s\nTG Port Secondary Access"
1639 " State: %s\nTG Port Secondary Access Status: %s\n",
1640 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1641 core_alua_dump_state(atomic_read(
1642 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1643 core_alua_dump_status(
1644 tg_pt_gp->tg_pt_gp_alua_access_status),
1645 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1647 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1649 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1654 ssize_t core_alua_store_tg_pt_gp_info(
1655 struct se_port *port,
1659 struct se_portal_group *tpg;
1661 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1662 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1663 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1664 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1667 tpg = port->sep_tpg;
1668 lun = port->sep_lun;
1670 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1671 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1672 " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1673 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1674 config_item_name(&lun->lun_group.cg_item));
1678 if (count > TG_PT_GROUP_NAME_BUF) {
1679 pr_err("ALUA Target Port Group alias too large!\n");
1682 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1683 memcpy(buf, page, count);
1685 * Any ALUA target port group alias besides "NULL" means we will be
1686 * making a new group association.
1688 if (strcmp(strstrip(buf), "NULL")) {
1690 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1691 * struct t10_alua_tg_pt_gp. This reference is released with
1692 * core_alua_put_tg_pt_gp_from_name() below.
1694 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
1699 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1700 if (!tg_pt_gp_mem) {
1702 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1703 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1707 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1708 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1711 * Clearing an existing tg_pt_gp association, and replacing
1712 * with the default_tg_pt_gp.
1714 if (!tg_pt_gp_new) {
1715 pr_debug("Target_Core_ConfigFS: Moving"
1716 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1717 " alua/%s, ID: %hu back to"
1718 " default_tg_pt_gp\n",
1719 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1720 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1721 config_item_name(&lun->lun_group.cg_item),
1723 &tg_pt_gp->tg_pt_gp_group.cg_item),
1724 tg_pt_gp->tg_pt_gp_id);
1726 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1727 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1728 su_dev->t10_alua.default_tg_pt_gp);
1729 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1734 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1736 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1740 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1742 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1743 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1744 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1745 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1746 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1747 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1748 config_item_name(&lun->lun_group.cg_item),
1749 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1750 tg_pt_gp_new->tg_pt_gp_id);
1752 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1756 ssize_t core_alua_show_access_type(
1757 struct t10_alua_tg_pt_gp *tg_pt_gp,
1760 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1761 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1762 return sprintf(page, "Implict and Explict\n");
1763 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1764 return sprintf(page, "Implict\n");
1765 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1766 return sprintf(page, "Explict\n");
1768 return sprintf(page, "None\n");
1771 ssize_t core_alua_store_access_type(
1772 struct t10_alua_tg_pt_gp *tg_pt_gp,
1779 ret = strict_strtoul(page, 0, &tmp);
1781 pr_err("Unable to extract alua_access_type\n");
1784 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1785 pr_err("Illegal value for alua_access_type:"
1790 tg_pt_gp->tg_pt_gp_alua_access_type =
1791 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1793 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1795 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1797 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1802 ssize_t core_alua_show_nonop_delay_msecs(
1803 struct t10_alua_tg_pt_gp *tg_pt_gp,
1806 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1809 ssize_t core_alua_store_nonop_delay_msecs(
1810 struct t10_alua_tg_pt_gp *tg_pt_gp,
1817 ret = strict_strtoul(page, 0, &tmp);
1819 pr_err("Unable to extract nonop_delay_msecs\n");
1822 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1823 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1824 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1825 ALUA_MAX_NONOP_DELAY_MSECS);
1828 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1833 ssize_t core_alua_show_trans_delay_msecs(
1834 struct t10_alua_tg_pt_gp *tg_pt_gp,
1837 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1840 ssize_t core_alua_store_trans_delay_msecs(
1841 struct t10_alua_tg_pt_gp *tg_pt_gp,
1848 ret = strict_strtoul(page, 0, &tmp);
1850 pr_err("Unable to extract trans_delay_msecs\n");
1853 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1854 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1855 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1856 ALUA_MAX_TRANS_DELAY_MSECS);
1859 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1864 ssize_t core_alua_show_preferred_bit(
1865 struct t10_alua_tg_pt_gp *tg_pt_gp,
1868 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1871 ssize_t core_alua_store_preferred_bit(
1872 struct t10_alua_tg_pt_gp *tg_pt_gp,
1879 ret = strict_strtoul(page, 0, &tmp);
1881 pr_err("Unable to extract preferred ALUA value\n");
1884 if ((tmp != 0) && (tmp != 1)) {
1885 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1888 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1893 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1898 return sprintf(page, "%d\n",
1899 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1902 ssize_t core_alua_store_offline_bit(
1907 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1914 ret = strict_strtoul(page, 0, &tmp);
1916 pr_err("Unable to extract alua_tg_pt_offline value\n");
1919 if ((tmp != 0) && (tmp != 1)) {
1920 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1924 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1925 if (!tg_pt_gp_mem) {
1926 pr_err("Unable to locate *tg_pt_gp_mem\n");
1930 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1931 lun->lun_sep, 0, (int)tmp);
1938 ssize_t core_alua_show_secondary_status(
1942 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1945 ssize_t core_alua_store_secondary_status(
1953 ret = strict_strtoul(page, 0, &tmp);
1955 pr_err("Unable to extract alua_tg_pt_status\n");
1958 if ((tmp != ALUA_STATUS_NONE) &&
1959 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1960 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1961 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1965 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1970 ssize_t core_alua_show_secondary_write_metadata(
1974 return sprintf(page, "%d\n",
1975 lun->lun_sep->sep_tg_pt_secondary_write_md);
1978 ssize_t core_alua_store_secondary_write_metadata(
1986 ret = strict_strtoul(page, 0, &tmp);
1988 pr_err("Unable to extract alua_tg_pt_write_md\n");
1991 if ((tmp != 0) && (tmp != 1)) {
1992 pr_err("Illegal value for alua_tg_pt_write_md:"
1996 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
2001 int core_setup_alua(struct se_device *dev, int force_pt)
2003 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2004 struct t10_alua *alua = &su_dev->t10_alua;
2005 struct t10_alua_lu_gp_member *lu_gp_mem;
2007 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
2008 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2009 * cause a problem because libata and some SATA RAID HBAs appear
2010 * under Linux/SCSI, but emulate SCSI logic themselves.
2012 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
2013 !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
2014 alua->alua_type = SPC_ALUA_PASSTHROUGH;
2015 alua->alua_state_check = &core_alua_state_check_nop;
2016 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2017 " emulation\n", dev->transport->name);
2021 * If SPC-3 or above is reported by real or emulated struct se_device,
2022 * use emulated ALUA.
2024 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
2025 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2026 " device\n", dev->transport->name);
2028 * Associate this struct se_device with the default ALUA
2031 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2032 if (IS_ERR(lu_gp_mem))
2033 return PTR_ERR(lu_gp_mem);
2035 alua->alua_type = SPC3_ALUA_EMULATED;
2036 alua->alua_state_check = &core_alua_state_check;
2037 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2038 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2040 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2042 pr_debug("%s: Adding to default ALUA LU Group:"
2043 " core/alua/lu_gps/default_lu_gp\n",
2044 dev->transport->name);
2046 alua->alua_type = SPC2_ALUA_DISABLED;
2047 alua->alua_state_check = &core_alua_state_check_nop;
2048 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2049 " device\n", dev->transport->name);