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/version.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/configfs.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_device.h>
36 #include <target/target_core_transport.h>
37 #include <target/target_core_fabric_ops.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_alua.h"
41 #include "target_core_hba.h"
42 #include "target_core_ua.h"
44 static int core_alua_check_transition(int state
, int *primary
);
45 static int core_alua_set_tg_pt_secondary_state(
46 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
47 struct se_port
*port
, int explict
, int offline
);
50 * REPORT_TARGET_PORT_GROUPS
52 * See spc4r17 section 6.27
54 int core_emulate_report_target_port_groups(struct se_cmd
*cmd
)
56 struct se_subsystem_dev
*su_dev
= SE_DEV(cmd
)->se_sub_dev
;
58 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
59 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
60 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
61 u32 rd_len
= 0, off
= 4; /* Skip over RESERVED area to first
62 Target port group descriptor */
64 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
65 list_for_each_entry(tg_pt_gp
, &T10_ALUA(su_dev
)->tg_pt_gps_list
,
68 * PREF: Preferred target port bit, determine if this
69 * bit should be set for port group.
71 if (tg_pt_gp
->tg_pt_gp_pref
)
74 * Set the ASYMMETRIC ACCESS State
76 buf
[off
++] |= (atomic_read(
77 &tg_pt_gp
->tg_pt_gp_alua_access_state
) & 0xff);
79 * Set supported ASYMMETRIC ACCESS State bits
81 buf
[off
] = 0x80; /* T_SUP */
82 buf
[off
] |= 0x40; /* O_SUP */
83 buf
[off
] |= 0x8; /* U_SUP */
84 buf
[off
] |= 0x4; /* S_SUP */
85 buf
[off
] |= 0x2; /* AN_SUP */
86 buf
[off
++] |= 0x1; /* AO_SUP */
90 buf
[off
++] = ((tg_pt_gp
->tg_pt_gp_id
>> 8) & 0xff);
91 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_id
& 0xff);
93 off
++; /* Skip over Reserved */
97 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
99 * Vendor Specific field
105 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
108 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
109 list_for_each_entry(tg_pt_gp_mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
111 port
= tg_pt_gp_mem
->tg_pt
;
113 * Start Target Port descriptor format
115 * See spc4r17 section 6.2.7 Table 247
117 off
+= 2; /* Skip over Obsolete */
119 * Set RELATIVE TARGET PORT IDENTIFIER
121 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
122 buf
[off
++] = (port
->sep_rtpi
& 0xff);
125 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
127 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
129 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
131 buf
[0] = ((rd_len
>> 24) & 0xff);
132 buf
[1] = ((rd_len
>> 16) & 0xff);
133 buf
[2] = ((rd_len
>> 8) & 0xff);
134 buf
[3] = (rd_len
& 0xff);
140 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
142 * See spc4r17 section 6.35
144 int core_emulate_set_target_port_groups(struct se_cmd
*cmd
)
146 struct se_device
*dev
= SE_DEV(cmd
);
147 struct se_subsystem_dev
*su_dev
= SE_DEV(cmd
)->se_sub_dev
;
148 struct se_port
*port
, *l_port
= SE_LUN(cmd
)->lun_sep
;
149 struct se_node_acl
*nacl
= SE_SESS(cmd
)->se_node_acl
;
150 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
151 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *l_tg_pt_gp_mem
;
152 unsigned char *buf
= (unsigned char *)T_TASK(cmd
)->t_task_buf
;
153 unsigned char *ptr
= &buf
[4]; /* Skip over RESERVED area in header */
154 u32 len
= 4; /* Skip over RESERVED area in header */
155 int alua_access_state
, primary
= 0, rc
;
159 return PYX_TRANSPORT_LU_COMM_FAILURE
;
161 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
162 * for the local tg_pt_gp.
164 l_tg_pt_gp_mem
= l_port
->sep_alua_tg_pt_gp_mem
;
165 if (!(l_tg_pt_gp_mem
)) {
166 printk(KERN_ERR
"Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
167 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
169 spin_lock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
170 l_tg_pt_gp
= l_tg_pt_gp_mem
->tg_pt_gp
;
172 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
173 printk(KERN_ERR
"Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
174 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
176 rc
= (l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
);
177 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
180 printk(KERN_INFO
"Unable to process SET_TARGET_PORT_GROUPS"
181 " while TPGS_EXPLICT_ALUA is disabled\n");
182 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
185 while (len
< cmd
->data_length
) {
186 alua_access_state
= (ptr
[0] & 0x0f);
188 * Check the received ALUA access state, and determine if
189 * the state is a primary or secondary target port asymmetric
192 rc
= core_alua_check_transition(alua_access_state
, &primary
);
195 * If the SET TARGET PORT GROUPS attempts to establish
196 * an invalid combination of target port asymmetric
197 * access states or attempts to establish an
198 * unsupported target port asymmetric access state,
199 * then the command shall be terminated with CHECK
200 * CONDITION status, with the sense key set to ILLEGAL
201 * REQUEST, and the additional sense code set to INVALID
202 * FIELD IN PARAMETER LIST.
204 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
208 * If the ASYMMETRIC ACCESS STATE field (see table 267)
209 * specifies a primary target port asymmetric access state,
210 * then the TARGET PORT GROUP OR TARGET PORT field specifies
211 * a primary target port group for which the primary target
212 * port asymmetric access state shall be changed. If the
213 * ASYMMETRIC ACCESS STATE field specifies a secondary target
214 * port asymmetric access state, then the TARGET PORT GROUP OR
215 * TARGET PORT field specifies the relative target port
216 * identifier (see 3.1.120) of the target port for which the
217 * secondary target port asymmetric access state shall be
221 tg_pt_id
= ((ptr
[2] << 8) & 0xff);
222 tg_pt_id
|= (ptr
[3] & 0xff);
224 * Locate the matching target port group ID from
225 * the global tg_pt_gp list
227 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
228 list_for_each_entry(tg_pt_gp
,
229 &T10_ALUA(su_dev
)->tg_pt_gps_list
,
231 if (!(tg_pt_gp
->tg_pt_gp_valid_id
))
234 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
237 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
238 smp_mb__after_atomic_inc();
239 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
241 rc
= core_alua_do_port_transition(tg_pt_gp
,
243 alua_access_state
, 1);
245 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
246 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
247 smp_mb__after_atomic_dec();
250 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
252 * If not matching target port group ID can be located
253 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
256 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
259 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
260 * the Target Port in question for the the incoming
261 * SET_TARGET_PORT_GROUPS op.
263 rtpi
= ((ptr
[2] << 8) & 0xff);
264 rtpi
|= (ptr
[3] & 0xff);
266 * Locate the matching relative target port identifer
267 * for the struct se_device storage object.
269 spin_lock(&dev
->se_port_lock
);
270 list_for_each_entry(port
, &dev
->dev_sep_list
,
272 if (port
->sep_rtpi
!= rtpi
)
275 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
276 spin_unlock(&dev
->se_port_lock
);
278 rc
= core_alua_set_tg_pt_secondary_state(
279 tg_pt_gp_mem
, port
, 1, 1);
281 spin_lock(&dev
->se_port_lock
);
284 spin_unlock(&dev
->se_port_lock
);
286 * If not matching relative target port identifier can
287 * be located, throw an exception with ASCQ:
288 * INVALID_PARAMETER_LIST
291 return PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
301 static inline int core_alua_state_nonoptimized(
304 int nonop_delay_msecs
,
308 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
309 * later to determine if processing of this cmd needs to be
310 * temporarily delayed for the Active/NonOptimized primary access state.
312 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
313 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
317 static inline int core_alua_state_standby(
323 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
324 * spc4r17 section 5.9.2.4.4
333 case RECEIVE_DIAGNOSTIC
:
334 case SEND_DIAGNOSTIC
:
337 case MI_REPORT_TARGET_PGS
:
340 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
343 case MAINTENANCE_OUT
:
345 case MO_SET_TARGET_PGS
:
348 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
352 case PERSISTENT_RESERVE_IN
:
353 case PERSISTENT_RESERVE_OUT
:
358 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
365 static inline int core_alua_state_unavailable(
371 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
372 * spc4r17 section 5.9.2.4.5
379 case MI_REPORT_TARGET_PGS
:
382 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
385 case MAINTENANCE_OUT
:
387 case MO_SET_TARGET_PGS
:
390 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
398 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
405 static inline int core_alua_state_transition(
411 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
412 * spc4r17 section 5.9.2.5
419 case MI_REPORT_TARGET_PGS
:
422 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
430 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
438 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
439 * in transport_cmd_sequencer(). This function is assigned to
440 * struct t10_alua *->state_check() in core_setup_alua()
442 static int core_alua_state_check_nop(
451 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
452 * This function is assigned to struct t10_alua *->state_check() in
455 * Also, this function can return three different return codes to
456 * signal transport_generic_cmd_sequencer()
458 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
459 * return 0: Used to signal success
460 * reutrn -1: Used to signal failure, and invalid cdb field
462 static int core_alua_state_check(
467 struct se_lun
*lun
= SE_LUN(cmd
);
468 struct se_port
*port
= lun
->lun_sep
;
469 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
470 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
471 int out_alua_state
, nonop_delay_msecs
;
476 * First, check for a struct se_port specific secondary ALUA target port
477 * access state: OFFLINE
479 if (atomic_read(&port
->sep_tg_pt_secondary_offline
)) {
480 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
481 printk(KERN_INFO
"ALUA: Got secondary offline status for local"
483 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
487 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
488 * ALUA target port group, to obtain current ALUA access state.
489 * Otherwise look for the underlying struct se_device association with
490 * a ALUA logical unit group.
492 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
493 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
494 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
495 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
496 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
497 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
499 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
500 * statement so the compiler knows explicitly to check this case first.
501 * For the Optimized ALUA access state case, we want to process the
502 * incoming fabric cmd ASAP..
504 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
)
507 switch (out_alua_state
) {
508 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
509 return core_alua_state_nonoptimized(cmd
, cdb
,
510 nonop_delay_msecs
, alua_ascq
);
511 case ALUA_ACCESS_STATE_STANDBY
:
512 return core_alua_state_standby(cmd
, cdb
, alua_ascq
);
513 case ALUA_ACCESS_STATE_UNAVAILABLE
:
514 return core_alua_state_unavailable(cmd
, cdb
, alua_ascq
);
515 case ALUA_ACCESS_STATE_TRANSITION
:
516 return core_alua_state_transition(cmd
, cdb
, alua_ascq
);
518 * OFFLINE is a secondary ALUA target port group access state, that is
519 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
521 case ALUA_ACCESS_STATE_OFFLINE
:
523 printk(KERN_ERR
"Unknown ALUA access state: 0x%02x\n",
532 * Check implict and explict ALUA state change request.
534 static int core_alua_check_transition(int state
, int *primary
)
537 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
538 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
539 case ALUA_ACCESS_STATE_STANDBY
:
540 case ALUA_ACCESS_STATE_UNAVAILABLE
:
542 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
543 * defined as primary target port asymmetric access states.
547 case ALUA_ACCESS_STATE_OFFLINE
:
549 * OFFLINE state is defined as a secondary target port
550 * asymmetric access state.
555 printk(KERN_ERR
"Unknown ALUA access state: 0x%02x\n", state
);
562 static char *core_alua_dump_state(int state
)
565 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
566 return "Active/Optimized";
567 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
568 return "Active/NonOptimized";
569 case ALUA_ACCESS_STATE_STANDBY
:
571 case ALUA_ACCESS_STATE_UNAVAILABLE
:
572 return "Unavailable";
573 case ALUA_ACCESS_STATE_OFFLINE
:
582 char *core_alua_dump_status(int status
)
585 case ALUA_STATUS_NONE
:
587 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
588 return "Altered by Explict STPG";
589 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
:
590 return "Altered by Implict ALUA";
599 * Used by fabric modules to determine when we need to delay processing
600 * for the Active/NonOptimized paths..
602 int core_alua_check_nonop_delay(
605 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
610 * The ALUA Active/NonOptimized access state delay can be disabled
611 * in via configfs with a value of zero
613 if (!(cmd
->alua_nonop_delay
))
616 * struct se_cmd->alua_nonop_delay gets set by a target port group
617 * defined interval in core_alua_state_nonoptimized()
619 msleep_interruptible(cmd
->alua_nonop_delay
);
622 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
625 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
628 static int core_alua_write_tpg_metadata(
630 unsigned char *md_buf
,
636 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
, ret
;
638 memset(iov
, 0, sizeof(struct iovec
));
640 file
= filp_open(path
, flags
, 0600);
641 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
642 printk(KERN_ERR
"filp_open(%s) for ALUA metadata failed\n",
647 iov
[0].iov_base
= &md_buf
[0];
648 iov
[0].iov_len
= md_buf_len
;
652 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
656 printk(KERN_ERR
"Error writing ALUA metadata file: %s\n", path
);
657 filp_close(file
, NULL
);
660 filp_close(file
, NULL
);
666 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
668 static int core_alua_update_tpg_primary_metadata(
669 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
671 unsigned char *md_buf
)
673 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
674 struct t10_wwn
*wwn
= &su_dev
->t10_wwn
;
675 char path
[ALUA_METADATA_PATH_LEN
];
678 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
680 len
= snprintf(md_buf
, tg_pt_gp
->tg_pt_gp_md_buf_len
,
682 "alua_access_state=0x%02x\n"
683 "alua_access_status=0x%02x\n",
684 tg_pt_gp
->tg_pt_gp_id
, primary_state
,
685 tg_pt_gp
->tg_pt_gp_alua_access_status
);
687 snprintf(path
, ALUA_METADATA_PATH_LEN
,
688 "/var/target/alua/tpgs_%s/%s", &wwn
->unit_serial
[0],
689 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
691 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
694 static int core_alua_do_transition_tg_pt(
695 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
696 struct se_port
*l_port
,
697 struct se_node_acl
*nacl
,
698 unsigned char *md_buf
,
702 struct se_dev_entry
*se_deve
;
703 struct se_lun_acl
*lacl
;
704 struct se_port
*port
;
705 struct t10_alua_tg_pt_gp_member
*mem
;
708 * Save the old primary ALUA access state, and set the current state
709 * to ALUA_ACCESS_STATE_TRANSITION.
711 old_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
712 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
713 ALUA_ACCESS_STATE_TRANSITION
);
714 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explict
) ?
715 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
716 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
718 * Check for the optional ALUA primary state transition delay
720 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
721 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
723 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
724 list_for_each_entry(mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
728 * After an implicit target port asymmetric access state
729 * change, a device server shall establish a unit attention
730 * condition for the initiator port associated with every I_T
731 * nexus with the additional sense code set to ASYMMETRIC
732 * ACCESS STATE CHAGED.
734 * After an explicit target port asymmetric access state
735 * change, a device server shall establish a unit attention
736 * condition with the additional sense code set to ASYMMETRIC
737 * ACCESS STATE CHANGED for the initiator port associated with
738 * every I_T nexus other than the I_T nexus on which the SET
739 * TARGET PORT GROUPS command
741 atomic_inc(&mem
->tg_pt_gp_mem_ref_cnt
);
742 smp_mb__after_atomic_inc();
743 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
745 spin_lock_bh(&port
->sep_alua_lock
);
746 list_for_each_entry(se_deve
, &port
->sep_alua_list
,
748 lacl
= se_deve
->se_lun_acl
;
750 * se_deve->se_lun_acl pointer may be NULL for a
751 * entry created without explict Node+MappedLUN ACLs
757 (nacl
!= NULL
) && (nacl
== lacl
->se_lun_nacl
) &&
758 (l_port
!= NULL
) && (l_port
== port
))
761 core_scsi3_ua_allocate(lacl
->se_lun_nacl
,
762 se_deve
->mapped_lun
, 0x2A,
763 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
765 spin_unlock_bh(&port
->sep_alua_lock
);
767 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
768 atomic_dec(&mem
->tg_pt_gp_mem_ref_cnt
);
769 smp_mb__after_atomic_dec();
771 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
773 * Update the ALUA metadata buf that has been allocated in
774 * core_alua_do_port_transition(), this metadata will be written
777 * Note that there is the case where we do not want to update the
778 * metadata when the saved metadata is being parsed in userspace
779 * when setting the existing port access state and access status.
781 * Also note that the failure to write out the ALUA metadata to
782 * struct file does NOT affect the actual ALUA transition.
784 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
785 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
786 core_alua_update_tpg_primary_metadata(tg_pt_gp
,
788 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
791 * Set the current primary ALUA access state to the requested new state
793 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
, new_state
);
795 printk(KERN_INFO
"Successful %s ALUA transition TG PT Group: %s ID: %hu"
796 " from primary access state %s to %s\n", (explict
) ? "explict" :
797 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
798 tg_pt_gp
->tg_pt_gp_id
, core_alua_dump_state(old_state
),
799 core_alua_dump_state(new_state
));
804 int core_alua_do_port_transition(
805 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
806 struct se_device
*l_dev
,
807 struct se_port
*l_port
,
808 struct se_node_acl
*l_nacl
,
812 struct se_device
*dev
;
813 struct se_port
*port
;
814 struct se_subsystem_dev
*su_dev
;
815 struct se_node_acl
*nacl
;
816 struct t10_alua_lu_gp
*lu_gp
;
817 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
818 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
819 unsigned char *md_buf
;
822 if (core_alua_check_transition(new_state
, &primary
) != 0)
825 md_buf
= kzalloc(l_tg_pt_gp
->tg_pt_gp_md_buf_len
, GFP_KERNEL
);
827 printk("Unable to allocate buf for ALUA metadata\n");
831 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
832 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
833 lu_gp
= local_lu_gp_mem
->lu_gp
;
834 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
835 smp_mb__after_atomic_inc();
836 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
838 * For storage objects that are members of the 'default_lu_gp',
839 * we only do transition on the passed *l_tp_pt_gp, and not
840 * on all of the matching target port groups IDs in default_lu_gp.
842 if (!(lu_gp
->lu_gp_id
)) {
844 * core_alua_do_transition_tg_pt() will always return
847 core_alua_do_transition_tg_pt(l_tg_pt_gp
, l_port
, l_nacl
,
848 md_buf
, new_state
, explict
);
849 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
850 smp_mb__after_atomic_dec();
855 * For all other LU groups aside from 'default_lu_gp', walk all of
856 * the associated storage objects looking for a matching target port
857 * group ID from the local target port group.
859 spin_lock(&lu_gp
->lu_gp_lock
);
860 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
863 dev
= lu_gp_mem
->lu_gp_mem_dev
;
864 su_dev
= dev
->se_sub_dev
;
865 atomic_inc(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
866 smp_mb__after_atomic_inc();
867 spin_unlock(&lu_gp
->lu_gp_lock
);
869 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
870 list_for_each_entry(tg_pt_gp
,
871 &T10_ALUA(su_dev
)->tg_pt_gps_list
,
874 if (!(tg_pt_gp
->tg_pt_gp_valid_id
))
877 * If the target behavior port asymmetric access state
878 * is changed for any target port group accessiable via
879 * a logical unit within a LU group, the target port
880 * behavior group asymmetric access states for the same
881 * target port group accessible via other logical units
882 * in that LU group will also change.
884 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
887 if (l_tg_pt_gp
== tg_pt_gp
) {
894 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
895 smp_mb__after_atomic_inc();
896 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
898 * core_alua_do_transition_tg_pt() will always return
901 core_alua_do_transition_tg_pt(tg_pt_gp
, port
,
902 nacl
, md_buf
, new_state
, explict
);
904 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
905 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
906 smp_mb__after_atomic_dec();
908 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
910 spin_lock(&lu_gp
->lu_gp_lock
);
911 atomic_dec(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
912 smp_mb__after_atomic_dec();
914 spin_unlock(&lu_gp
->lu_gp_lock
);
916 printk(KERN_INFO
"Successfully processed LU Group: %s all ALUA TG PT"
917 " Group IDs: %hu %s transition to primary state: %s\n",
918 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
919 l_tg_pt_gp
->tg_pt_gp_id
, (explict
) ? "explict" : "implict",
920 core_alua_dump_state(new_state
));
922 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
923 smp_mb__after_atomic_dec();
929 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
931 static int core_alua_update_tpg_secondary_metadata(
932 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
933 struct se_port
*port
,
934 unsigned char *md_buf
,
937 struct se_portal_group
*se_tpg
= port
->sep_tpg
;
938 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
941 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
942 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
944 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
945 TPG_TFO(se_tpg
)->tpg_get_wwn(se_tpg
));
947 if (TPG_TFO(se_tpg
)->tpg_get_tag
!= NULL
)
948 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
949 TPG_TFO(se_tpg
)->tpg_get_tag(se_tpg
));
951 len
= snprintf(md_buf
, md_buf_len
, "alua_tg_pt_offline=%d\n"
952 "alua_tg_pt_status=0x%02x\n",
953 atomic_read(&port
->sep_tg_pt_secondary_offline
),
954 port
->sep_tg_pt_secondary_stat
);
956 snprintf(path
, ALUA_METADATA_PATH_LEN
, "/var/target/alua/%s/%s/lun_%u",
957 TPG_TFO(se_tpg
)->get_fabric_name(), wwn
,
958 port
->sep_lun
->unpacked_lun
);
960 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
963 static int core_alua_set_tg_pt_secondary_state(
964 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
965 struct se_port
*port
,
969 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
970 unsigned char *md_buf
;
972 int trans_delay_msecs
;
974 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
975 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
977 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
978 printk(KERN_ERR
"Unable to complete secondary state"
982 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
984 * Set the secondary ALUA target port access state to OFFLINE
985 * or release the previously secondary state for struct se_port
988 atomic_set(&port
->sep_tg_pt_secondary_offline
, 1);
990 atomic_set(&port
->sep_tg_pt_secondary_offline
, 0);
992 md_buf_len
= tg_pt_gp
->tg_pt_gp_md_buf_len
;
993 port
->sep_tg_pt_secondary_stat
= (explict
) ?
994 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
995 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
997 printk(KERN_INFO
"Successful %s ALUA transition TG PT Group: %s ID: %hu"
998 " to secondary access state: %s\n", (explict
) ? "explict" :
999 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1000 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1002 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1004 * Do the optional transition delay after we set the secondary
1005 * ALUA access state.
1007 if (trans_delay_msecs
!= 0)
1008 msleep_interruptible(trans_delay_msecs
);
1010 * See if we need to update the ALUA fabric port metadata for
1011 * secondary state and status
1013 if (port
->sep_tg_pt_secondary_write_md
) {
1014 md_buf
= kzalloc(md_buf_len
, GFP_KERNEL
);
1016 printk(KERN_ERR
"Unable to allocate md_buf for"
1017 " secondary ALUA access metadata\n");
1020 mutex_lock(&port
->sep_tg_pt_md_mutex
);
1021 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem
, port
,
1022 md_buf
, md_buf_len
);
1023 mutex_unlock(&port
->sep_tg_pt_md_mutex
);
1031 struct t10_alua_lu_gp
*
1032 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1034 struct t10_alua_lu_gp
*lu_gp
;
1036 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1038 printk(KERN_ERR
"Unable to allocate struct t10_alua_lu_gp\n");
1039 return ERR_PTR(-ENOMEM
);
1041 INIT_LIST_HEAD(&lu_gp
->lu_gp_list
);
1042 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1043 spin_lock_init(&lu_gp
->lu_gp_lock
);
1044 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1047 lu_gp
->lu_gp_id
= se_global
->alua_lu_gps_counter
++;
1048 lu_gp
->lu_gp_valid_id
= 1;
1049 se_global
->alua_lu_gps_count
++;
1055 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1057 struct t10_alua_lu_gp
*lu_gp_tmp
;
1060 * The lu_gp->lu_gp_id may only be set once..
1062 if (lu_gp
->lu_gp_valid_id
) {
1063 printk(KERN_WARNING
"ALUA LU Group already has a valid ID,"
1064 " ignoring request\n");
1068 spin_lock(&se_global
->lu_gps_lock
);
1069 if (se_global
->alua_lu_gps_count
== 0x0000ffff) {
1070 printk(KERN_ERR
"Maximum ALUA se_global->alua_lu_gps_count:"
1071 " 0x0000ffff reached\n");
1072 spin_unlock(&se_global
->lu_gps_lock
);
1073 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1077 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1078 se_global
->alua_lu_gps_counter
++;
1080 list_for_each_entry(lu_gp_tmp
, &se_global
->g_lu_gps_list
, lu_gp_list
) {
1081 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1085 printk(KERN_WARNING
"ALUA Logical Unit Group ID: %hu"
1086 " already exists, ignoring request\n",
1088 spin_unlock(&se_global
->lu_gps_lock
);
1093 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1094 lu_gp
->lu_gp_valid_id
= 1;
1095 list_add_tail(&lu_gp
->lu_gp_list
, &se_global
->g_lu_gps_list
);
1096 se_global
->alua_lu_gps_count
++;
1097 spin_unlock(&se_global
->lu_gps_lock
);
1102 static struct t10_alua_lu_gp_member
*
1103 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1105 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1107 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1109 printk(KERN_ERR
"Unable to allocate struct t10_alua_lu_gp_member\n");
1110 return ERR_PTR(-ENOMEM
);
1112 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1113 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1114 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1116 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1117 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1122 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1124 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1126 * Once we have reached this point, config_item_put() has
1127 * already been called from target_core_alua_drop_lu_gp().
1129 * Here, we remove the *lu_gp from the global list so that
1130 * no associations can be made while we are releasing
1131 * struct t10_alua_lu_gp.
1133 spin_lock(&se_global
->lu_gps_lock
);
1134 atomic_set(&lu_gp
->lu_gp_shutdown
, 1);
1135 list_del(&lu_gp
->lu_gp_list
);
1136 se_global
->alua_lu_gps_count
--;
1137 spin_unlock(&se_global
->lu_gps_lock
);
1139 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1140 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1141 * released with core_alua_put_lu_gp_from_name()
1143 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1146 * Release reference to struct t10_alua_lu_gp * from all associated
1149 spin_lock(&lu_gp
->lu_gp_lock
);
1150 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1151 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1152 if (lu_gp_mem
->lu_gp_assoc
) {
1153 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1154 lu_gp
->lu_gp_members
--;
1155 lu_gp_mem
->lu_gp_assoc
= 0;
1157 spin_unlock(&lu_gp
->lu_gp_lock
);
1160 * lu_gp_mem is associated with a single
1161 * struct se_device->dev_alua_lu_gp_mem, and is released when
1162 * struct se_device is released via core_alua_free_lu_gp_mem().
1164 * If the passed lu_gp does NOT match the default_lu_gp, assume
1165 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1167 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1168 if (lu_gp
!= se_global
->default_lu_gp
)
1169 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1170 se_global
->default_lu_gp
);
1172 lu_gp_mem
->lu_gp
= NULL
;
1173 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1175 spin_lock(&lu_gp
->lu_gp_lock
);
1177 spin_unlock(&lu_gp
->lu_gp_lock
);
1179 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1182 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1184 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1185 struct t10_alua
*alua
= T10_ALUA(su_dev
);
1186 struct t10_alua_lu_gp
*lu_gp
;
1187 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1189 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1192 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1196 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1199 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1200 lu_gp
= lu_gp_mem
->lu_gp
;
1202 spin_lock(&lu_gp
->lu_gp_lock
);
1203 if (lu_gp_mem
->lu_gp_assoc
) {
1204 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1205 lu_gp
->lu_gp_members
--;
1206 lu_gp_mem
->lu_gp_assoc
= 0;
1208 spin_unlock(&lu_gp
->lu_gp_lock
);
1209 lu_gp_mem
->lu_gp
= NULL
;
1211 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1213 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1216 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1218 struct t10_alua_lu_gp
*lu_gp
;
1219 struct config_item
*ci
;
1221 spin_lock(&se_global
->lu_gps_lock
);
1222 list_for_each_entry(lu_gp
, &se_global
->g_lu_gps_list
, lu_gp_list
) {
1223 if (!(lu_gp
->lu_gp_valid_id
))
1225 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1226 if (!(strcmp(config_item_name(ci
), name
))) {
1227 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1228 spin_unlock(&se_global
->lu_gps_lock
);
1232 spin_unlock(&se_global
->lu_gps_lock
);
1237 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1239 spin_lock(&se_global
->lu_gps_lock
);
1240 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1241 spin_unlock(&se_global
->lu_gps_lock
);
1245 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1247 void __core_alua_attach_lu_gp_mem(
1248 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1249 struct t10_alua_lu_gp
*lu_gp
)
1251 spin_lock(&lu_gp
->lu_gp_lock
);
1252 lu_gp_mem
->lu_gp
= lu_gp
;
1253 lu_gp_mem
->lu_gp_assoc
= 1;
1254 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1255 lu_gp
->lu_gp_members
++;
1256 spin_unlock(&lu_gp
->lu_gp_lock
);
1260 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1262 void __core_alua_drop_lu_gp_mem(
1263 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1264 struct t10_alua_lu_gp
*lu_gp
)
1266 spin_lock(&lu_gp
->lu_gp_lock
);
1267 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1268 lu_gp_mem
->lu_gp
= NULL
;
1269 lu_gp_mem
->lu_gp_assoc
= 0;
1270 lu_gp
->lu_gp_members
--;
1271 spin_unlock(&lu_gp
->lu_gp_lock
);
1274 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(
1275 struct se_subsystem_dev
*su_dev
,
1279 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1281 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1283 printk(KERN_ERR
"Unable to allocate struct t10_alua_tg_pt_gp\n");
1286 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1287 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_mem_list
);
1288 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1289 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1290 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1291 tg_pt_gp
->tg_pt_gp_su_dev
= su_dev
;
1292 tg_pt_gp
->tg_pt_gp_md_buf_len
= ALUA_MD_BUF_LEN
;
1293 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1294 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
);
1296 * Enable both explict and implict ALUA support by default
1298 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1299 TPGS_EXPLICT_ALUA
| TPGS_IMPLICT_ALUA
;
1301 * Set the default Active/NonOptimized Delay in milliseconds
1303 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1304 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1307 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1308 tg_pt_gp
->tg_pt_gp_id
=
1309 T10_ALUA(su_dev
)->alua_tg_pt_gps_counter
++;
1310 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1311 T10_ALUA(su_dev
)->alua_tg_pt_gps_count
++;
1312 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1313 &T10_ALUA(su_dev
)->tg_pt_gps_list
);
1314 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1320 int core_alua_set_tg_pt_gp_id(
1321 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1324 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1325 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1326 u16 tg_pt_gp_id_tmp
;
1328 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1330 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1331 printk(KERN_WARNING
"ALUA TG PT Group already has a valid ID,"
1332 " ignoring request\n");
1336 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1337 if (T10_ALUA(su_dev
)->alua_tg_pt_gps_count
== 0x0000ffff) {
1338 printk(KERN_ERR
"Maximum ALUA alua_tg_pt_gps_count:"
1339 " 0x0000ffff reached\n");
1340 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1341 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1345 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1346 T10_ALUA(su_dev
)->alua_tg_pt_gps_counter
++;
1348 list_for_each_entry(tg_pt_gp_tmp
, &T10_ALUA(su_dev
)->tg_pt_gps_list
,
1350 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1354 printk(KERN_ERR
"ALUA Target Port Group ID: %hu already"
1355 " exists, ignoring request\n", tg_pt_gp_id
);
1356 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1361 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1362 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1363 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1364 &T10_ALUA(su_dev
)->tg_pt_gps_list
);
1365 T10_ALUA(su_dev
)->alua_tg_pt_gps_count
++;
1366 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1371 struct t10_alua_tg_pt_gp_member
*core_alua_allocate_tg_pt_gp_mem(
1372 struct se_port
*port
)
1374 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1376 tg_pt_gp_mem
= kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache
,
1378 if (!(tg_pt_gp_mem
)) {
1379 printk(KERN_ERR
"Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1380 return ERR_PTR(-ENOMEM
);
1382 INIT_LIST_HEAD(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1383 spin_lock_init(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1384 atomic_set(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
, 0);
1386 tg_pt_gp_mem
->tg_pt
= port
;
1387 port
->sep_alua_tg_pt_gp_mem
= tg_pt_gp_mem
;
1388 atomic_set(&port
->sep_tg_pt_gp_active
, 1);
1390 return tg_pt_gp_mem
;
1393 void core_alua_free_tg_pt_gp(
1394 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1396 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1397 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *tg_pt_gp_mem_tmp
;
1399 * Once we have reached this point, config_item_put() has already
1400 * been called from target_core_alua_drop_tg_pt_gp().
1402 * Here we remove *tg_pt_gp from the global list so that
1403 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1404 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1406 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1407 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1408 T10_ALUA(su_dev
)->alua_tg_pt_gps_counter
--;
1409 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1411 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1412 * core_alua_get_tg_pt_gp_by_name() in
1413 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1414 * to be released with core_alua_put_tg_pt_gp_from_name().
1416 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1419 * Release reference to struct t10_alua_tg_pt_gp from all associated
1422 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1423 list_for_each_entry_safe(tg_pt_gp_mem
, tg_pt_gp_mem_tmp
,
1424 &tg_pt_gp
->tg_pt_gp_mem_list
, tg_pt_gp_mem_list
) {
1425 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1426 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1427 tg_pt_gp
->tg_pt_gp_members
--;
1428 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1430 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1432 * tg_pt_gp_mem is associated with a single
1433 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1434 * core_alua_free_tg_pt_gp_mem().
1436 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1437 * assume we want to re-assocate a given tg_pt_gp_mem with
1440 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1441 if (tg_pt_gp
!= T10_ALUA(su_dev
)->default_tg_pt_gp
) {
1442 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1443 T10_ALUA(su_dev
)->default_tg_pt_gp
);
1445 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1446 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1448 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1450 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1452 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1455 void core_alua_free_tg_pt_gp_mem(struct se_port
*port
)
1457 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1458 struct t10_alua
*alua
= T10_ALUA(su_dev
);
1459 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1460 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1462 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1465 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1466 if (!(tg_pt_gp_mem
))
1469 while (atomic_read(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
))
1472 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1473 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1475 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1476 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1477 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1478 tg_pt_gp
->tg_pt_gp_members
--;
1479 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1481 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1482 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1484 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1486 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache
, tg_pt_gp_mem
);
1489 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1490 struct se_subsystem_dev
*su_dev
,
1493 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1494 struct config_item
*ci
;
1496 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1497 list_for_each_entry(tg_pt_gp
, &T10_ALUA(su_dev
)->tg_pt_gps_list
,
1499 if (!(tg_pt_gp
->tg_pt_gp_valid_id
))
1501 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1502 if (!(strcmp(config_item_name(ci
), name
))) {
1503 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1504 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1508 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1513 static void core_alua_put_tg_pt_gp_from_name(
1514 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1516 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1518 spin_lock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1519 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1520 spin_unlock(&T10_ALUA(su_dev
)->tg_pt_gps_lock
);
1524 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1526 void __core_alua_attach_tg_pt_gp_mem(
1527 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1528 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1530 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1531 tg_pt_gp_mem
->tg_pt_gp
= tg_pt_gp
;
1532 tg_pt_gp_mem
->tg_pt_gp_assoc
= 1;
1533 list_add_tail(&tg_pt_gp_mem
->tg_pt_gp_mem_list
,
1534 &tg_pt_gp
->tg_pt_gp_mem_list
);
1535 tg_pt_gp
->tg_pt_gp_members
++;
1536 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1540 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1542 static void __core_alua_drop_tg_pt_gp_mem(
1543 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1544 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1546 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1547 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1548 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1549 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1550 tg_pt_gp
->tg_pt_gp_members
--;
1551 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1554 ssize_t
core_alua_show_tg_pt_gp_info(struct se_port
*port
, char *page
)
1556 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1557 struct config_item
*tg_pt_ci
;
1558 struct t10_alua
*alua
= T10_ALUA(su_dev
);
1559 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1560 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1563 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1566 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1567 if (!(tg_pt_gp_mem
))
1570 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1571 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1573 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1574 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1575 " %hu\nTG Port Primary Access State: %s\nTG Port "
1576 "Primary Access Status: %s\nTG Port Secondary Access"
1577 " State: %s\nTG Port Secondary Access Status: %s\n",
1578 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1579 core_alua_dump_state(atomic_read(
1580 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
1581 core_alua_dump_status(
1582 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1583 (atomic_read(&port
->sep_tg_pt_secondary_offline
)) ?
1585 core_alua_dump_status(port
->sep_tg_pt_secondary_stat
));
1587 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1592 ssize_t
core_alua_store_tg_pt_gp_info(
1593 struct se_port
*port
,
1597 struct se_portal_group
*tpg
;
1599 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1600 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1601 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1602 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1605 tpg
= port
->sep_tpg
;
1606 lun
= port
->sep_lun
;
1608 if (T10_ALUA(su_dev
)->alua_type
!= SPC3_ALUA_EMULATED
) {
1609 printk(KERN_WARNING
"SPC3_ALUA_EMULATED not enabled for"
1610 " %s/tpgt_%hu/%s\n", TPG_TFO(tpg
)->tpg_get_wwn(tpg
),
1611 TPG_TFO(tpg
)->tpg_get_tag(tpg
),
1612 config_item_name(&lun
->lun_group
.cg_item
));
1616 if (count
> TG_PT_GROUP_NAME_BUF
) {
1617 printk(KERN_ERR
"ALUA Target Port Group alias too large!\n");
1620 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1621 memcpy(buf
, page
, count
);
1623 * Any ALUA target port group alias besides "NULL" means we will be
1624 * making a new group association.
1626 if (strcmp(strstrip(buf
), "NULL")) {
1628 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1629 * struct t10_alua_tg_pt_gp. This reference is released with
1630 * core_alua_put_tg_pt_gp_from_name() below.
1632 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(su_dev
,
1634 if (!(tg_pt_gp_new
))
1637 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1638 if (!(tg_pt_gp_mem
)) {
1640 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1641 printk(KERN_ERR
"NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1645 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1646 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1649 * Clearing an existing tg_pt_gp association, and replacing
1650 * with the default_tg_pt_gp.
1652 if (!(tg_pt_gp_new
)) {
1653 printk(KERN_INFO
"Target_Core_ConfigFS: Moving"
1654 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1655 " alua/%s, ID: %hu back to"
1656 " default_tg_pt_gp\n",
1657 TPG_TFO(tpg
)->tpg_get_wwn(tpg
),
1658 TPG_TFO(tpg
)->tpg_get_tag(tpg
),
1659 config_item_name(&lun
->lun_group
.cg_item
),
1661 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1662 tg_pt_gp
->tg_pt_gp_id
);
1664 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1665 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1666 T10_ALUA(su_dev
)->default_tg_pt_gp
);
1667 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1672 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1674 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1678 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1680 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp_new
);
1681 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1682 printk(KERN_INFO
"Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1683 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1684 "Moving" : "Adding", TPG_TFO(tpg
)->tpg_get_wwn(tpg
),
1685 TPG_TFO(tpg
)->tpg_get_tag(tpg
),
1686 config_item_name(&lun
->lun_group
.cg_item
),
1687 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
1688 tg_pt_gp_new
->tg_pt_gp_id
);
1690 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1694 ssize_t
core_alua_show_access_type(
1695 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1698 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
) &&
1699 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
))
1700 return sprintf(page
, "Implict and Explict\n");
1701 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
)
1702 return sprintf(page
, "Implict\n");
1703 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
)
1704 return sprintf(page
, "Explict\n");
1706 return sprintf(page
, "None\n");
1709 ssize_t
core_alua_store_access_type(
1710 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1717 ret
= strict_strtoul(page
, 0, &tmp
);
1719 printk(KERN_ERR
"Unable to extract alua_access_type\n");
1722 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
1723 printk(KERN_ERR
"Illegal value for alua_access_type:"
1728 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1729 TPGS_IMPLICT_ALUA
| TPGS_EXPLICT_ALUA
;
1731 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICT_ALUA
;
1733 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICT_ALUA
;
1735 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
1740 ssize_t
core_alua_show_nonop_delay_msecs(
1741 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1744 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
1747 ssize_t
core_alua_store_nonop_delay_msecs(
1748 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1755 ret
= strict_strtoul(page
, 0, &tmp
);
1757 printk(KERN_ERR
"Unable to extract nonop_delay_msecs\n");
1760 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
1761 printk(KERN_ERR
"Passed nonop_delay_msecs: %lu, exceeds"
1762 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
1763 ALUA_MAX_NONOP_DELAY_MSECS
);
1766 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
1771 ssize_t
core_alua_show_trans_delay_msecs(
1772 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1775 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1778 ssize_t
core_alua_store_trans_delay_msecs(
1779 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1786 ret
= strict_strtoul(page
, 0, &tmp
);
1788 printk(KERN_ERR
"Unable to extract trans_delay_msecs\n");
1791 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
1792 printk(KERN_ERR
"Passed trans_delay_msecs: %lu, exceeds"
1793 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
1794 ALUA_MAX_TRANS_DELAY_MSECS
);
1797 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
1802 ssize_t
core_alua_show_preferred_bit(
1803 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1806 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
1809 ssize_t
core_alua_store_preferred_bit(
1810 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1817 ret
= strict_strtoul(page
, 0, &tmp
);
1819 printk(KERN_ERR
"Unable to extract preferred ALUA value\n");
1822 if ((tmp
!= 0) && (tmp
!= 1)) {
1823 printk(KERN_ERR
"Illegal value for preferred ALUA: %lu\n", tmp
);
1826 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
1831 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
1833 if (!(lun
->lun_sep
))
1836 return sprintf(page
, "%d\n",
1837 atomic_read(&lun
->lun_sep
->sep_tg_pt_secondary_offline
));
1840 ssize_t
core_alua_store_offline_bit(
1845 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1849 if (!(lun
->lun_sep
))
1852 ret
= strict_strtoul(page
, 0, &tmp
);
1854 printk(KERN_ERR
"Unable to extract alua_tg_pt_offline value\n");
1857 if ((tmp
!= 0) && (tmp
!= 1)) {
1858 printk(KERN_ERR
"Illegal value for alua_tg_pt_offline: %lu\n",
1862 tg_pt_gp_mem
= lun
->lun_sep
->sep_alua_tg_pt_gp_mem
;
1863 if (!(tg_pt_gp_mem
)) {
1864 printk(KERN_ERR
"Unable to locate *tg_pt_gp_mem\n");
1868 ret
= core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem
,
1869 lun
->lun_sep
, 0, (int)tmp
);
1876 ssize_t
core_alua_show_secondary_status(
1880 return sprintf(page
, "%d\n", lun
->lun_sep
->sep_tg_pt_secondary_stat
);
1883 ssize_t
core_alua_store_secondary_status(
1891 ret
= strict_strtoul(page
, 0, &tmp
);
1893 printk(KERN_ERR
"Unable to extract alua_tg_pt_status\n");
1896 if ((tmp
!= ALUA_STATUS_NONE
) &&
1897 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
) &&
1898 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
)) {
1899 printk(KERN_ERR
"Illegal value for alua_tg_pt_status: %lu\n",
1903 lun
->lun_sep
->sep_tg_pt_secondary_stat
= (int)tmp
;
1908 ssize_t
core_alua_show_secondary_write_metadata(
1912 return sprintf(page
, "%d\n",
1913 lun
->lun_sep
->sep_tg_pt_secondary_write_md
);
1916 ssize_t
core_alua_store_secondary_write_metadata(
1924 ret
= strict_strtoul(page
, 0, &tmp
);
1926 printk(KERN_ERR
"Unable to extract alua_tg_pt_write_md\n");
1929 if ((tmp
!= 0) && (tmp
!= 1)) {
1930 printk(KERN_ERR
"Illegal value for alua_tg_pt_write_md:"
1934 lun
->lun_sep
->sep_tg_pt_secondary_write_md
= (int)tmp
;
1939 int core_setup_alua(struct se_device
*dev
, int force_pt
)
1941 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1942 struct t10_alua
*alua
= T10_ALUA(su_dev
);
1943 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1945 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
1946 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
1947 * cause a problem because libata and some SATA RAID HBAs appear
1948 * under Linux/SCSI, but emulate SCSI logic themselves.
1950 if (((TRANSPORT(dev
)->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
1951 !(DEV_ATTRIB(dev
)->emulate_alua
)) || force_pt
) {
1952 alua
->alua_type
= SPC_ALUA_PASSTHROUGH
;
1953 alua
->alua_state_check
= &core_alua_state_check_nop
;
1954 printk(KERN_INFO
"%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
1955 " emulation\n", TRANSPORT(dev
)->name
);
1959 * If SPC-3 or above is reported by real or emulated struct se_device,
1960 * use emulated ALUA.
1962 if (TRANSPORT(dev
)->get_device_rev(dev
) >= SCSI_3
) {
1963 printk(KERN_INFO
"%s: Enabling ALUA Emulation for SPC-3"
1964 " device\n", TRANSPORT(dev
)->name
);
1966 * Associate this struct se_device with the default ALUA
1969 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
1970 if (IS_ERR(lu_gp_mem
) || !lu_gp_mem
)
1973 alua
->alua_type
= SPC3_ALUA_EMULATED
;
1974 alua
->alua_state_check
= &core_alua_state_check
;
1975 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1976 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1977 se_global
->default_lu_gp
);
1978 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1980 printk(KERN_INFO
"%s: Adding to default ALUA LU Group:"
1981 " core/alua/lu_gps/default_lu_gp\n",
1982 TRANSPORT(dev
)->name
);
1984 alua
->alua_type
= SPC2_ALUA_DISABLED
;
1985 alua
->alua_state_check
= &core_alua_state_check_nop
;
1986 printk(KERN_INFO
"%s: Disabling ALUA Emulation for SPC-2"
1987 " device\n", TRANSPORT(dev
)->name
);