1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * (c) Copyright 2009-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/configfs.h>
29 #include <linux/export.h>
30 #include <linux/file.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
39 #include "target_core_internal.h"
40 #include "target_core_alua.h"
41 #include "target_core_ua.h"
43 static sense_reason_t
core_alua_check_transition(int state
, int valid
,
45 static int core_alua_set_tg_pt_secondary_state(
46 struct se_lun
*lun
, int explicit, int offline
);
48 static char *core_alua_dump_state(int state
);
50 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
51 struct t10_alua_tg_pt_gp
*tg_pt_gp
);
53 static u16 alua_lu_gps_counter
;
54 static u32 alua_lu_gps_count
;
56 static DEFINE_SPINLOCK(lu_gps_lock
);
57 static LIST_HEAD(lu_gps_list
);
59 struct t10_alua_lu_gp
*default_lu_gp
;
64 * See sbc3r35 section 5.23
67 target_emulate_report_referrals(struct se_cmd
*cmd
)
69 struct se_device
*dev
= cmd
->se_dev
;
70 struct t10_alua_lba_map
*map
;
71 struct t10_alua_lba_map_member
*map_mem
;
75 if (cmd
->data_length
< 4) {
76 pr_warn("REPORT REFERRALS allocation length %u too"
77 " small\n", cmd
->data_length
);
78 return TCM_INVALID_CDB_FIELD
;
81 buf
= transport_kmap_data_sg(cmd
);
83 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
86 spin_lock(&dev
->t10_alua
.lba_map_lock
);
87 if (list_empty(&dev
->t10_alua
.lba_map_list
)) {
88 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
89 transport_kunmap_data_sg(cmd
);
91 return TCM_UNSUPPORTED_SCSI_OPCODE
;
94 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
96 int desc_num
= off
+ 3;
100 if (cmd
->data_length
> off
)
101 put_unaligned_be64(map
->lba_map_first_lba
, &buf
[off
]);
103 if (cmd
->data_length
> off
)
104 put_unaligned_be64(map
->lba_map_last_lba
, &buf
[off
]);
108 list_for_each_entry(map_mem
, &map
->lba_map_mem_list
,
110 int alua_state
= map_mem
->lba_map_mem_alua_state
;
111 int alua_pg_id
= map_mem
->lba_map_mem_alua_pg_id
;
113 if (cmd
->data_length
> off
)
114 buf
[off
] = alua_state
& 0x0f;
116 if (cmd
->data_length
> off
)
117 buf
[off
] = (alua_pg_id
>> 8) & 0xff;
119 if (cmd
->data_length
> off
)
120 buf
[off
] = (alua_pg_id
& 0xff);
125 if (cmd
->data_length
> desc_num
)
126 buf
[desc_num
] = pg_num
;
128 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
131 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
133 put_unaligned_be16(rd_len
, &buf
[2]);
135 transport_kunmap_data_sg(cmd
);
137 target_complete_cmd(cmd
, GOOD
);
142 * REPORT_TARGET_PORT_GROUPS
144 * See spc4r17 section 6.27
147 target_emulate_report_target_port_groups(struct se_cmd
*cmd
)
149 struct se_device
*dev
= cmd
->se_dev
;
150 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
154 int ext_hdr
= (cmd
->t_task_cdb
[1] & 0x20);
157 * Skip over RESERVED area to first Target port group descriptor
158 * depending on the PARAMETER DATA FORMAT type..
165 if (cmd
->data_length
< off
) {
166 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
167 " small for %s header\n", cmd
->data_length
,
168 (ext_hdr
) ? "extended" : "normal");
169 return TCM_INVALID_CDB_FIELD
;
171 buf
= transport_kmap_data_sg(cmd
);
173 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
175 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
176 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
179 * Check if the Target port group and Target port descriptor list
180 * based on tg_pt_gp_members count will fit into the response payload.
181 * Otherwise, bump rd_len to let the initiator know we have exceeded
182 * the allocation length and the response is truncated.
184 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
186 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
190 * PREF: Preferred target port bit, determine if this
191 * bit should be set for port group.
193 if (tg_pt_gp
->tg_pt_gp_pref
)
196 * Set the ASYMMETRIC ACCESS State
198 buf
[off
++] |= (atomic_read(
199 &tg_pt_gp
->tg_pt_gp_alua_access_state
) & 0xff);
201 * Set supported ASYMMETRIC ACCESS State bits
203 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
207 buf
[off
++] = ((tg_pt_gp
->tg_pt_gp_id
>> 8) & 0xff);
208 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_id
& 0xff);
210 off
++; /* Skip over Reserved */
214 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
216 * Vendor Specific field
222 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
225 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
226 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
229 * Start Target Port descriptor format
231 * See spc4r17 section 6.2.7 Table 247
233 off
+= 2; /* Skip over Obsolete */
235 * Set RELATIVE TARGET PORT IDENTIFIER
237 buf
[off
++] = ((lun
->lun_rtpi
>> 8) & 0xff);
238 buf
[off
++] = (lun
->lun_rtpi
& 0xff);
241 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
243 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
245 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
247 put_unaligned_be32(rd_len
, &buf
[0]);
250 * Fill in the Extended header parameter data format if requested
255 * Set the implicit transition time (in seconds) for the application
256 * client to use as a base for it's transition timeout value.
258 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
259 * this CDB was received upon to determine this value individually
260 * for ALUA target port group.
262 spin_lock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
263 tg_pt_gp
= cmd
->se_lun
->lun_tg_pt_gp
;
265 buf
[5] = tg_pt_gp
->tg_pt_gp_implicit_trans_secs
;
266 spin_unlock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
268 transport_kunmap_data_sg(cmd
);
270 target_complete_cmd(cmd
, GOOD
);
275 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
277 * See spc4r17 section 6.35
280 target_emulate_set_target_port_groups(struct se_cmd
*cmd
)
282 struct se_device
*dev
= cmd
->se_dev
;
283 struct se_lun
*l_lun
= cmd
->se_lun
;
284 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
285 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
288 sense_reason_t rc
= TCM_NO_SENSE
;
289 u32 len
= 4; /* Skip over RESERVED area in header */
290 int alua_access_state
, primary
= 0, valid_states
;
293 if (cmd
->data_length
< 4) {
294 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
295 " small\n", cmd
->data_length
);
296 return TCM_INVALID_PARAMETER_LIST
;
299 buf
= transport_kmap_data_sg(cmd
);
301 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
304 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
305 * for the local tg_pt_gp.
307 spin_lock(&l_lun
->lun_tg_pt_gp_lock
);
308 l_tg_pt_gp
= l_lun
->lun_tg_pt_gp
;
310 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
311 pr_err("Unable to access l_lun->tg_pt_gp\n");
312 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
316 if (!(l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)) {
317 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
318 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
319 " while TPGS_EXPLICIT_ALUA is disabled\n");
320 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
323 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
324 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
326 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
328 while (len
< cmd
->data_length
) {
330 alua_access_state
= (ptr
[0] & 0x0f);
332 * Check the received ALUA access state, and determine if
333 * the state is a primary or secondary target port asymmetric
336 rc
= core_alua_check_transition(alua_access_state
,
337 valid_states
, &primary
);
340 * If the SET TARGET PORT GROUPS attempts to establish
341 * an invalid combination of target port asymmetric
342 * access states or attempts to establish an
343 * unsupported target port asymmetric access state,
344 * then the command shall be terminated with CHECK
345 * CONDITION status, with the sense key set to ILLEGAL
346 * REQUEST, and the additional sense code set to INVALID
347 * FIELD IN PARAMETER LIST.
353 * If the ASYMMETRIC ACCESS STATE field (see table 267)
354 * specifies a primary target port asymmetric access state,
355 * then the TARGET PORT GROUP OR TARGET PORT field specifies
356 * a primary target port group for which the primary target
357 * port asymmetric access state shall be changed. If the
358 * ASYMMETRIC ACCESS STATE field specifies a secondary target
359 * port asymmetric access state, then the TARGET PORT GROUP OR
360 * TARGET PORT field specifies the relative target port
361 * identifier (see 3.1.120) of the target port for which the
362 * secondary target port asymmetric access state shall be
366 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
368 * Locate the matching target port group ID from
369 * the global tg_pt_gp list
371 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
372 list_for_each_entry(tg_pt_gp
,
373 &dev
->t10_alua
.tg_pt_gps_list
,
375 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
378 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
381 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
383 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
385 if (!core_alua_do_port_transition(tg_pt_gp
,
387 alua_access_state
, 1))
390 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
391 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
394 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
399 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
400 * the Target Port in question for the the incoming
401 * SET_TARGET_PORT_GROUPS op.
403 rtpi
= get_unaligned_be16(ptr
+ 2);
405 * Locate the matching relative target port identifier
406 * for the struct se_device storage object.
408 spin_lock(&dev
->se_port_lock
);
409 list_for_each_entry(lun
, &dev
->dev_sep_list
,
411 if (lun
->lun_rtpi
!= rtpi
)
415 spin_unlock(&dev
->se_port_lock
);
417 if (!core_alua_set_tg_pt_secondary_state(
421 spin_lock(&dev
->se_port_lock
);
424 spin_unlock(&dev
->se_port_lock
);
428 rc
= TCM_INVALID_PARAMETER_LIST
;
437 transport_kunmap_data_sg(cmd
);
439 target_complete_cmd(cmd
, GOOD
);
443 static inline void set_ascq(struct se_cmd
*cmd
, u8 alua_ascq
)
446 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
447 * The ALUA additional sense code qualifier (ASCQ) is determined
448 * by the ALUA primary or secondary access state..
450 pr_debug("[%s]: ALUA TG Port not available, "
451 "SenseKey: NOT_READY, ASC/ASCQ: "
453 cmd
->se_tfo
->get_fabric_name(), alua_ascq
);
455 cmd
->scsi_asc
= 0x04;
456 cmd
->scsi_ascq
= alua_ascq
;
459 static inline void core_alua_state_nonoptimized(
462 int nonop_delay_msecs
)
465 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
466 * later to determine if processing of this cmd needs to be
467 * temporarily delayed for the Active/NonOptimized primary access state.
469 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
470 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
473 static inline int core_alua_state_lba_dependent(
475 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
477 struct se_device
*dev
= cmd
->se_dev
;
478 u64 segment_size
, segment_mult
, sectors
, lba
;
480 /* Only need to check for cdb actually containing LBAs */
481 if (!(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
))
484 spin_lock(&dev
->t10_alua
.lba_map_lock
);
485 segment_size
= dev
->t10_alua
.lba_map_segment_size
;
486 segment_mult
= dev
->t10_alua
.lba_map_segment_multiplier
;
487 sectors
= cmd
->data_length
/ dev
->dev_attrib
.block_size
;
489 lba
= cmd
->t_task_lba
;
490 while (lba
< cmd
->t_task_lba
+ sectors
) {
491 struct t10_alua_lba_map
*cur_map
= NULL
, *map
;
492 struct t10_alua_lba_map_member
*map_mem
;
494 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
496 u64 start_lba
, last_lba
;
497 u64 first_lba
= map
->lba_map_first_lba
;
501 start_lba
= do_div(tmp
, segment_size
* segment_mult
);
503 last_lba
= first_lba
+ segment_size
- 1;
504 if (start_lba
>= first_lba
&&
505 start_lba
<= last_lba
) {
511 last_lba
= map
->lba_map_last_lba
;
512 if (lba
>= first_lba
&& lba
<= last_lba
) {
520 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
521 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
524 list_for_each_entry(map_mem
, &cur_map
->lba_map_mem_list
,
526 if (map_mem
->lba_map_mem_alua_pg_id
!=
527 tg_pt_gp
->tg_pt_gp_id
)
529 switch(map_mem
->lba_map_mem_alua_state
) {
530 case ALUA_ACCESS_STATE_STANDBY
:
531 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
532 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
534 case ALUA_ACCESS_STATE_UNAVAILABLE
:
535 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
536 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
543 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
547 static inline int core_alua_state_standby(
552 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
553 * spc4r17 section 5.9.2.4.4
562 case RECEIVE_DIAGNOSTIC
:
563 case SEND_DIAGNOSTIC
:
566 case SERVICE_ACTION_IN_16
:
567 switch (cdb
[1] & 0x1f) {
568 case SAI_READ_CAPACITY_16
:
571 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
575 switch (cdb
[1] & 0x1f) {
576 case MI_REPORT_TARGET_PGS
:
579 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
582 case MAINTENANCE_OUT
:
584 case MO_SET_TARGET_PGS
:
587 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
591 case PERSISTENT_RESERVE_IN
:
592 case PERSISTENT_RESERVE_OUT
:
597 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
604 static inline int core_alua_state_unavailable(
609 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
610 * spc4r17 section 5.9.2.4.5
617 switch (cdb
[1] & 0x1f) {
618 case MI_REPORT_TARGET_PGS
:
621 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
624 case MAINTENANCE_OUT
:
626 case MO_SET_TARGET_PGS
:
629 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
637 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
644 static inline int core_alua_state_transition(
649 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
650 * spc4r17 section 5.9.2.5
657 switch (cdb
[1] & 0x1f) {
658 case MI_REPORT_TARGET_PGS
:
661 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
669 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
677 * return 1: Is used to signal LUN not accessible, and check condition/not ready
678 * return 0: Used to signal success
679 * return -1: Used to signal failure, and invalid cdb field
682 target_alua_state_check(struct se_cmd
*cmd
)
684 struct se_device
*dev
= cmd
->se_dev
;
685 unsigned char *cdb
= cmd
->t_task_cdb
;
686 struct se_lun
*lun
= cmd
->se_lun
;
687 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
688 int out_alua_state
, nonop_delay_msecs
;
690 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
692 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
696 * First, check for a struct se_port specific secondary ALUA target port
697 * access state: OFFLINE
699 if (atomic_read(&lun
->lun_tg_pt_secondary_offline
)) {
700 pr_debug("ALUA: Got secondary offline status for local"
702 set_ascq(cmd
, ASCQ_04H_ALUA_OFFLINE
);
703 return TCM_CHECK_CONDITION_NOT_READY
;
706 if (!lun
->lun_tg_pt_gp
)
709 spin_lock(&lun
->lun_tg_pt_gp_lock
);
710 tg_pt_gp
= lun
->lun_tg_pt_gp
;
711 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
712 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
714 // XXX: keeps using tg_pt_gp witout reference after unlock
715 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
717 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
718 * statement so the compiler knows explicitly to check this case first.
719 * For the Optimized ALUA access state case, we want to process the
720 * incoming fabric cmd ASAP..
722 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
)
725 switch (out_alua_state
) {
726 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
727 core_alua_state_nonoptimized(cmd
, cdb
, nonop_delay_msecs
);
729 case ALUA_ACCESS_STATE_STANDBY
:
730 if (core_alua_state_standby(cmd
, cdb
))
731 return TCM_CHECK_CONDITION_NOT_READY
;
733 case ALUA_ACCESS_STATE_UNAVAILABLE
:
734 if (core_alua_state_unavailable(cmd
, cdb
))
735 return TCM_CHECK_CONDITION_NOT_READY
;
737 case ALUA_ACCESS_STATE_TRANSITION
:
738 if (core_alua_state_transition(cmd
, cdb
))
739 return TCM_CHECK_CONDITION_NOT_READY
;
741 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
742 if (core_alua_state_lba_dependent(cmd
, tg_pt_gp
))
743 return TCM_CHECK_CONDITION_NOT_READY
;
746 * OFFLINE is a secondary ALUA target port group access state, that is
747 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
749 case ALUA_ACCESS_STATE_OFFLINE
:
751 pr_err("Unknown ALUA access state: 0x%02x\n",
753 return TCM_INVALID_CDB_FIELD
;
760 * Check implicit and explicit ALUA state change request.
762 static sense_reason_t
763 core_alua_check_transition(int state
, int valid
, int *primary
)
766 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
767 * defined as primary target port asymmetric access states.
770 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
771 if (!(valid
& ALUA_AO_SUP
))
775 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
776 if (!(valid
& ALUA_AN_SUP
))
780 case ALUA_ACCESS_STATE_STANDBY
:
781 if (!(valid
& ALUA_S_SUP
))
785 case ALUA_ACCESS_STATE_UNAVAILABLE
:
786 if (!(valid
& ALUA_U_SUP
))
790 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
791 if (!(valid
& ALUA_LBD_SUP
))
795 case ALUA_ACCESS_STATE_OFFLINE
:
797 * OFFLINE state is defined as a secondary target port
798 * asymmetric access state.
800 if (!(valid
& ALUA_O_SUP
))
804 case ALUA_ACCESS_STATE_TRANSITION
:
806 * Transitioning is set internally, and
807 * cannot be selected manually.
811 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
812 return TCM_INVALID_PARAMETER_LIST
;
818 pr_err("ALUA access state %s not supported",
819 core_alua_dump_state(state
));
820 return TCM_INVALID_PARAMETER_LIST
;
823 static char *core_alua_dump_state(int state
)
826 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
827 return "Active/Optimized";
828 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
829 return "Active/NonOptimized";
830 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
831 return "LBA Dependent";
832 case ALUA_ACCESS_STATE_STANDBY
:
834 case ALUA_ACCESS_STATE_UNAVAILABLE
:
835 return "Unavailable";
836 case ALUA_ACCESS_STATE_OFFLINE
:
838 case ALUA_ACCESS_STATE_TRANSITION
:
839 return "Transitioning";
847 char *core_alua_dump_status(int status
)
850 case ALUA_STATUS_NONE
:
852 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
853 return "Altered by Explicit STPG";
854 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
:
855 return "Altered by Implicit ALUA";
864 * Used by fabric modules to determine when we need to delay processing
865 * for the Active/NonOptimized paths..
867 int core_alua_check_nonop_delay(
870 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
875 * The ALUA Active/NonOptimized access state delay can be disabled
876 * in via configfs with a value of zero
878 if (!cmd
->alua_nonop_delay
)
881 * struct se_cmd->alua_nonop_delay gets set by a target port group
882 * defined interval in core_alua_state_nonoptimized()
884 msleep_interruptible(cmd
->alua_nonop_delay
);
887 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
889 static int core_alua_write_tpg_metadata(
891 unsigned char *md_buf
,
894 struct file
*file
= filp_open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
898 pr_err("filp_open(%s) for ALUA metadata failed\n", path
);
901 ret
= kernel_write(file
, md_buf
, md_buf_len
, 0);
903 pr_err("Error writing ALUA metadata file: %s\n", path
);
905 return (ret
< 0) ? -EIO
: 0;
909 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
911 static int core_alua_update_tpg_primary_metadata(
912 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
914 unsigned char *md_buf
;
915 struct t10_wwn
*wwn
= &tg_pt_gp
->tg_pt_gp_dev
->t10_wwn
;
916 char path
[ALUA_METADATA_PATH_LEN
];
919 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
921 pr_err("Unable to allocate buf for ALUA metadata\n");
925 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
927 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
,
929 "alua_access_state=0x%02x\n"
930 "alua_access_status=0x%02x\n",
931 tg_pt_gp
->tg_pt_gp_id
,
932 tg_pt_gp
->tg_pt_gp_alua_pending_state
,
933 tg_pt_gp
->tg_pt_gp_alua_access_status
);
935 snprintf(path
, ALUA_METADATA_PATH_LEN
,
936 "/var/target/alua/tpgs_%s/%s", &wwn
->unit_serial
[0],
937 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
939 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
944 static void core_alua_do_transition_tg_pt_work(struct work_struct
*work
)
946 struct t10_alua_tg_pt_gp
*tg_pt_gp
= container_of(work
,
947 struct t10_alua_tg_pt_gp
, tg_pt_gp_transition_work
.work
);
948 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
949 struct se_dev_entry
*se_deve
;
951 struct se_lun_acl
*lacl
;
952 bool explicit = (tg_pt_gp
->tg_pt_gp_alua_access_status
==
953 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
);
955 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
956 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
959 * After an implicit target port asymmetric access state
960 * change, a device server shall establish a unit attention
961 * condition for the initiator port associated with every I_T
962 * nexus with the additional sense code set to ASYMMETRIC
963 * ACCESS STATE CHANGED.
965 * After an explicit target port asymmetric access state
966 * change, a device server shall establish a unit attention
967 * condition with the additional sense code set to ASYMMETRIC
968 * ACCESS STATE CHANGED for the initiator port associated with
969 * every I_T nexus other than the I_T nexus on which the SET
970 * TARGET PORT GROUPS command
972 if (!percpu_ref_tryget_live(&lun
->lun_ref
))
974 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
976 spin_lock_bh(&lun
->lun_deve_lock
);
977 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
) {
978 lacl
= rcu_dereference_check(se_deve
->se_lun_acl
,
979 lockdep_is_held(&lun
->lun_deve_lock
));
981 * se_deve->se_lun_acl pointer may be NULL for a
982 * entry created without explicit Node+MappedLUN ACLs
987 if ((tg_pt_gp
->tg_pt_gp_alua_access_status
==
988 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
989 (tg_pt_gp
->tg_pt_gp_alua_nacl
!= NULL
) &&
990 (tg_pt_gp
->tg_pt_gp_alua_nacl
== lacl
->se_lun_nacl
) &&
991 (tg_pt_gp
->tg_pt_gp_alua_lun
!= NULL
) &&
992 (tg_pt_gp
->tg_pt_gp_alua_lun
== lun
))
995 core_scsi3_ua_allocate(lacl
->se_lun_nacl
,
996 se_deve
->mapped_lun
, 0x2A,
997 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
999 spin_unlock_bh(&lun
->lun_deve_lock
);
1001 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1002 percpu_ref_put(&lun
->lun_ref
);
1004 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1006 * Update the ALUA metadata buf that has been allocated in
1007 * core_alua_do_port_transition(), this metadata will be written
1010 * Note that there is the case where we do not want to update the
1011 * metadata when the saved metadata is being parsed in userspace
1012 * when setting the existing port access state and access status.
1014 * Also note that the failure to write out the ALUA metadata to
1015 * struct file does NOT affect the actual ALUA transition.
1017 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
1018 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1019 core_alua_update_tpg_primary_metadata(tg_pt_gp
);
1020 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1023 * Set the current primary ALUA access state to the requested new state
1025 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1026 tg_pt_gp
->tg_pt_gp_alua_pending_state
);
1028 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1029 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1030 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1031 tg_pt_gp
->tg_pt_gp_id
,
1032 core_alua_dump_state(tg_pt_gp
->tg_pt_gp_alua_previous_state
),
1033 core_alua_dump_state(tg_pt_gp
->tg_pt_gp_alua_pending_state
));
1034 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1035 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1036 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1038 if (tg_pt_gp
->tg_pt_gp_transition_complete
)
1039 complete(tg_pt_gp
->tg_pt_gp_transition_complete
);
1042 static int core_alua_do_transition_tg_pt(
1043 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1047 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1048 DECLARE_COMPLETION_ONSTACK(wait
);
1050 /* Nothing to be done here */
1051 if (atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
) == new_state
)
1054 if (new_state
== ALUA_ACCESS_STATE_TRANSITION
)
1058 * Flush any pending transitions
1060 if (!explicit && tg_pt_gp
->tg_pt_gp_implicit_trans_secs
&&
1061 atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
) ==
1062 ALUA_ACCESS_STATE_TRANSITION
) {
1064 tg_pt_gp
->tg_pt_gp_alua_pending_state
= new_state
;
1065 tg_pt_gp
->tg_pt_gp_transition_complete
= &wait
;
1066 flush_delayed_work(&tg_pt_gp
->tg_pt_gp_transition_work
);
1067 wait_for_completion(&wait
);
1068 tg_pt_gp
->tg_pt_gp_transition_complete
= NULL
;
1073 * Save the old primary ALUA access state, and set the current state
1074 * to ALUA_ACCESS_STATE_TRANSITION.
1076 tg_pt_gp
->tg_pt_gp_alua_previous_state
=
1077 atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
1078 tg_pt_gp
->tg_pt_gp_alua_pending_state
= new_state
;
1080 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1081 ALUA_ACCESS_STATE_TRANSITION
);
1082 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explicit) ?
1083 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1084 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1087 * Check for the optional ALUA primary state transition delay
1089 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
1090 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1093 * Take a reference for workqueue item
1095 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1096 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1097 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1099 if (!explicit && tg_pt_gp
->tg_pt_gp_implicit_trans_secs
) {
1100 unsigned long transition_tmo
;
1102 transition_tmo
= tg_pt_gp
->tg_pt_gp_implicit_trans_secs
* HZ
;
1103 queue_delayed_work(tg_pt_gp
->tg_pt_gp_dev
->tmr_wq
,
1104 &tg_pt_gp
->tg_pt_gp_transition_work
,
1107 tg_pt_gp
->tg_pt_gp_transition_complete
= &wait
;
1108 queue_delayed_work(tg_pt_gp
->tg_pt_gp_dev
->tmr_wq
,
1109 &tg_pt_gp
->tg_pt_gp_transition_work
, 0);
1110 wait_for_completion(&wait
);
1111 tg_pt_gp
->tg_pt_gp_transition_complete
= NULL
;
1117 int core_alua_do_port_transition(
1118 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
1119 struct se_device
*l_dev
,
1120 struct se_lun
*l_lun
,
1121 struct se_node_acl
*l_nacl
,
1125 struct se_device
*dev
;
1126 struct t10_alua_lu_gp
*lu_gp
;
1127 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
1128 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1129 int primary
, valid_states
, rc
= 0;
1131 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1132 if (core_alua_check_transition(new_state
, valid_states
, &primary
) != 0)
1135 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
1136 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1137 lu_gp
= local_lu_gp_mem
->lu_gp
;
1138 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1139 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1141 * For storage objects that are members of the 'default_lu_gp',
1142 * we only do transition on the passed *l_tp_pt_gp, and not
1143 * on all of the matching target port groups IDs in default_lu_gp.
1145 if (!lu_gp
->lu_gp_id
) {
1147 * core_alua_do_transition_tg_pt() will always return
1150 l_tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1151 l_tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1152 rc
= core_alua_do_transition_tg_pt(l_tg_pt_gp
,
1153 new_state
, explicit);
1154 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1158 * For all other LU groups aside from 'default_lu_gp', walk all of
1159 * the associated storage objects looking for a matching target port
1160 * group ID from the local target port group.
1162 spin_lock(&lu_gp
->lu_gp_lock
);
1163 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
1166 dev
= lu_gp_mem
->lu_gp_mem_dev
;
1167 atomic_inc_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1168 spin_unlock(&lu_gp
->lu_gp_lock
);
1170 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1171 list_for_each_entry(tg_pt_gp
,
1172 &dev
->t10_alua
.tg_pt_gps_list
,
1175 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1178 * If the target behavior port asymmetric access state
1179 * is changed for any target port group accessible via
1180 * a logical unit within a LU group, the target port
1181 * behavior group asymmetric access states for the same
1182 * target port group accessible via other logical units
1183 * in that LU group will also change.
1185 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
1188 if (l_tg_pt_gp
== tg_pt_gp
) {
1189 tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1190 tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1192 tg_pt_gp
->tg_pt_gp_alua_lun
= NULL
;
1193 tg_pt_gp
->tg_pt_gp_alua_nacl
= NULL
;
1195 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1196 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1198 * core_alua_do_transition_tg_pt() will always return
1201 rc
= core_alua_do_transition_tg_pt(tg_pt_gp
,
1202 new_state
, explicit);
1204 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1205 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1209 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1211 spin_lock(&lu_gp
->lu_gp_lock
);
1212 atomic_dec_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1214 spin_unlock(&lu_gp
->lu_gp_lock
);
1217 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1218 " Group IDs: %hu %s transition to primary state: %s\n",
1219 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1220 l_tg_pt_gp
->tg_pt_gp_id
,
1221 (explicit) ? "explicit" : "implicit",
1222 core_alua_dump_state(new_state
));
1225 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1229 static int core_alua_update_tpg_secondary_metadata(struct se_lun
*lun
)
1231 struct se_portal_group
*se_tpg
= lun
->lun_tpg
;
1232 unsigned char *md_buf
;
1233 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
1236 mutex_lock(&lun
->lun_tg_pt_md_mutex
);
1238 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
1240 pr_err("Unable to allocate buf for ALUA metadata\n");
1245 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
1246 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
1248 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
1249 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
));
1251 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
)
1252 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
1253 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
1255 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
, "alua_tg_pt_offline=%d\n"
1256 "alua_tg_pt_status=0x%02x\n",
1257 atomic_read(&lun
->lun_tg_pt_secondary_offline
),
1258 lun
->lun_tg_pt_secondary_stat
);
1260 snprintf(path
, ALUA_METADATA_PATH_LEN
, "/var/target/alua/%s/%s/lun_%llu",
1261 se_tpg
->se_tpg_tfo
->get_fabric_name(), wwn
,
1264 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
1268 mutex_unlock(&lun
->lun_tg_pt_md_mutex
);
1272 static int core_alua_set_tg_pt_secondary_state(
1277 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1278 int trans_delay_msecs
;
1280 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1281 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1283 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1284 pr_err("Unable to complete secondary state"
1288 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1290 * Set the secondary ALUA target port access state to OFFLINE
1291 * or release the previously secondary state for struct se_lun
1294 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 1);
1296 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 0);
1298 lun
->lun_tg_pt_secondary_stat
= (explicit) ?
1299 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1300 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1302 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1303 " to secondary access state: %s\n", (explicit) ? "explicit" :
1304 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1305 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1307 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1309 * Do the optional transition delay after we set the secondary
1310 * ALUA access state.
1312 if (trans_delay_msecs
!= 0)
1313 msleep_interruptible(trans_delay_msecs
);
1315 * See if we need to update the ALUA fabric port metadata for
1316 * secondary state and status
1318 if (lun
->lun_tg_pt_secondary_write_md
)
1319 core_alua_update_tpg_secondary_metadata(lun
);
1324 struct t10_alua_lba_map
*
1325 core_alua_allocate_lba_map(struct list_head
*list
,
1326 u64 first_lba
, u64 last_lba
)
1328 struct t10_alua_lba_map
*lba_map
;
1330 lba_map
= kmem_cache_zalloc(t10_alua_lba_map_cache
, GFP_KERNEL
);
1332 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1333 return ERR_PTR(-ENOMEM
);
1335 INIT_LIST_HEAD(&lba_map
->lba_map_mem_list
);
1336 lba_map
->lba_map_first_lba
= first_lba
;
1337 lba_map
->lba_map_last_lba
= last_lba
;
1339 list_add_tail(&lba_map
->lba_map_list
, list
);
1344 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map
*lba_map
,
1345 int pg_id
, int state
)
1347 struct t10_alua_lba_map_member
*lba_map_mem
;
1349 list_for_each_entry(lba_map_mem
, &lba_map
->lba_map_mem_list
,
1351 if (lba_map_mem
->lba_map_mem_alua_pg_id
== pg_id
) {
1352 pr_err("Duplicate pg_id %d in lba_map\n", pg_id
);
1357 lba_map_mem
= kmem_cache_zalloc(t10_alua_lba_map_mem_cache
, GFP_KERNEL
);
1359 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1362 lba_map_mem
->lba_map_mem_alua_state
= state
;
1363 lba_map_mem
->lba_map_mem_alua_pg_id
= pg_id
;
1365 list_add_tail(&lba_map_mem
->lba_map_mem_list
,
1366 &lba_map
->lba_map_mem_list
);
1371 core_alua_free_lba_map(struct list_head
*lba_list
)
1373 struct t10_alua_lba_map
*lba_map
, *lba_map_tmp
;
1374 struct t10_alua_lba_map_member
*lba_map_mem
, *lba_map_mem_tmp
;
1376 list_for_each_entry_safe(lba_map
, lba_map_tmp
, lba_list
,
1378 list_for_each_entry_safe(lba_map_mem
, lba_map_mem_tmp
,
1379 &lba_map
->lba_map_mem_list
,
1381 list_del(&lba_map_mem
->lba_map_mem_list
);
1382 kmem_cache_free(t10_alua_lba_map_mem_cache
,
1385 list_del(&lba_map
->lba_map_list
);
1386 kmem_cache_free(t10_alua_lba_map_cache
, lba_map
);
1391 core_alua_set_lba_map(struct se_device
*dev
, struct list_head
*lba_map_list
,
1392 int segment_size
, int segment_mult
)
1394 struct list_head old_lba_map_list
;
1395 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1396 int activate
= 0, supported
;
1398 INIT_LIST_HEAD(&old_lba_map_list
);
1399 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1400 dev
->t10_alua
.lba_map_segment_size
= segment_size
;
1401 dev
->t10_alua
.lba_map_segment_multiplier
= segment_mult
;
1402 list_splice_init(&dev
->t10_alua
.lba_map_list
, &old_lba_map_list
);
1404 list_splice_init(lba_map_list
, &dev
->t10_alua
.lba_map_list
);
1407 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1408 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1409 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1412 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1414 supported
= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1416 supported
|= ALUA_LBD_SUP
;
1418 supported
&= ~ALUA_LBD_SUP
;
1419 tg_pt_gp
->tg_pt_gp_alua_supported_states
= supported
;
1421 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1422 core_alua_free_lba_map(&old_lba_map_list
);
1425 struct t10_alua_lu_gp
*
1426 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1428 struct t10_alua_lu_gp
*lu_gp
;
1430 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1432 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1433 return ERR_PTR(-ENOMEM
);
1435 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1436 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1437 spin_lock_init(&lu_gp
->lu_gp_lock
);
1438 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1441 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1442 lu_gp
->lu_gp_valid_id
= 1;
1443 alua_lu_gps_count
++;
1449 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1451 struct t10_alua_lu_gp
*lu_gp_tmp
;
1454 * The lu_gp->lu_gp_id may only be set once..
1456 if (lu_gp
->lu_gp_valid_id
) {
1457 pr_warn("ALUA LU Group already has a valid ID,"
1458 " ignoring request\n");
1462 spin_lock(&lu_gps_lock
);
1463 if (alua_lu_gps_count
== 0x0000ffff) {
1464 pr_err("Maximum ALUA alua_lu_gps_count:"
1465 " 0x0000ffff reached\n");
1466 spin_unlock(&lu_gps_lock
);
1467 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1471 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1472 alua_lu_gps_counter
++;
1474 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1475 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1479 pr_warn("ALUA Logical Unit Group ID: %hu"
1480 " already exists, ignoring request\n",
1482 spin_unlock(&lu_gps_lock
);
1487 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1488 lu_gp
->lu_gp_valid_id
= 1;
1489 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1490 alua_lu_gps_count
++;
1491 spin_unlock(&lu_gps_lock
);
1496 static struct t10_alua_lu_gp_member
*
1497 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1499 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1501 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1503 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1504 return ERR_PTR(-ENOMEM
);
1506 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1507 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1508 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1510 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1511 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1516 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1518 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1520 * Once we have reached this point, config_item_put() has
1521 * already been called from target_core_alua_drop_lu_gp().
1523 * Here, we remove the *lu_gp from the global list so that
1524 * no associations can be made while we are releasing
1525 * struct t10_alua_lu_gp.
1527 spin_lock(&lu_gps_lock
);
1528 list_del(&lu_gp
->lu_gp_node
);
1529 alua_lu_gps_count
--;
1530 spin_unlock(&lu_gps_lock
);
1532 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1533 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1534 * released with core_alua_put_lu_gp_from_name()
1536 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1539 * Release reference to struct t10_alua_lu_gp * from all associated
1542 spin_lock(&lu_gp
->lu_gp_lock
);
1543 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1544 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1545 if (lu_gp_mem
->lu_gp_assoc
) {
1546 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1547 lu_gp
->lu_gp_members
--;
1548 lu_gp_mem
->lu_gp_assoc
= 0;
1550 spin_unlock(&lu_gp
->lu_gp_lock
);
1553 * lu_gp_mem is associated with a single
1554 * struct se_device->dev_alua_lu_gp_mem, and is released when
1555 * struct se_device is released via core_alua_free_lu_gp_mem().
1557 * If the passed lu_gp does NOT match the default_lu_gp, assume
1558 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1560 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1561 if (lu_gp
!= default_lu_gp
)
1562 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1565 lu_gp_mem
->lu_gp
= NULL
;
1566 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1568 spin_lock(&lu_gp
->lu_gp_lock
);
1570 spin_unlock(&lu_gp
->lu_gp_lock
);
1572 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1575 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1577 struct t10_alua_lu_gp
*lu_gp
;
1578 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1580 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1584 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1587 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1588 lu_gp
= lu_gp_mem
->lu_gp
;
1590 spin_lock(&lu_gp
->lu_gp_lock
);
1591 if (lu_gp_mem
->lu_gp_assoc
) {
1592 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1593 lu_gp
->lu_gp_members
--;
1594 lu_gp_mem
->lu_gp_assoc
= 0;
1596 spin_unlock(&lu_gp
->lu_gp_lock
);
1597 lu_gp_mem
->lu_gp
= NULL
;
1599 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1601 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1604 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1606 struct t10_alua_lu_gp
*lu_gp
;
1607 struct config_item
*ci
;
1609 spin_lock(&lu_gps_lock
);
1610 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1611 if (!lu_gp
->lu_gp_valid_id
)
1613 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1614 if (!strcmp(config_item_name(ci
), name
)) {
1615 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1616 spin_unlock(&lu_gps_lock
);
1620 spin_unlock(&lu_gps_lock
);
1625 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1627 spin_lock(&lu_gps_lock
);
1628 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1629 spin_unlock(&lu_gps_lock
);
1633 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1635 void __core_alua_attach_lu_gp_mem(
1636 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1637 struct t10_alua_lu_gp
*lu_gp
)
1639 spin_lock(&lu_gp
->lu_gp_lock
);
1640 lu_gp_mem
->lu_gp
= lu_gp
;
1641 lu_gp_mem
->lu_gp_assoc
= 1;
1642 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1643 lu_gp
->lu_gp_members
++;
1644 spin_unlock(&lu_gp
->lu_gp_lock
);
1648 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1650 void __core_alua_drop_lu_gp_mem(
1651 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1652 struct t10_alua_lu_gp
*lu_gp
)
1654 spin_lock(&lu_gp
->lu_gp_lock
);
1655 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1656 lu_gp_mem
->lu_gp
= NULL
;
1657 lu_gp_mem
->lu_gp_assoc
= 0;
1658 lu_gp
->lu_gp_members
--;
1659 spin_unlock(&lu_gp
->lu_gp_lock
);
1662 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(struct se_device
*dev
,
1663 const char *name
, int def_group
)
1665 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1667 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1669 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1672 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1673 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_lun_list
);
1674 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1675 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1676 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1677 INIT_DELAYED_WORK(&tg_pt_gp
->tg_pt_gp_transition_work
,
1678 core_alua_do_transition_tg_pt_work
);
1679 tg_pt_gp
->tg_pt_gp_dev
= dev
;
1680 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1681 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
);
1683 * Enable both explicit and implicit ALUA support by default
1685 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1686 TPGS_EXPLICIT_ALUA
| TPGS_IMPLICIT_ALUA
;
1688 * Set the default Active/NonOptimized Delay in milliseconds
1690 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1691 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1692 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= ALUA_DEFAULT_IMPLICIT_TRANS_SECS
;
1695 * Enable all supported states
1697 tg_pt_gp
->tg_pt_gp_alua_supported_states
=
1698 ALUA_T_SUP
| ALUA_O_SUP
|
1699 ALUA_U_SUP
| ALUA_S_SUP
| ALUA_AN_SUP
| ALUA_AO_SUP
;
1702 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1703 tg_pt_gp
->tg_pt_gp_id
=
1704 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1705 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1706 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1707 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1708 &dev
->t10_alua
.tg_pt_gps_list
);
1709 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1715 int core_alua_set_tg_pt_gp_id(
1716 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1719 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1720 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1721 u16 tg_pt_gp_id_tmp
;
1724 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1726 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1727 pr_warn("ALUA TG PT Group already has a valid ID,"
1728 " ignoring request\n");
1732 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1733 if (dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1734 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1735 " 0x0000ffff reached\n");
1736 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1737 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1741 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1742 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1744 list_for_each_entry(tg_pt_gp_tmp
, &dev
->t10_alua
.tg_pt_gps_list
,
1746 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1750 pr_err("ALUA Target Port Group ID: %hu already"
1751 " exists, ignoring request\n", tg_pt_gp_id
);
1752 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1757 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1758 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1759 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1760 &dev
->t10_alua
.tg_pt_gps_list
);
1761 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1762 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1767 void core_alua_free_tg_pt_gp(
1768 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1770 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1771 struct se_lun
*lun
, *next
;
1774 * Once we have reached this point, config_item_put() has already
1775 * been called from target_core_alua_drop_tg_pt_gp().
1777 * Here we remove *tg_pt_gp from the global list so that
1778 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1779 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1781 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1782 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1783 dev
->t10_alua
.alua_tg_pt_gps_counter
--;
1784 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1786 flush_delayed_work(&tg_pt_gp
->tg_pt_gp_transition_work
);
1789 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1790 * core_alua_get_tg_pt_gp_by_name() in
1791 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1792 * to be released with core_alua_put_tg_pt_gp_from_name().
1794 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1798 * Release reference to struct t10_alua_tg_pt_gp from all associated
1801 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1802 list_for_each_entry_safe(lun
, next
,
1803 &tg_pt_gp
->tg_pt_gp_lun_list
, lun_tg_pt_gp_link
) {
1804 list_del_init(&lun
->lun_tg_pt_gp_link
);
1805 tg_pt_gp
->tg_pt_gp_members
--;
1807 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1809 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1810 * assume we want to re-associate a given tg_pt_gp_mem with
1813 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1814 if (tg_pt_gp
!= dev
->t10_alua
.default_tg_pt_gp
) {
1815 __target_attach_tg_pt_gp(lun
,
1816 dev
->t10_alua
.default_tg_pt_gp
);
1818 lun
->lun_tg_pt_gp
= NULL
;
1819 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1821 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1823 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1825 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1828 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1829 struct se_device
*dev
, const char *name
)
1831 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1832 struct config_item
*ci
;
1834 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1835 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1837 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1839 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1840 if (!strcmp(config_item_name(ci
), name
)) {
1841 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1842 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1846 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1851 static void core_alua_put_tg_pt_gp_from_name(
1852 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1854 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1856 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1857 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1858 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1861 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
1862 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1864 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1866 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1867 lun
->lun_tg_pt_gp
= tg_pt_gp
;
1868 list_add_tail(&lun
->lun_tg_pt_gp_link
, &tg_pt_gp
->tg_pt_gp_lun_list
);
1869 tg_pt_gp
->tg_pt_gp_members
++;
1870 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1873 void target_attach_tg_pt_gp(struct se_lun
*lun
,
1874 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1876 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1877 __target_attach_tg_pt_gp(lun
, tg_pt_gp
);
1878 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1881 static void __target_detach_tg_pt_gp(struct se_lun
*lun
,
1882 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1884 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1886 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1887 list_del_init(&lun
->lun_tg_pt_gp_link
);
1888 tg_pt_gp
->tg_pt_gp_members
--;
1889 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1891 lun
->lun_tg_pt_gp
= NULL
;
1894 void target_detach_tg_pt_gp(struct se_lun
*lun
)
1896 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1898 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1899 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1901 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1902 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1905 ssize_t
core_alua_show_tg_pt_gp_info(struct se_lun
*lun
, char *page
)
1907 struct config_item
*tg_pt_ci
;
1908 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1911 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1912 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1914 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1915 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1916 " %hu\nTG Port Primary Access State: %s\nTG Port "
1917 "Primary Access Status: %s\nTG Port Secondary Access"
1918 " State: %s\nTG Port Secondary Access Status: %s\n",
1919 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1920 core_alua_dump_state(atomic_read(
1921 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
1922 core_alua_dump_status(
1923 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1924 atomic_read(&lun
->lun_tg_pt_secondary_offline
) ?
1926 core_alua_dump_status(lun
->lun_tg_pt_secondary_stat
));
1928 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1933 ssize_t
core_alua_store_tg_pt_gp_info(
1938 struct se_portal_group
*tpg
= lun
->lun_tpg
;
1940 * rcu_dereference_raw protected by se_lun->lun_group symlink
1941 * reference to se_device->dev_group.
1943 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
1944 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1945 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1948 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
||
1949 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
1952 if (count
> TG_PT_GROUP_NAME_BUF
) {
1953 pr_err("ALUA Target Port Group alias too large!\n");
1956 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1957 memcpy(buf
, page
, count
);
1959 * Any ALUA target port group alias besides "NULL" means we will be
1960 * making a new group association.
1962 if (strcmp(strstrip(buf
), "NULL")) {
1964 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1965 * struct t10_alua_tg_pt_gp. This reference is released with
1966 * core_alua_put_tg_pt_gp_from_name() below.
1968 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(dev
,
1974 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1975 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1978 * Clearing an existing tg_pt_gp association, and replacing
1979 * with the default_tg_pt_gp.
1981 if (!tg_pt_gp_new
) {
1982 pr_debug("Target_Core_ConfigFS: Moving"
1983 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1984 " alua/%s, ID: %hu back to"
1985 " default_tg_pt_gp\n",
1986 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1987 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1988 config_item_name(&lun
->lun_group
.cg_item
),
1990 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1991 tg_pt_gp
->tg_pt_gp_id
);
1993 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1994 __target_attach_tg_pt_gp(lun
,
1995 dev
->t10_alua
.default_tg_pt_gp
);
1996 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
2000 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
2004 __target_attach_tg_pt_gp(lun
, tg_pt_gp_new
);
2005 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
2006 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
2007 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
2008 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
2009 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
2010 config_item_name(&lun
->lun_group
.cg_item
),
2011 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
2012 tg_pt_gp_new
->tg_pt_gp_id
);
2014 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
2018 ssize_t
core_alua_show_access_type(
2019 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2022 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
) &&
2023 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
))
2024 return sprintf(page
, "Implicit and Explicit\n");
2025 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
)
2026 return sprintf(page
, "Implicit\n");
2027 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)
2028 return sprintf(page
, "Explicit\n");
2030 return sprintf(page
, "None\n");
2033 ssize_t
core_alua_store_access_type(
2034 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2041 ret
= kstrtoul(page
, 0, &tmp
);
2043 pr_err("Unable to extract alua_access_type\n");
2046 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
2047 pr_err("Illegal value for alua_access_type:"
2052 tg_pt_gp
->tg_pt_gp_alua_access_type
=
2053 TPGS_IMPLICIT_ALUA
| TPGS_EXPLICIT_ALUA
;
2055 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICIT_ALUA
;
2057 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICIT_ALUA
;
2059 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
2064 ssize_t
core_alua_show_nonop_delay_msecs(
2065 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2068 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
2071 ssize_t
core_alua_store_nonop_delay_msecs(
2072 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2079 ret
= kstrtoul(page
, 0, &tmp
);
2081 pr_err("Unable to extract nonop_delay_msecs\n");
2084 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
2085 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2086 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
2087 ALUA_MAX_NONOP_DELAY_MSECS
);
2090 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
2095 ssize_t
core_alua_show_trans_delay_msecs(
2096 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2099 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
2102 ssize_t
core_alua_store_trans_delay_msecs(
2103 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2110 ret
= kstrtoul(page
, 0, &tmp
);
2112 pr_err("Unable to extract trans_delay_msecs\n");
2115 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
2116 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2117 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
2118 ALUA_MAX_TRANS_DELAY_MSECS
);
2121 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
2126 ssize_t
core_alua_show_implicit_trans_secs(
2127 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2130 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implicit_trans_secs
);
2133 ssize_t
core_alua_store_implicit_trans_secs(
2134 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2141 ret
= kstrtoul(page
, 0, &tmp
);
2143 pr_err("Unable to extract implicit_trans_secs\n");
2146 if (tmp
> ALUA_MAX_IMPLICIT_TRANS_SECS
) {
2147 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2148 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp
,
2149 ALUA_MAX_IMPLICIT_TRANS_SECS
);
2152 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= (int)tmp
;
2157 ssize_t
core_alua_show_preferred_bit(
2158 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2161 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
2164 ssize_t
core_alua_store_preferred_bit(
2165 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2172 ret
= kstrtoul(page
, 0, &tmp
);
2174 pr_err("Unable to extract preferred ALUA value\n");
2177 if ((tmp
!= 0) && (tmp
!= 1)) {
2178 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
2181 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
2186 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
2188 return sprintf(page
, "%d\n",
2189 atomic_read(&lun
->lun_tg_pt_secondary_offline
));
2192 ssize_t
core_alua_store_offline_bit(
2198 * rcu_dereference_raw protected by se_lun->lun_group symlink
2199 * reference to se_device->dev_group.
2201 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
2205 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
||
2206 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
2209 ret
= kstrtoul(page
, 0, &tmp
);
2211 pr_err("Unable to extract alua_tg_pt_offline value\n");
2214 if ((tmp
!= 0) && (tmp
!= 1)) {
2215 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2220 ret
= core_alua_set_tg_pt_secondary_state(lun
, 0, (int)tmp
);
2227 ssize_t
core_alua_show_secondary_status(
2231 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_stat
);
2234 ssize_t
core_alua_store_secondary_status(
2242 ret
= kstrtoul(page
, 0, &tmp
);
2244 pr_err("Unable to extract alua_tg_pt_status\n");
2247 if ((tmp
!= ALUA_STATUS_NONE
) &&
2248 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
2249 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
)) {
2250 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2254 lun
->lun_tg_pt_secondary_stat
= (int)tmp
;
2259 ssize_t
core_alua_show_secondary_write_metadata(
2263 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_write_md
);
2266 ssize_t
core_alua_store_secondary_write_metadata(
2274 ret
= kstrtoul(page
, 0, &tmp
);
2276 pr_err("Unable to extract alua_tg_pt_write_md\n");
2279 if ((tmp
!= 0) && (tmp
!= 1)) {
2280 pr_err("Illegal value for alua_tg_pt_write_md:"
2284 lun
->lun_tg_pt_secondary_write_md
= (int)tmp
;
2289 int core_setup_alua(struct se_device
*dev
)
2291 if (!(dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
) &&
2292 !(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)) {
2293 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2296 * Associate this struct se_device with the default ALUA
2299 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2300 if (IS_ERR(lu_gp_mem
))
2301 return PTR_ERR(lu_gp_mem
);
2303 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2304 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2306 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2308 pr_debug("%s: Adding to default ALUA LU Group:"
2309 " core/alua/lu_gps/default_lu_gp\n",
2310 dev
->transport
->name
);