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/delay.h>
30 #include <linux/export.h>
31 #include <linux/fcntl.h>
32 #include <linux/file.h>
34 #include <scsi/scsi_proto.h>
35 #include <asm/unaligned.h>
37 #include <target/target_core_base.h>
38 #include <target/target_core_backend.h>
39 #include <target/target_core_fabric.h>
41 #include "target_core_internal.h"
42 #include "target_core_alua.h"
43 #include "target_core_ua.h"
45 static sense_reason_t
core_alua_check_transition(int state
, int valid
,
46 int *primary
, int explicit);
47 static int core_alua_set_tg_pt_secondary_state(
48 struct se_lun
*lun
, int explicit, int offline
);
50 static char *core_alua_dump_state(int state
);
52 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
53 struct t10_alua_tg_pt_gp
*tg_pt_gp
);
55 static u16 alua_lu_gps_counter
;
56 static u32 alua_lu_gps_count
;
58 static DEFINE_SPINLOCK(lu_gps_lock
);
59 static LIST_HEAD(lu_gps_list
);
61 struct t10_alua_lu_gp
*default_lu_gp
;
66 * See sbc3r35 section 5.23
69 target_emulate_report_referrals(struct se_cmd
*cmd
)
71 struct se_device
*dev
= cmd
->se_dev
;
72 struct t10_alua_lba_map
*map
;
73 struct t10_alua_lba_map_member
*map_mem
;
77 if (cmd
->data_length
< 4) {
78 pr_warn("REPORT REFERRALS allocation length %u too"
79 " small\n", cmd
->data_length
);
80 return TCM_INVALID_CDB_FIELD
;
83 buf
= transport_kmap_data_sg(cmd
);
85 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
88 spin_lock(&dev
->t10_alua
.lba_map_lock
);
89 if (list_empty(&dev
->t10_alua
.lba_map_list
)) {
90 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
91 transport_kunmap_data_sg(cmd
);
93 return TCM_UNSUPPORTED_SCSI_OPCODE
;
96 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
98 int desc_num
= off
+ 3;
102 if (cmd
->data_length
> off
)
103 put_unaligned_be64(map
->lba_map_first_lba
, &buf
[off
]);
105 if (cmd
->data_length
> off
)
106 put_unaligned_be64(map
->lba_map_last_lba
, &buf
[off
]);
110 list_for_each_entry(map_mem
, &map
->lba_map_mem_list
,
112 int alua_state
= map_mem
->lba_map_mem_alua_state
;
113 int alua_pg_id
= map_mem
->lba_map_mem_alua_pg_id
;
115 if (cmd
->data_length
> off
)
116 buf
[off
] = alua_state
& 0x0f;
118 if (cmd
->data_length
> off
)
119 buf
[off
] = (alua_pg_id
>> 8) & 0xff;
121 if (cmd
->data_length
> off
)
122 buf
[off
] = (alua_pg_id
& 0xff);
127 if (cmd
->data_length
> desc_num
)
128 buf
[desc_num
] = pg_num
;
130 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
133 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
135 put_unaligned_be16(rd_len
, &buf
[2]);
137 transport_kunmap_data_sg(cmd
);
139 target_complete_cmd(cmd
, GOOD
);
144 * REPORT_TARGET_PORT_GROUPS
146 * See spc4r17 section 6.27
149 target_emulate_report_target_port_groups(struct se_cmd
*cmd
)
151 struct se_device
*dev
= cmd
->se_dev
;
152 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
156 int ext_hdr
= (cmd
->t_task_cdb
[1] & 0x20);
159 * Skip over RESERVED area to first Target port group descriptor
160 * depending on the PARAMETER DATA FORMAT type..
167 if (cmd
->data_length
< off
) {
168 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
169 " small for %s header\n", cmd
->data_length
,
170 (ext_hdr
) ? "extended" : "normal");
171 return TCM_INVALID_CDB_FIELD
;
173 buf
= transport_kmap_data_sg(cmd
);
175 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
177 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
178 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
181 * Check if the Target port group and Target port descriptor list
182 * based on tg_pt_gp_members count will fit into the response payload.
183 * Otherwise, bump rd_len to let the initiator know we have exceeded
184 * the allocation length and the response is truncated.
186 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
188 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
192 * PREF: Preferred target port bit, determine if this
193 * bit should be set for port group.
195 if (tg_pt_gp
->tg_pt_gp_pref
)
198 * Set the ASYMMETRIC ACCESS State
200 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_access_state
& 0xff;
202 * Set supported ASYMMETRIC ACCESS State bits
204 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
208 put_unaligned_be16(tg_pt_gp
->tg_pt_gp_id
, &buf
[off
]);
211 off
++; /* Skip over Reserved */
215 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
217 * Vendor Specific field
223 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
226 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
227 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
230 * Start Target Port descriptor format
232 * See spc4r17 section 6.2.7 Table 247
234 off
+= 2; /* Skip over Obsolete */
236 * Set RELATIVE TARGET PORT IDENTIFIER
238 put_unaligned_be16(lun
->lun_rtpi
, &buf
[off
]);
242 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
244 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
246 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
248 put_unaligned_be32(rd_len
, &buf
[0]);
251 * Fill in the Extended header parameter data format if requested
256 * Set the implicit transition time (in seconds) for the application
257 * client to use as a base for it's transition timeout value.
259 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
260 * this CDB was received upon to determine this value individually
261 * for ALUA target port group.
263 spin_lock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
264 tg_pt_gp
= cmd
->se_lun
->lun_tg_pt_gp
;
266 buf
[5] = tg_pt_gp
->tg_pt_gp_implicit_trans_secs
;
267 spin_unlock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
269 transport_kunmap_data_sg(cmd
);
271 target_complete_cmd_with_length(cmd
, GOOD
, rd_len
+ 4);
276 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
278 * See spc4r17 section 6.35
281 target_emulate_set_target_port_groups(struct se_cmd
*cmd
)
283 struct se_device
*dev
= cmd
->se_dev
;
284 struct se_lun
*l_lun
= cmd
->se_lun
;
285 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
286 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
289 sense_reason_t rc
= TCM_NO_SENSE
;
290 u32 len
= 4; /* Skip over RESERVED area in header */
291 int alua_access_state
, primary
= 0, valid_states
;
294 if (cmd
->data_length
< 4) {
295 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
296 " small\n", cmd
->data_length
);
297 return TCM_INVALID_PARAMETER_LIST
;
300 buf
= transport_kmap_data_sg(cmd
);
302 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
305 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
306 * for the local tg_pt_gp.
308 spin_lock(&l_lun
->lun_tg_pt_gp_lock
);
309 l_tg_pt_gp
= l_lun
->lun_tg_pt_gp
;
311 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
312 pr_err("Unable to access l_lun->tg_pt_gp\n");
313 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
317 if (!(l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)) {
318 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
319 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
320 " while TPGS_EXPLICIT_ALUA is disabled\n");
321 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
324 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
325 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
327 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
329 while (len
< cmd
->data_length
) {
331 alua_access_state
= (ptr
[0] & 0x0f);
333 * Check the received ALUA access state, and determine if
334 * the state is a primary or secondary target port asymmetric
337 rc
= core_alua_check_transition(alua_access_state
, valid_states
,
341 * If the SET TARGET PORT GROUPS attempts to establish
342 * an invalid combination of target port asymmetric
343 * access states or attempts to establish an
344 * unsupported target port asymmetric access state,
345 * then the command shall be terminated with CHECK
346 * CONDITION status, with the sense key set to ILLEGAL
347 * REQUEST, and the additional sense code set to INVALID
348 * FIELD IN PARAMETER LIST.
354 * If the ASYMMETRIC ACCESS STATE field (see table 267)
355 * specifies a primary target port asymmetric access state,
356 * then the TARGET PORT GROUP OR TARGET PORT field specifies
357 * a primary target port group for which the primary target
358 * port asymmetric access state shall be changed. If the
359 * ASYMMETRIC ACCESS STATE field specifies a secondary target
360 * port asymmetric access state, then the TARGET PORT GROUP OR
361 * TARGET PORT field specifies the relative target port
362 * identifier (see 3.1.120) of the target port for which the
363 * secondary target port asymmetric access state shall be
367 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
369 * Locate the matching target port group ID from
370 * the global tg_pt_gp list
372 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
373 list_for_each_entry(tg_pt_gp
,
374 &dev
->t10_alua
.tg_pt_gps_list
,
376 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
379 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
382 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
384 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
386 if (!core_alua_do_port_transition(tg_pt_gp
,
388 alua_access_state
, 1))
391 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
392 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
395 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
400 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
401 * the Target Port in question for the the incoming
402 * SET_TARGET_PORT_GROUPS op.
404 rtpi
= get_unaligned_be16(ptr
+ 2);
406 * Locate the matching relative target port identifier
407 * for the struct se_device storage object.
409 spin_lock(&dev
->se_port_lock
);
410 list_for_each_entry(lun
, &dev
->dev_sep_list
,
412 if (lun
->lun_rtpi
!= rtpi
)
416 spin_unlock(&dev
->se_port_lock
);
418 if (!core_alua_set_tg_pt_secondary_state(
422 spin_lock(&dev
->se_port_lock
);
425 spin_unlock(&dev
->se_port_lock
);
429 rc
= TCM_INVALID_PARAMETER_LIST
;
438 transport_kunmap_data_sg(cmd
);
440 target_complete_cmd(cmd
, GOOD
);
444 static inline void set_ascq(struct se_cmd
*cmd
, u8 alua_ascq
)
447 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
448 * The ALUA additional sense code qualifier (ASCQ) is determined
449 * by the ALUA primary or secondary access state..
451 pr_debug("[%s]: ALUA TG Port not available, "
452 "SenseKey: NOT_READY, ASC/ASCQ: "
454 cmd
->se_tfo
->fabric_name
, alua_ascq
);
456 cmd
->scsi_asc
= 0x04;
457 cmd
->scsi_ascq
= alua_ascq
;
460 static inline void core_alua_state_nonoptimized(
463 int nonop_delay_msecs
)
466 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
467 * later to determine if processing of this cmd needs to be
468 * temporarily delayed for the Active/NonOptimized primary access state.
470 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
471 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
474 static inline int core_alua_state_lba_dependent(
476 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
478 struct se_device
*dev
= cmd
->se_dev
;
479 u64 segment_size
, segment_mult
, sectors
, lba
;
481 /* Only need to check for cdb actually containing LBAs */
482 if (!(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
))
485 spin_lock(&dev
->t10_alua
.lba_map_lock
);
486 segment_size
= dev
->t10_alua
.lba_map_segment_size
;
487 segment_mult
= dev
->t10_alua
.lba_map_segment_multiplier
;
488 sectors
= cmd
->data_length
/ dev
->dev_attrib
.block_size
;
490 lba
= cmd
->t_task_lba
;
491 while (lba
< cmd
->t_task_lba
+ sectors
) {
492 struct t10_alua_lba_map
*cur_map
= NULL
, *map
;
493 struct t10_alua_lba_map_member
*map_mem
;
495 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
497 u64 start_lba
, last_lba
;
498 u64 first_lba
= map
->lba_map_first_lba
;
502 start_lba
= do_div(tmp
, segment_size
* segment_mult
);
504 last_lba
= first_lba
+ segment_size
- 1;
505 if (start_lba
>= first_lba
&&
506 start_lba
<= last_lba
) {
512 last_lba
= map
->lba_map_last_lba
;
513 if (lba
>= first_lba
&& lba
<= last_lba
) {
521 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
522 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
525 list_for_each_entry(map_mem
, &cur_map
->lba_map_mem_list
,
527 if (map_mem
->lba_map_mem_alua_pg_id
!=
528 tg_pt_gp
->tg_pt_gp_id
)
530 switch(map_mem
->lba_map_mem_alua_state
) {
531 case ALUA_ACCESS_STATE_STANDBY
:
532 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
533 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
535 case ALUA_ACCESS_STATE_UNAVAILABLE
:
536 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
537 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
544 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
548 static inline int core_alua_state_standby(
553 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
554 * spc4r17 section 5.9.2.4.4
563 case RECEIVE_DIAGNOSTIC
:
564 case SEND_DIAGNOSTIC
:
567 case SERVICE_ACTION_IN_16
:
568 switch (cdb
[1] & 0x1f) {
569 case SAI_READ_CAPACITY_16
:
572 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
576 switch (cdb
[1] & 0x1f) {
577 case MI_REPORT_TARGET_PGS
:
580 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
583 case MAINTENANCE_OUT
:
585 case MO_SET_TARGET_PGS
:
588 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
592 case PERSISTENT_RESERVE_IN
:
593 case PERSISTENT_RESERVE_OUT
:
598 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
605 static inline int core_alua_state_unavailable(
610 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
611 * spc4r17 section 5.9.2.4.5
618 switch (cdb
[1] & 0x1f) {
619 case MI_REPORT_TARGET_PGS
:
622 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
625 case MAINTENANCE_OUT
:
627 case MO_SET_TARGET_PGS
:
630 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
638 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
645 static inline int core_alua_state_transition(
650 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
651 * spc4r17 section 5.9.2.5
658 switch (cdb
[1] & 0x1f) {
659 case MI_REPORT_TARGET_PGS
:
662 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
670 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
678 * return 1: Is used to signal LUN not accessible, and check condition/not ready
679 * return 0: Used to signal success
680 * return -1: Used to signal failure, and invalid cdb field
683 target_alua_state_check(struct se_cmd
*cmd
)
685 struct se_device
*dev
= cmd
->se_dev
;
686 unsigned char *cdb
= cmd
->t_task_cdb
;
687 struct se_lun
*lun
= cmd
->se_lun
;
688 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
689 int out_alua_state
, nonop_delay_msecs
;
691 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
693 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
)
697 * First, check for a struct se_port specific secondary ALUA target port
698 * access state: OFFLINE
700 if (atomic_read(&lun
->lun_tg_pt_secondary_offline
)) {
701 pr_debug("ALUA: Got secondary offline status for local"
703 set_ascq(cmd
, ASCQ_04H_ALUA_OFFLINE
);
704 return TCM_CHECK_CONDITION_NOT_READY
;
707 if (!lun
->lun_tg_pt_gp
)
710 spin_lock(&lun
->lun_tg_pt_gp_lock
);
711 tg_pt_gp
= lun
->lun_tg_pt_gp
;
712 out_alua_state
= tg_pt_gp
->tg_pt_gp_alua_access_state
;
713 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
715 // XXX: keeps using tg_pt_gp witout reference after unlock
716 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
718 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
719 * statement so the compiler knows explicitly to check this case first.
720 * For the Optimized ALUA access state case, we want to process the
721 * incoming fabric cmd ASAP..
723 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
)
726 switch (out_alua_state
) {
727 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
728 core_alua_state_nonoptimized(cmd
, cdb
, nonop_delay_msecs
);
730 case ALUA_ACCESS_STATE_STANDBY
:
731 if (core_alua_state_standby(cmd
, cdb
))
732 return TCM_CHECK_CONDITION_NOT_READY
;
734 case ALUA_ACCESS_STATE_UNAVAILABLE
:
735 if (core_alua_state_unavailable(cmd
, cdb
))
736 return TCM_CHECK_CONDITION_NOT_READY
;
738 case ALUA_ACCESS_STATE_TRANSITION
:
739 if (core_alua_state_transition(cmd
, cdb
))
740 return TCM_CHECK_CONDITION_NOT_READY
;
742 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
743 if (core_alua_state_lba_dependent(cmd
, tg_pt_gp
))
744 return TCM_CHECK_CONDITION_NOT_READY
;
747 * OFFLINE is a secondary ALUA target port group access state, that is
748 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
750 case ALUA_ACCESS_STATE_OFFLINE
:
752 pr_err("Unknown ALUA access state: 0x%02x\n",
754 return TCM_INVALID_CDB_FIELD
;
761 * Check implicit and explicit ALUA state change request.
763 static sense_reason_t
764 core_alua_check_transition(int state
, int valid
, int *primary
, int explicit)
767 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
768 * defined as primary target port asymmetric access states.
771 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
772 if (!(valid
& ALUA_AO_SUP
))
776 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
777 if (!(valid
& ALUA_AN_SUP
))
781 case ALUA_ACCESS_STATE_STANDBY
:
782 if (!(valid
& ALUA_S_SUP
))
786 case ALUA_ACCESS_STATE_UNAVAILABLE
:
787 if (!(valid
& ALUA_U_SUP
))
791 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
792 if (!(valid
& ALUA_LBD_SUP
))
796 case ALUA_ACCESS_STATE_OFFLINE
:
798 * OFFLINE state is defined as a secondary target port
799 * asymmetric access state.
801 if (!(valid
& ALUA_O_SUP
))
805 case ALUA_ACCESS_STATE_TRANSITION
:
806 if (!(valid
& ALUA_T_SUP
) || explicit)
808 * Transitioning is set internally and by tcmu daemon,
809 * and cannot be selected through a STPG.
815 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
816 return TCM_INVALID_PARAMETER_LIST
;
822 pr_err("ALUA access state %s not supported",
823 core_alua_dump_state(state
));
824 return TCM_INVALID_PARAMETER_LIST
;
827 static char *core_alua_dump_state(int state
)
830 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
831 return "Active/Optimized";
832 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
833 return "Active/NonOptimized";
834 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
835 return "LBA Dependent";
836 case ALUA_ACCESS_STATE_STANDBY
:
838 case ALUA_ACCESS_STATE_UNAVAILABLE
:
839 return "Unavailable";
840 case ALUA_ACCESS_STATE_OFFLINE
:
842 case ALUA_ACCESS_STATE_TRANSITION
:
843 return "Transitioning";
851 char *core_alua_dump_status(int status
)
854 case ALUA_STATUS_NONE
:
856 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
857 return "Altered by Explicit STPG";
858 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
:
859 return "Altered by Implicit ALUA";
868 * Used by fabric modules to determine when we need to delay processing
869 * for the Active/NonOptimized paths..
871 int core_alua_check_nonop_delay(
874 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
879 * The ALUA Active/NonOptimized access state delay can be disabled
880 * in via configfs with a value of zero
882 if (!cmd
->alua_nonop_delay
)
885 * struct se_cmd->alua_nonop_delay gets set by a target port group
886 * defined interval in core_alua_state_nonoptimized()
888 msleep_interruptible(cmd
->alua_nonop_delay
);
891 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
893 static int core_alua_write_tpg_metadata(
895 unsigned char *md_buf
,
898 struct file
*file
= filp_open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
903 pr_err("filp_open(%s) for ALUA metadata failed\n", path
);
906 ret
= kernel_write(file
, md_buf
, md_buf_len
, &pos
);
908 pr_err("Error writing ALUA metadata file: %s\n", path
);
910 return (ret
< 0) ? -EIO
: 0;
913 static int core_alua_update_tpg_primary_metadata(
914 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
916 unsigned char *md_buf
;
917 struct t10_wwn
*wwn
= &tg_pt_gp
->tg_pt_gp_dev
->t10_wwn
;
921 lockdep_assert_held(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
923 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
925 pr_err("Unable to allocate buf for ALUA metadata\n");
929 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
,
931 "alua_access_state=0x%02x\n"
932 "alua_access_status=0x%02x\n",
933 tg_pt_gp
->tg_pt_gp_id
,
934 tg_pt_gp
->tg_pt_gp_alua_access_state
,
935 tg_pt_gp
->tg_pt_gp_alua_access_status
);
938 path
= kasprintf(GFP_KERNEL
, "%s/alua/tpgs_%s/%s", db_root
,
939 &wwn
->unit_serial
[0],
940 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
942 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
949 static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp
*tg_pt_gp
)
951 struct se_dev_entry
*se_deve
;
953 struct se_lun_acl
*lacl
;
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(&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
));
983 * After an explicit target port asymmetric access
984 * state change, a device server shall establish a
985 * unit attention condition with the additional sense
986 * code set to ASYMMETRIC ACCESS STATE CHANGED for
987 * the initiator port associated with every I_T nexus
988 * other than the I_T nexus on which the SET TARGET
989 * PORT GROUPS command was received.
991 if ((tg_pt_gp
->tg_pt_gp_alua_access_status
==
992 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
993 (tg_pt_gp
->tg_pt_gp_alua_lun
!= NULL
) &&
994 (tg_pt_gp
->tg_pt_gp_alua_lun
== lun
))
998 * se_deve->se_lun_acl pointer may be NULL for a
999 * entry created without explicit Node+MappedLUN ACLs
1001 if (lacl
&& (tg_pt_gp
->tg_pt_gp_alua_nacl
!= NULL
) &&
1002 (tg_pt_gp
->tg_pt_gp_alua_nacl
== lacl
->se_lun_nacl
))
1005 core_scsi3_ua_allocate(se_deve
, 0x2A,
1006 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
1008 spin_unlock(&lun
->lun_deve_lock
);
1010 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1011 percpu_ref_put(&lun
->lun_ref
);
1013 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1016 static int core_alua_do_transition_tg_pt(
1017 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1023 mutex_lock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1024 /* Nothing to be done here */
1025 if (tg_pt_gp
->tg_pt_gp_alua_access_state
== new_state
) {
1026 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1030 if (explicit && new_state
== ALUA_ACCESS_STATE_TRANSITION
) {
1031 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1036 * Save the old primary ALUA access state, and set the current state
1037 * to ALUA_ACCESS_STATE_TRANSITION.
1039 prev_state
= tg_pt_gp
->tg_pt_gp_alua_access_state
;
1040 tg_pt_gp
->tg_pt_gp_alua_access_state
= ALUA_ACCESS_STATE_TRANSITION
;
1041 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explicit) ?
1042 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1043 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1045 core_alua_queue_state_change_ua(tg_pt_gp
);
1047 if (new_state
== ALUA_ACCESS_STATE_TRANSITION
) {
1048 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1053 * Check for the optional ALUA primary state transition delay
1055 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
1056 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1059 * Set the current primary ALUA access state to the requested new state
1061 tg_pt_gp
->tg_pt_gp_alua_access_state
= new_state
;
1064 * Update the ALUA metadata buf that has been allocated in
1065 * core_alua_do_port_transition(), this metadata will be written
1068 * Note that there is the case where we do not want to update the
1069 * metadata when the saved metadata is being parsed in userspace
1070 * when setting the existing port access state and access status.
1072 * Also note that the failure to write out the ALUA metadata to
1073 * struct file does NOT affect the actual ALUA transition.
1075 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
1076 core_alua_update_tpg_primary_metadata(tg_pt_gp
);
1079 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1080 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1081 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1082 tg_pt_gp
->tg_pt_gp_id
,
1083 core_alua_dump_state(prev_state
),
1084 core_alua_dump_state(new_state
));
1086 core_alua_queue_state_change_ua(tg_pt_gp
);
1088 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1092 int core_alua_do_port_transition(
1093 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
1094 struct se_device
*l_dev
,
1095 struct se_lun
*l_lun
,
1096 struct se_node_acl
*l_nacl
,
1100 struct se_device
*dev
;
1101 struct t10_alua_lu_gp
*lu_gp
;
1102 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
1103 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1104 int primary
, valid_states
, rc
= 0;
1106 if (l_dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
)
1109 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1110 if (core_alua_check_transition(new_state
, valid_states
, &primary
,
1114 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
1115 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1116 lu_gp
= local_lu_gp_mem
->lu_gp
;
1117 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1118 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1120 * For storage objects that are members of the 'default_lu_gp',
1121 * we only do transition on the passed *l_tp_pt_gp, and not
1122 * on all of the matching target port groups IDs in default_lu_gp.
1124 if (!lu_gp
->lu_gp_id
) {
1126 * core_alua_do_transition_tg_pt() will always return
1129 l_tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1130 l_tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1131 rc
= core_alua_do_transition_tg_pt(l_tg_pt_gp
,
1132 new_state
, explicit);
1133 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1137 * For all other LU groups aside from 'default_lu_gp', walk all of
1138 * the associated storage objects looking for a matching target port
1139 * group ID from the local target port group.
1141 spin_lock(&lu_gp
->lu_gp_lock
);
1142 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
1145 dev
= lu_gp_mem
->lu_gp_mem_dev
;
1146 atomic_inc_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1147 spin_unlock(&lu_gp
->lu_gp_lock
);
1149 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1150 list_for_each_entry(tg_pt_gp
,
1151 &dev
->t10_alua
.tg_pt_gps_list
,
1154 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1157 * If the target behavior port asymmetric access state
1158 * is changed for any target port group accessible via
1159 * a logical unit within a LU group, the target port
1160 * behavior group asymmetric access states for the same
1161 * target port group accessible via other logical units
1162 * in that LU group will also change.
1164 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
1167 if (l_tg_pt_gp
== tg_pt_gp
) {
1168 tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1169 tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1171 tg_pt_gp
->tg_pt_gp_alua_lun
= NULL
;
1172 tg_pt_gp
->tg_pt_gp_alua_nacl
= NULL
;
1174 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1175 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1177 * core_alua_do_transition_tg_pt() will always return
1180 rc
= core_alua_do_transition_tg_pt(tg_pt_gp
,
1181 new_state
, explicit);
1183 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1184 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1188 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1190 spin_lock(&lu_gp
->lu_gp_lock
);
1191 atomic_dec_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1193 spin_unlock(&lu_gp
->lu_gp_lock
);
1196 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1197 " Group IDs: %hu %s transition to primary state: %s\n",
1198 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1199 l_tg_pt_gp
->tg_pt_gp_id
,
1200 (explicit) ? "explicit" : "implicit",
1201 core_alua_dump_state(new_state
));
1204 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1208 static int core_alua_update_tpg_secondary_metadata(struct se_lun
*lun
)
1210 struct se_portal_group
*se_tpg
= lun
->lun_tpg
;
1211 unsigned char *md_buf
;
1215 mutex_lock(&lun
->lun_tg_pt_md_mutex
);
1217 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
1219 pr_err("Unable to allocate buf for ALUA metadata\n");
1224 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
, "alua_tg_pt_offline=%d\n"
1225 "alua_tg_pt_status=0x%02x\n",
1226 atomic_read(&lun
->lun_tg_pt_secondary_offline
),
1227 lun
->lun_tg_pt_secondary_stat
);
1229 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
) {
1230 path
= kasprintf(GFP_KERNEL
, "%s/alua/%s/%s+%hu/lun_%llu",
1231 db_root
, se_tpg
->se_tpg_tfo
->fabric_name
,
1232 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
),
1233 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
),
1236 path
= kasprintf(GFP_KERNEL
, "%s/alua/%s/%s/lun_%llu",
1237 db_root
, se_tpg
->se_tpg_tfo
->fabric_name
,
1238 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
),
1246 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
1251 mutex_unlock(&lun
->lun_tg_pt_md_mutex
);
1255 static int core_alua_set_tg_pt_secondary_state(
1260 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1261 int trans_delay_msecs
;
1263 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1264 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1266 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1267 pr_err("Unable to complete secondary state"
1271 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1273 * Set the secondary ALUA target port access state to OFFLINE
1274 * or release the previously secondary state for struct se_lun
1277 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 1);
1279 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 0);
1281 lun
->lun_tg_pt_secondary_stat
= (explicit) ?
1282 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1283 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1285 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1286 " to secondary access state: %s\n", (explicit) ? "explicit" :
1287 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1288 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1290 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1292 * Do the optional transition delay after we set the secondary
1293 * ALUA access state.
1295 if (trans_delay_msecs
!= 0)
1296 msleep_interruptible(trans_delay_msecs
);
1298 * See if we need to update the ALUA fabric port metadata for
1299 * secondary state and status
1301 if (lun
->lun_tg_pt_secondary_write_md
)
1302 core_alua_update_tpg_secondary_metadata(lun
);
1307 struct t10_alua_lba_map
*
1308 core_alua_allocate_lba_map(struct list_head
*list
,
1309 u64 first_lba
, u64 last_lba
)
1311 struct t10_alua_lba_map
*lba_map
;
1313 lba_map
= kmem_cache_zalloc(t10_alua_lba_map_cache
, GFP_KERNEL
);
1315 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1316 return ERR_PTR(-ENOMEM
);
1318 INIT_LIST_HEAD(&lba_map
->lba_map_mem_list
);
1319 lba_map
->lba_map_first_lba
= first_lba
;
1320 lba_map
->lba_map_last_lba
= last_lba
;
1322 list_add_tail(&lba_map
->lba_map_list
, list
);
1327 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map
*lba_map
,
1328 int pg_id
, int state
)
1330 struct t10_alua_lba_map_member
*lba_map_mem
;
1332 list_for_each_entry(lba_map_mem
, &lba_map
->lba_map_mem_list
,
1334 if (lba_map_mem
->lba_map_mem_alua_pg_id
== pg_id
) {
1335 pr_err("Duplicate pg_id %d in lba_map\n", pg_id
);
1340 lba_map_mem
= kmem_cache_zalloc(t10_alua_lba_map_mem_cache
, GFP_KERNEL
);
1342 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1345 lba_map_mem
->lba_map_mem_alua_state
= state
;
1346 lba_map_mem
->lba_map_mem_alua_pg_id
= pg_id
;
1348 list_add_tail(&lba_map_mem
->lba_map_mem_list
,
1349 &lba_map
->lba_map_mem_list
);
1354 core_alua_free_lba_map(struct list_head
*lba_list
)
1356 struct t10_alua_lba_map
*lba_map
, *lba_map_tmp
;
1357 struct t10_alua_lba_map_member
*lba_map_mem
, *lba_map_mem_tmp
;
1359 list_for_each_entry_safe(lba_map
, lba_map_tmp
, lba_list
,
1361 list_for_each_entry_safe(lba_map_mem
, lba_map_mem_tmp
,
1362 &lba_map
->lba_map_mem_list
,
1364 list_del(&lba_map_mem
->lba_map_mem_list
);
1365 kmem_cache_free(t10_alua_lba_map_mem_cache
,
1368 list_del(&lba_map
->lba_map_list
);
1369 kmem_cache_free(t10_alua_lba_map_cache
, lba_map
);
1374 core_alua_set_lba_map(struct se_device
*dev
, struct list_head
*lba_map_list
,
1375 int segment_size
, int segment_mult
)
1377 struct list_head old_lba_map_list
;
1378 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1379 int activate
= 0, supported
;
1381 INIT_LIST_HEAD(&old_lba_map_list
);
1382 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1383 dev
->t10_alua
.lba_map_segment_size
= segment_size
;
1384 dev
->t10_alua
.lba_map_segment_multiplier
= segment_mult
;
1385 list_splice_init(&dev
->t10_alua
.lba_map_list
, &old_lba_map_list
);
1387 list_splice_init(lba_map_list
, &dev
->t10_alua
.lba_map_list
);
1390 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1391 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1392 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1395 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1397 supported
= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1399 supported
|= ALUA_LBD_SUP
;
1401 supported
&= ~ALUA_LBD_SUP
;
1402 tg_pt_gp
->tg_pt_gp_alua_supported_states
= supported
;
1404 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1405 core_alua_free_lba_map(&old_lba_map_list
);
1408 struct t10_alua_lu_gp
*
1409 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1411 struct t10_alua_lu_gp
*lu_gp
;
1413 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1415 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1416 return ERR_PTR(-ENOMEM
);
1418 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1419 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1420 spin_lock_init(&lu_gp
->lu_gp_lock
);
1421 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1424 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1425 lu_gp
->lu_gp_valid_id
= 1;
1426 alua_lu_gps_count
++;
1432 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1434 struct t10_alua_lu_gp
*lu_gp_tmp
;
1437 * The lu_gp->lu_gp_id may only be set once..
1439 if (lu_gp
->lu_gp_valid_id
) {
1440 pr_warn("ALUA LU Group already has a valid ID,"
1441 " ignoring request\n");
1445 spin_lock(&lu_gps_lock
);
1446 if (alua_lu_gps_count
== 0x0000ffff) {
1447 pr_err("Maximum ALUA alua_lu_gps_count:"
1448 " 0x0000ffff reached\n");
1449 spin_unlock(&lu_gps_lock
);
1450 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1454 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1455 alua_lu_gps_counter
++;
1457 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1458 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1462 pr_warn("ALUA Logical Unit Group ID: %hu"
1463 " already exists, ignoring request\n",
1465 spin_unlock(&lu_gps_lock
);
1470 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1471 lu_gp
->lu_gp_valid_id
= 1;
1472 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1473 alua_lu_gps_count
++;
1474 spin_unlock(&lu_gps_lock
);
1479 static struct t10_alua_lu_gp_member
*
1480 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1482 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1484 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1486 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1487 return ERR_PTR(-ENOMEM
);
1489 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1490 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1491 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1493 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1494 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1499 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1501 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1503 * Once we have reached this point, config_item_put() has
1504 * already been called from target_core_alua_drop_lu_gp().
1506 * Here, we remove the *lu_gp from the global list so that
1507 * no associations can be made while we are releasing
1508 * struct t10_alua_lu_gp.
1510 spin_lock(&lu_gps_lock
);
1511 list_del(&lu_gp
->lu_gp_node
);
1512 alua_lu_gps_count
--;
1513 spin_unlock(&lu_gps_lock
);
1515 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1516 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1517 * released with core_alua_put_lu_gp_from_name()
1519 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1522 * Release reference to struct t10_alua_lu_gp * from all associated
1525 spin_lock(&lu_gp
->lu_gp_lock
);
1526 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1527 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1528 if (lu_gp_mem
->lu_gp_assoc
) {
1529 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1530 lu_gp
->lu_gp_members
--;
1531 lu_gp_mem
->lu_gp_assoc
= 0;
1533 spin_unlock(&lu_gp
->lu_gp_lock
);
1536 * lu_gp_mem is associated with a single
1537 * struct se_device->dev_alua_lu_gp_mem, and is released when
1538 * struct se_device is released via core_alua_free_lu_gp_mem().
1540 * If the passed lu_gp does NOT match the default_lu_gp, assume
1541 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1543 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1544 if (lu_gp
!= default_lu_gp
)
1545 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1548 lu_gp_mem
->lu_gp
= NULL
;
1549 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1551 spin_lock(&lu_gp
->lu_gp_lock
);
1553 spin_unlock(&lu_gp
->lu_gp_lock
);
1555 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1558 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1560 struct t10_alua_lu_gp
*lu_gp
;
1561 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1563 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1567 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1570 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1571 lu_gp
= lu_gp_mem
->lu_gp
;
1573 spin_lock(&lu_gp
->lu_gp_lock
);
1574 if (lu_gp_mem
->lu_gp_assoc
) {
1575 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1576 lu_gp
->lu_gp_members
--;
1577 lu_gp_mem
->lu_gp_assoc
= 0;
1579 spin_unlock(&lu_gp
->lu_gp_lock
);
1580 lu_gp_mem
->lu_gp
= NULL
;
1582 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1584 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1587 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1589 struct t10_alua_lu_gp
*lu_gp
;
1590 struct config_item
*ci
;
1592 spin_lock(&lu_gps_lock
);
1593 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1594 if (!lu_gp
->lu_gp_valid_id
)
1596 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1597 if (!strcmp(config_item_name(ci
), name
)) {
1598 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1599 spin_unlock(&lu_gps_lock
);
1603 spin_unlock(&lu_gps_lock
);
1608 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1610 spin_lock(&lu_gps_lock
);
1611 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1612 spin_unlock(&lu_gps_lock
);
1616 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1618 void __core_alua_attach_lu_gp_mem(
1619 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1620 struct t10_alua_lu_gp
*lu_gp
)
1622 spin_lock(&lu_gp
->lu_gp_lock
);
1623 lu_gp_mem
->lu_gp
= lu_gp
;
1624 lu_gp_mem
->lu_gp_assoc
= 1;
1625 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1626 lu_gp
->lu_gp_members
++;
1627 spin_unlock(&lu_gp
->lu_gp_lock
);
1631 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1633 void __core_alua_drop_lu_gp_mem(
1634 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1635 struct t10_alua_lu_gp
*lu_gp
)
1637 spin_lock(&lu_gp
->lu_gp_lock
);
1638 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1639 lu_gp_mem
->lu_gp
= NULL
;
1640 lu_gp_mem
->lu_gp_assoc
= 0;
1641 lu_gp
->lu_gp_members
--;
1642 spin_unlock(&lu_gp
->lu_gp_lock
);
1645 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(struct se_device
*dev
,
1646 const char *name
, int def_group
)
1648 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1650 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1652 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1655 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1656 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_lun_list
);
1657 mutex_init(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1658 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1659 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1660 tg_pt_gp
->tg_pt_gp_dev
= dev
;
1661 tg_pt_gp
->tg_pt_gp_alua_access_state
=
1662 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
;
1664 * Enable both explicit and implicit ALUA support by default
1666 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1667 TPGS_EXPLICIT_ALUA
| TPGS_IMPLICIT_ALUA
;
1669 * Set the default Active/NonOptimized Delay in milliseconds
1671 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1672 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1673 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= ALUA_DEFAULT_IMPLICIT_TRANS_SECS
;
1676 * Enable all supported states
1678 tg_pt_gp
->tg_pt_gp_alua_supported_states
=
1679 ALUA_T_SUP
| ALUA_O_SUP
|
1680 ALUA_U_SUP
| ALUA_S_SUP
| ALUA_AN_SUP
| ALUA_AO_SUP
;
1683 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1684 tg_pt_gp
->tg_pt_gp_id
=
1685 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1686 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1687 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1688 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1689 &dev
->t10_alua
.tg_pt_gps_list
);
1690 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1696 int core_alua_set_tg_pt_gp_id(
1697 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1700 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1701 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1702 u16 tg_pt_gp_id_tmp
;
1705 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1707 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1708 pr_warn("ALUA TG PT Group already has a valid ID,"
1709 " ignoring request\n");
1713 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1714 if (dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1715 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1716 " 0x0000ffff reached\n");
1717 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1718 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1722 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1723 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1725 list_for_each_entry(tg_pt_gp_tmp
, &dev
->t10_alua
.tg_pt_gps_list
,
1727 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1731 pr_err("ALUA Target Port Group ID: %hu already"
1732 " exists, ignoring request\n", tg_pt_gp_id
);
1733 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1738 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1739 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1740 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1741 &dev
->t10_alua
.tg_pt_gps_list
);
1742 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1743 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1748 void core_alua_free_tg_pt_gp(
1749 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1751 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1752 struct se_lun
*lun
, *next
;
1755 * Once we have reached this point, config_item_put() has already
1756 * been called from target_core_alua_drop_tg_pt_gp().
1758 * Here we remove *tg_pt_gp from the global list so that
1759 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1760 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1762 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1763 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1764 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1765 dev
->t10_alua
.alua_tg_pt_gps_count
--;
1767 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1770 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1771 * core_alua_get_tg_pt_gp_by_name() in
1772 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1773 * to be released with core_alua_put_tg_pt_gp_from_name().
1775 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1779 * Release reference to struct t10_alua_tg_pt_gp from all associated
1782 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1783 list_for_each_entry_safe(lun
, next
,
1784 &tg_pt_gp
->tg_pt_gp_lun_list
, lun_tg_pt_gp_link
) {
1785 list_del_init(&lun
->lun_tg_pt_gp_link
);
1786 tg_pt_gp
->tg_pt_gp_members
--;
1788 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1790 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1791 * assume we want to re-associate a given tg_pt_gp_mem with
1794 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1795 if (tg_pt_gp
!= dev
->t10_alua
.default_tg_pt_gp
) {
1796 __target_attach_tg_pt_gp(lun
,
1797 dev
->t10_alua
.default_tg_pt_gp
);
1799 lun
->lun_tg_pt_gp
= NULL
;
1800 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1802 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1804 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1806 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1809 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1810 struct se_device
*dev
, const char *name
)
1812 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1813 struct config_item
*ci
;
1815 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1816 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1818 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1820 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1821 if (!strcmp(config_item_name(ci
), name
)) {
1822 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1823 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1827 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1832 static void core_alua_put_tg_pt_gp_from_name(
1833 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1835 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1837 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1838 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1839 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1842 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
1843 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1845 struct se_dev_entry
*se_deve
;
1847 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1849 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1850 lun
->lun_tg_pt_gp
= tg_pt_gp
;
1851 list_add_tail(&lun
->lun_tg_pt_gp_link
, &tg_pt_gp
->tg_pt_gp_lun_list
);
1852 tg_pt_gp
->tg_pt_gp_members
++;
1853 spin_lock(&lun
->lun_deve_lock
);
1854 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
)
1855 core_scsi3_ua_allocate(se_deve
, 0x3f,
1856 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED
);
1857 spin_unlock(&lun
->lun_deve_lock
);
1858 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1861 void target_attach_tg_pt_gp(struct se_lun
*lun
,
1862 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1864 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1865 __target_attach_tg_pt_gp(lun
, tg_pt_gp
);
1866 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1869 static void __target_detach_tg_pt_gp(struct se_lun
*lun
,
1870 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1872 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1874 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1875 list_del_init(&lun
->lun_tg_pt_gp_link
);
1876 tg_pt_gp
->tg_pt_gp_members
--;
1877 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1879 lun
->lun_tg_pt_gp
= NULL
;
1882 void target_detach_tg_pt_gp(struct se_lun
*lun
)
1884 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1886 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1887 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1889 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1890 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1893 ssize_t
core_alua_show_tg_pt_gp_info(struct se_lun
*lun
, char *page
)
1895 struct config_item
*tg_pt_ci
;
1896 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1899 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1900 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1902 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1903 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1904 " %hu\nTG Port Primary Access State: %s\nTG Port "
1905 "Primary Access Status: %s\nTG Port Secondary Access"
1906 " State: %s\nTG Port Secondary Access Status: %s\n",
1907 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1908 core_alua_dump_state(
1909 tg_pt_gp
->tg_pt_gp_alua_access_state
),
1910 core_alua_dump_status(
1911 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1912 atomic_read(&lun
->lun_tg_pt_secondary_offline
) ?
1914 core_alua_dump_status(lun
->lun_tg_pt_secondary_stat
));
1916 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1921 ssize_t
core_alua_store_tg_pt_gp_info(
1926 struct se_portal_group
*tpg
= lun
->lun_tpg
;
1928 * rcu_dereference_raw protected by se_lun->lun_group symlink
1929 * reference to se_device->dev_group.
1931 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
1932 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1933 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1936 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
||
1937 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
1940 if (count
> TG_PT_GROUP_NAME_BUF
) {
1941 pr_err("ALUA Target Port Group alias too large!\n");
1944 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1945 memcpy(buf
, page
, count
);
1947 * Any ALUA target port group alias besides "NULL" means we will be
1948 * making a new group association.
1950 if (strcmp(strstrip(buf
), "NULL")) {
1952 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1953 * struct t10_alua_tg_pt_gp. This reference is released with
1954 * core_alua_put_tg_pt_gp_from_name() below.
1956 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(dev
,
1962 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1963 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1966 * Clearing an existing tg_pt_gp association, and replacing
1967 * with the default_tg_pt_gp.
1969 if (!tg_pt_gp_new
) {
1970 pr_debug("Target_Core_ConfigFS: Moving"
1971 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1972 " alua/%s, ID: %hu back to"
1973 " default_tg_pt_gp\n",
1974 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1975 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1976 config_item_name(&lun
->lun_group
.cg_item
),
1978 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1979 tg_pt_gp
->tg_pt_gp_id
);
1981 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1982 __target_attach_tg_pt_gp(lun
,
1983 dev
->t10_alua
.default_tg_pt_gp
);
1984 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1988 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1992 __target_attach_tg_pt_gp(lun
, tg_pt_gp_new
);
1993 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1994 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1995 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1996 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1997 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1998 config_item_name(&lun
->lun_group
.cg_item
),
1999 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
2000 tg_pt_gp_new
->tg_pt_gp_id
);
2002 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
2006 ssize_t
core_alua_show_access_type(
2007 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2010 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
) &&
2011 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
))
2012 return sprintf(page
, "Implicit and Explicit\n");
2013 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
)
2014 return sprintf(page
, "Implicit\n");
2015 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)
2016 return sprintf(page
, "Explicit\n");
2018 return sprintf(page
, "None\n");
2021 ssize_t
core_alua_store_access_type(
2022 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2029 ret
= kstrtoul(page
, 0, &tmp
);
2031 pr_err("Unable to extract alua_access_type\n");
2034 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
2035 pr_err("Illegal value for alua_access_type:"
2040 tg_pt_gp
->tg_pt_gp_alua_access_type
=
2041 TPGS_IMPLICIT_ALUA
| TPGS_EXPLICIT_ALUA
;
2043 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICIT_ALUA
;
2045 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICIT_ALUA
;
2047 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
2052 ssize_t
core_alua_show_nonop_delay_msecs(
2053 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2056 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
2059 ssize_t
core_alua_store_nonop_delay_msecs(
2060 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2067 ret
= kstrtoul(page
, 0, &tmp
);
2069 pr_err("Unable to extract nonop_delay_msecs\n");
2072 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
2073 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2074 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
2075 ALUA_MAX_NONOP_DELAY_MSECS
);
2078 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
2083 ssize_t
core_alua_show_trans_delay_msecs(
2084 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2087 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
2090 ssize_t
core_alua_store_trans_delay_msecs(
2091 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2098 ret
= kstrtoul(page
, 0, &tmp
);
2100 pr_err("Unable to extract trans_delay_msecs\n");
2103 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
2104 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2105 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
2106 ALUA_MAX_TRANS_DELAY_MSECS
);
2109 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
2114 ssize_t
core_alua_show_implicit_trans_secs(
2115 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2118 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implicit_trans_secs
);
2121 ssize_t
core_alua_store_implicit_trans_secs(
2122 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2129 ret
= kstrtoul(page
, 0, &tmp
);
2131 pr_err("Unable to extract implicit_trans_secs\n");
2134 if (tmp
> ALUA_MAX_IMPLICIT_TRANS_SECS
) {
2135 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2136 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp
,
2137 ALUA_MAX_IMPLICIT_TRANS_SECS
);
2140 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= (int)tmp
;
2145 ssize_t
core_alua_show_preferred_bit(
2146 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2149 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
2152 ssize_t
core_alua_store_preferred_bit(
2153 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2160 ret
= kstrtoul(page
, 0, &tmp
);
2162 pr_err("Unable to extract preferred ALUA value\n");
2165 if ((tmp
!= 0) && (tmp
!= 1)) {
2166 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
2169 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
2174 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
2176 return sprintf(page
, "%d\n",
2177 atomic_read(&lun
->lun_tg_pt_secondary_offline
));
2180 ssize_t
core_alua_store_offline_bit(
2186 * rcu_dereference_raw protected by se_lun->lun_group symlink
2187 * reference to se_device->dev_group.
2189 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
2193 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
||
2194 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
2197 ret
= kstrtoul(page
, 0, &tmp
);
2199 pr_err("Unable to extract alua_tg_pt_offline value\n");
2202 if ((tmp
!= 0) && (tmp
!= 1)) {
2203 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2208 ret
= core_alua_set_tg_pt_secondary_state(lun
, 0, (int)tmp
);
2215 ssize_t
core_alua_show_secondary_status(
2219 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_stat
);
2222 ssize_t
core_alua_store_secondary_status(
2230 ret
= kstrtoul(page
, 0, &tmp
);
2232 pr_err("Unable to extract alua_tg_pt_status\n");
2235 if ((tmp
!= ALUA_STATUS_NONE
) &&
2236 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
2237 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
)) {
2238 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2242 lun
->lun_tg_pt_secondary_stat
= (int)tmp
;
2247 ssize_t
core_alua_show_secondary_write_metadata(
2251 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_write_md
);
2254 ssize_t
core_alua_store_secondary_write_metadata(
2262 ret
= kstrtoul(page
, 0, &tmp
);
2264 pr_err("Unable to extract alua_tg_pt_write_md\n");
2267 if ((tmp
!= 0) && (tmp
!= 1)) {
2268 pr_err("Illegal value for alua_tg_pt_write_md:"
2272 lun
->lun_tg_pt_secondary_write_md
= (int)tmp
;
2277 int core_setup_alua(struct se_device
*dev
)
2279 if (!(dev
->transport
->transport_flags
&
2280 TRANSPORT_FLAG_PASSTHROUGH_ALUA
) &&
2281 !(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)) {
2282 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2285 * Associate this struct se_device with the default ALUA
2288 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2289 if (IS_ERR(lu_gp_mem
))
2290 return PTR_ERR(lu_gp_mem
);
2292 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2293 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2295 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2297 pr_debug("%s: Adding to default ALUA LU Group:"
2298 " core/alua/lu_gps/default_lu_gp\n",
2299 dev
->transport
->name
);